1 filter.admin.inc filter_admin_format_form($form, &$form_state, $format)

Form constructor for the text format add/edit form.

Parameters

$format: A format object having the properties:

  • format: A machine-readable name representing the ID of the text format to save. If this corresponds to an existing text format, that format will be updated; otherwise, a new format will be created.
  • name: The title of the text format.
  • cache: An integer indicating whether the text format is cacheable (1) or not (0). Defaults to 1.
  • status: (optional) An integer indicating whether the text format is enabled (1) or not (0). Defaults to 1.
  • weight: (optional) The weight of the text format, which controls its placement in text format lists. If omitted, the weight is set to 0.

See also

filter_admin_format_form_validate()

filter_admin_format_form_submit()

Related topics

File

core/modules/filter/filter.admin.inc, line 197
Admin page callbacks for the Filter module.

Code

function filter_admin_format_form($form, &$form_state, $format) {
  // Use a format stored in tempstore if available.
  if ($stored_format = filter_get_format_tempstore($format->format)) {
    filter_admin_set_message($stored_format);
    $format = $stored_format;
  }
  $is_fallback = ($format->format == filter_fallback_format());
  $editors = filter_get_editors();
  if (isset($form_state['editor_info'])) {
    $editor_info = $form_state['editor_info'];

    // If the editor changed, update the format settings accordingly.
    $current_editor = $form_state['values']['editor'];
    if ($format->editor != $current_editor && isset($editors[$current_editor])) {
      $format->editor = $current_editor;
      $format->editor_settings = array();
    }
  }
  else {
    $editor_info = ($format->editor && isset($editors[$format->editor])) ? $editors[$format->editor] : NULL;
  }

  $form['messages'] = array(
    '#theme' => 'status_messages',
    '#messages' => '',
    '#weight' => -100,
    // Prefix/suffix used to identify in AJAX requests.
    '#prefix' => '<div id="filter-messages">',
    '#suffix' => '</div>',
  );

  $form_state['format'] = $format;
  $form_state['editor_info'] = $editor_info;

  $form['#validate'] = array();
  $form['#submit'] = array();
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = array('filter', 'filter.admin');
  $form['#attached']['library'][] = array('filter', 'filter.filtered_html.admin');
  $form['#attached']['library'][] = array('system', 'backdrop.dialog');

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $format->name,
    '#required' => TRUE,
    '#weight' => -20,
  );
  $form['format'] = array(
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#default_value' => $format->format,
    '#maxlength' => 255,
    '#disabled' => !empty($format->format),
    '#machine_name' => array(
      'exists' => 'filter_format_exists',
    ),
    '#weight' => -19,
  );

  // Build the list of all available editors.
  $editor_options = array('' => t('None'));
  foreach ($editors as $editor_name => $editor) {
    $editor_options[$editor_name] = $editor['label'];
  }

  // Associate an editor with this format.
  if ($editor_info) {
    // Load the associated editor callbacks file, if any.
    if (!empty($editor_info['file'])) {
      $filepath = $editor_info['file'];
      $extension = substr($filepath, strrpos($filepath, '.') + 1);
      $filepath = substr($filepath, 0, strrpos($filepath, '.'));
      form_load_include($form_state, $extension, $editor_info['module'], $filepath);
    }
  }

  $form['editor'] = array(
    '#weight' => -9,
  );
  $form['editor']['editor'] = array(
    '#type' => 'select',
    '#title' => t('Text editor'),
    '#options' => $editor_options,
    '#empty_option' => t('None'),
    '#default_value' => $format->editor ? $format->editor : '',
    '#ajax' => array(
      'trigger_as' => array('name' => 'editor_configure'),
      'callback' => 'filter_admin_format_editor_ajax',
      'wrapper' => 'editor-settings-wrapper',
    ),
    '#parents' => array('editor'),
  );
  $form['editor']['configure'] = array(
    '#type' => 'submit',
    '#name' => 'editor_configure',
    '#value' => t('Configure editor'),
    '#limit_validation_errors' => array(array('editor')),
    '#submit' => array('filter_admin_format_editor_submit'),
    '#ajax' => array(
      'callback' => 'filter_admin_format_editor_ajax',
      'wrapper' => 'editor-settings-wrapper',
    ),
    '#attributes' => array('class' => array('js-hide')),
    '#parents' => array('editor_configure'),
  );

  // If there aren't any options (other than "None"), disable the select list.
  if (empty($editor_options)) {
    $form['editor']['editor']['#disabled'] = TRUE;
    $form['editor']['editor']['#description'] = t('This option is disabled because no modules that provide a text editor are currently enabled.');
  }

  $form['editor_settings'] = array(
    '#tree' => TRUE,
    '#weight' => -8,
    '#type' => 'container',
    '#id' => 'editor-settings-wrapper',
  );

  // Populate editor defaults.
  if (!empty($editor_info['default settings'])) {
    $format->editor_settings += $editor_info['default settings'];
  }

  // Add editor-specific validation and submit handlers.
  if (!empty($editor_info['settings callback'])) {
    $function = $editor_info['settings callback'];
    $form['editor_settings'] = array_merge($function($form, $form_state, $format), $form['editor_settings']);
    $form['editor_settings']['#parents'] = array('editor_settings');
  }

  // Add user role access selection.
  $form['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#options' => array_map('check_plain', user_roles()),
    '#disabled' => $is_fallback,
  );
  if ($is_fallback) {
    $form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
  }
  if (!empty($format->format)) {
    // If editing an existing text format, pre-select its current permissions.
    $form['roles']['#default_value'] = filter_get_roles_by_format($format);
  }
  elseif ($admin_role = config_get('system.core', 'user_admin_role')) {
    // If adding a new text format and the site has an administrative role,
    // pre-select that role so as to grant administrators access to the new
    // text format permission by default.
    $form['roles']['#default_value'] = array($admin_role);
  }

  // Retrieve available filters and load all configured filters for existing
  // text formats.
  $all_filter_info = filter_get_filters();

  // Create an empty filter object for new/unconfigured filters.
  foreach ($all_filter_info as $name => $filter_info) {
    if (!isset($format->filters[$name])) {
      $format->filters[$name] = (object) array(
        'format' => $format->format,
        'module' => $filter_info['module'],
        'name' => $name,
        'status' => 0,
        'weight' => $filter_info['weight'],
        'settings' => $filter_info['default settings'],
      );
    }
  }

  // Filter order (tabledrag).
  $form['filters'] = array(
    '#type' => 'item',
    '#title' => t('Filter settings'),
    '#theme' => 'filter_admin_format_filter_order',
  );
  foreach ($all_filter_info as $name => $filter_info) {
    $filter_config = $format->filters[$name];
    $form['filters'][$name]['status'] = array(
      '#type' => 'checkbox',
      '#default_value' => $filter_config->status,
      '#attributes' => array('class' => array('filter-status')),
    );
    if (isset($filter_info['settings callback'])) {
      $form['filters'][$name]['configure_button'] = array(
        '#type' => 'submit',
        '#name' => $name,
        '#validate' => array('filter_admin_format_filter_settings_form_redirect_validate'),
        '#submit' => array('filter_admin_format_filter_settings_form_redirect'),
        '#ajax' => array(
          'callback' => 'filter_admin_format_filter_settings_form_ajax',
          'effect' => 'fade',
        ),
        '#value' => t('Configure'),
      );
    }
    $form['filters'][$name]['filter'] = array(
      '#type' => 'item',
      '#title' => $filter_info['title'],
      '#description' => _filter_admin_format_get_tips($format, $name),
      '#wrapper_attributes' => array(
        'class' => array('filter-info'),
        'data-filter-name' => $name,
      ),
    );

    $form['filters'][$name]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array('@title' => $filter_info['title'])),
      '#title_display' => 'invisible',
      '#delta' => 50,
      '#default_value' => $filter_config->weight,
      '#parents' => array('filters', $name, 'weight'),
    );
    $form['filters'][$name]['#weight'] = $filter_config->weight;
  }

  // This hidden field is used to store allowed HTML tags for the filter_html
  // filter. This is necessary to allow JS to respond to changes in editor
  // buttons, changing allowed tags in response.
  $filter_html = $format->filters['filter_html'];
  $form['allowed_html'] = array(
    '#type' => 'hidden',
    '#maxlength' => 1024,
    '#attributes' => array('id' => array('allowed-html')),
    '#default_value' => $filter_html->settings['allowed_html'],
  );

  $form['#validate'][] = 'filter_admin_format_form_validate';
  $form['#submit'][] = 'filter_admin_format_form_submit';

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#validate' => array(),
    '#submit' => array('filter_admin_format_form_cancel'),
  );

  return $form;
}