FilterFormatFormBase::form

public FilterFormatFormBase::form(array $form, FormStateInterface $form_state)

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/filter/src/FilterFormatFormBase.php, line 45

Class

FilterFormatFormBase
Provides a base form for a filter format.

Namespace

Drupal\filter

Code

public function form(array $form, FormStateInterface $form_state) {
  $format = $this->entity;
  $is_fallback = ($format->id() == $this->config('filter.settings')->get('fallback_format'));

  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'filter/drupal.filter.admin';

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => $this->t('Name'),
    '#default_value' => $format->label(),
    '#required' => TRUE,
    '#weight' => -30,
  );
  $form['format'] = array(
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#default_value' => $format->id(),
    '#maxlength' => 255,
    '#machine_name' => array(
      'exists' => array($this, 'exists'),
      'source' => array('name'),
    ),
    '#disabled' => !$format->isNew(),
    '#weight' => -20,
  );

  // Add user role access selection.
  $form['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => $this->t('Roles'),
    '#options' => array_map('\Drupal\Component\Utility\Html::escape', user_role_names()),
    '#disabled' => $is_fallback,
    '#weight' => -10,
  );
  if ($is_fallback) {
    $form['roles']['#description'] = $this->t('All roles for this text format must be enabled and cannot be changed.');
  }
  if (!$format->isNew()) {
    // If editing an existing text format, pre-select its current permissions.
    $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
  }

  // Create filter plugin instances for all available filters, including both
  // enabled/configured ones as well as new and not yet unconfigured ones.
  $filters = $format->filters();
  foreach ($filters as $filter_id => $filter) {
    // When a filter is missing, it is replaced by the null filter. Remove it
    // here, so that saving the form will remove the missing filter.
    if ($filter instanceof FilterNull) {
      drupal_set_message($this->t('The %filter filter is missing, and will be removed once this format is saved.', array('%filter' => $filter_id)), 'warning');
      $filters->removeInstanceID($filter_id);
    }
  }

  // Filter status.
  $form['filters']['status'] = array(
    '#type' => 'item',
    '#title' => $this->t('Enabled filters'),
    '#prefix' => '<div id="filters-status-wrapper">',
    '#suffix' => '</div>',
    // This item is used as a pure wrapping container with heading. Ignore its
    // value, since 'filters' should only contain filter definitions.
    // See https://www.drupal.org/node/1829202.
    '#input' => FALSE,
  );
  // Filter order (tabledrag).
  $form['filters']['order'] = array(
    '#type' => 'table',
    // For filter.admin.js
    '#attributes' => array('id' => 'filter-order'),
    '#title' => $this->t('Filter processing order'),
    '#tabledrag' => array(
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'filter-order-weight',
      ),
    ),
    '#tree' => FALSE,
    '#input' => FALSE,
    '#theme_wrappers' => array('form_element'),
  );
  // Filter settings.
  $form['filter_settings'] = array(
    '#type' => 'vertical_tabs',
    '#title' => $this->t('Filter settings'),
  );

  foreach ($filters as $name => $filter) {
    $form['filters']['status'][$name] = array(
      '#type' => 'checkbox',
      '#title' => $filter->getLabel(),
      '#default_value' => $filter->status,
      '#parents' => array('filters', $name, 'status'),
      '#description' => $filter->getDescription(),
      '#weight' => $filter->weight,
    );

    $form['filters']['order'][$name]['#attributes']['class'][] = 'draggable';
    $form['filters']['order'][$name]['#weight'] = $filter->weight;
    $form['filters']['order'][$name]['filter'] = array(
      '#markup' => $filter->getLabel(),
    );
    $form['filters']['order'][$name]['weight'] = array(
      '#type' => 'weight',
      '#title' => $this->t('Weight for @title', array('@title' => $filter->getLabel())),
      '#title_display' => 'invisible',
      '#delta' => 50,
      '#default_value' => $filter->weight,
      '#parents' => array('filters', $name, 'weight'),
      '#attributes' => array('class' => array('filter-order-weight')),
    );

    // Retrieve the settings form of the filter plugin. The plugin should not be
    // aware of the text format. Therefore, it only receives a set of minimal
    // base properties to allow advanced implementations to work.
    $settings_form = array(
      '#parents' => array('filters', $name, 'settings'),
      '#tree' => TRUE,
    );
    $settings_form = $filter->settingsForm($settings_form, $form_state);
    if (!empty($settings_form)) {
      $form['filters']['settings'][$name] = array(
        '#type' => 'details',
        '#title' => $filter->getLabel(),
        '#open' => TRUE,
        '#weight' => $filter->weight,
        '#parents' => array('filters', $name, 'settings'),
        '#group' => 'filter_settings',
      );
      $form['filters']['settings'][$name] += $settings_form;
    }
  }
  return parent::form($form, $form_state);
}
doc_Drupal
2016-10-29 09:14:42
Comments
Leave a Comment

Please login to continue.