You are here

public function Freelinking::settingsForm in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Filter/Freelinking.php \Drupal\freelinking\Plugin\Filter\Freelinking::settingsForm()

Generates a filter's settings form.

Parameters

array $form: A minimally prepopulated form array.

\Drupal\Core\Form\FormStateInterface $form_state: The state of the (entire) configuration form.

Return value

array The $form array with additional form elements for the settings of this filter. The submitted form values should match $this->settings.

Overrides FilterBase::settingsForm

File

src/Plugin/Filter/Freelinking.php, line 86

Class

Freelinking
Freelinking input filter plugin.

Namespace

Drupal\freelinking\Plugin\Filter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $plugins = $this->freelinkingManager
    ->getDefinitions();
  $form['default'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Default plugin'),
    '#description' => $this
      ->t('Default plugin to use when no indicator is specified. “Nodetitle” mimics previous versions of Freelinking.'),
    '#default_value' => $this->settings['default'],
    '#required' => TRUE,
  ];
  $form['global_options'] = [
    '#tree' => TRUE,
    'ignore_upi' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Ignore Unknown Plugin Indicators (UPI)'),
      '#description' => $this
        ->t('Choose whether to ignore markup that may look like freelinking or does not have a valid plugin.'),
      '#options' => [
        0 => $this
          ->t('Display an error'),
        1 => $this
          ->t('Ignore indicators'),
      ],
      '#required' => TRUE,
      '#default_value' => $this->settings['global_options']['ignore_upi'],
    ],
  ];
  $form['plugins'] = [
    '#tree' => TRUE,
  ];
  foreach ($plugins as $plugin_name => $plugin_definition) {
    $config = $this
      ->extractPluginSettings($plugin_name, $this->settings['plugins']);
    $plugin_settings = isset($config['settings']) ? $config['settings'] : [];
    $plugin = $this->freelinkingManager
      ->createInstance($plugin_name, [
      'settings' => $plugin_settings,
    ]);
    $form['plugins'][$plugin_name] = [
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#title' => $plugin_definition['title'],
      'plugin' => [
        '#type' => 'value',
        '#value' => $plugin_name,
      ],
      'enabled' => [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable'),
        '#default_value' => isset($config['enabled']) ? $config['enabled'] : FALSE,
      ],
      'settings' => [
        '#tree' => TRUE,
        '#type' => 'container',
      ] + $plugin
        ->settingsForm($form, $form_state),
    ];

    // Hide the enabled checkbox if the plugin is always enabled.
    if ($plugin
      ->isHidden()) {
      $form['plugins'][$plugin_name]['enabled']['#disabled'] = TRUE;
      $form['plugins'][$plugin_name]['enabled']['#default_value'] = TRUE;
    }
  }
  return $form;
}