You are here

public function WordfilterConfigurationForm::buildForm in Wordfilter 8.2

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/WordfilterConfigurationForm.php, line 44

Class

WordfilterConfigurationForm
Class WordfilterConfigurationForm.

Namespace

Drupal\wordfilter\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form_state
    ->setCached(FALSE);
  $wordfilter_configuration = $this
    ->getWordfilterConfiguration();
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $wordfilter_configuration
      ->label(),
    '#description' => $this
      ->t("Label for the Wordfilter configuration."),
    '#required' => TRUE,
    '#weight' => 10,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $wordfilter_configuration
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\wordfilter\\Entity\\WordfilterConfiguration::load',
    ],
    '#disabled' => !$wordfilter_configuration
      ->isNew(),
    '#weight' => 20,
  ];
  $form['process'] = [
    '#type' => 'fieldset',
    '#title' => t('Filtering process'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => 30,
  ];

  /**
   * @var \Drupal\wordfilter\Plugin\WordfilterProcessManager
   */
  $plugin_manager = \Drupal::service('plugin.manager.wordfilter_process');
  $definitions = $plugin_manager
    ->getDefinitions();
  $available_plugins = [];
  foreach ($definitions as $id => $definition) {
    $available_plugins[$id] = $definition['label'];
  }
  $form['process']['process_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Implementation'),
    '#description' => $this
      ->t('The selected implementation will be used to filter the specified words.'),
    '#options' => $available_plugins,
    '#default_value' => $wordfilter_configuration
      ->get('process_id'),
    '#required' => FALSE,
    '#weight' => 10,
    '#ajax' => [
      'event' => 'change',
      'callback' => [
        $this,
        'wordfilterProcessSettingsAjax',
      ],
      'wrapper' => 'wordfilter-process-settings',
      'progress' => [
        'type' => 'throbber',
        'message' => t('Loading settings...'),
      ],
    ],
  ];
  $this
    ->wordfilterProcessSettings($form, $form_state);
  $items = $wordfilter_configuration
    ->getItems();
  $form['items'] = [
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#title' => t('Filtering items'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => 40,
  ];
  foreach ($items as $item) {
    $this
      ->wordfilterItemSettings($form, $form_state, $item);
  }
  $num_items = count($items);
  $form['items']['add_destination'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'wordfilter-item-add-destination',
    ],
    '#weight' => $num_items * 100 - 10,
  ];
  $form['items']['add'] = [
    '#tree' => FALSE,
    '#type' => 'submit',
    '#name' => 'item_add',
    '#value' => t('Add another item'),
    '#weight' => $num_items * 100,
    '#ajax' => [
      'callback' => [
        $this,
        'wordfilterNewItemSettingsAjax',
      ],
      'wrapper' => 'wordfilter-item-add-destination',
      'effect' => 'fade',
      'method' => 'before',
      'progress' => [
        'type' => 'throbber',
        'message' => t('Loading settings...'),
      ],
    ],
    '#submit' => [
      '::wordfilterNewItemSettingsAjax',
    ],
  ];
  return $form;
}