You are here

public function Shortcode::settingsForm in Shortcode 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/Shortcode.php \Drupal\shortcode\Plugin\Filter\Shortcode::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/Shortcode.php, line 25

Class

Shortcode
Provides a filter for insert view.

Namespace

Drupal\shortcode\Plugin\Filter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\shortcode\ShortcodeService $shortcodeService */
  $shortcodeService = \Drupal::service('shortcode');
  $shortcodes = $shortcodeService
    ->loadShortcodePlugins();
  $shortcodes_by_provider = [];

  // Group shortcodes by provider.
  foreach ($shortcodes as $shortcode_id => $shortcode_info) {
    $provider_id = $shortcode_info['provider'];
    if (!isset($shortcodes_by_provider[$provider_id])) {
      $shortcodes_by_provider[$provider_id] = [];
    }
    $shortcodes_by_provider[$provider_id][$shortcode_id] = $shortcode_info;
  }

  // Generate form elements.
  $settings = [];
  foreach ($shortcodes_by_provider as $provider_id => $shortcodes) {

    // Add section header.
    // todo: add Translate markup.
    $settings['header-' . $provider_id] = [
      '#markup' => '<b class="shortcodeSectionHeader">Shortcodes provided by ' . $provider_id . '</b>',
    ];

    // Sort definitions by weight property.
    $sorted_shortcodes = $shortcodes;
    uasort($sorted_shortcodes, function ($a, $b) {
      return $b['weight'] - $a['weight'];
    });

    /** @var \Drupal\shortcode\Plugin\ShortcodeInterface $shortcode */
    foreach ($sorted_shortcodes as $shortcode_id => $shortcode_info) {
      $settings[$shortcode_id] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable %name shortcode', [
          '%name' => $shortcode_info['title'],
        ]),
        '#default_value' => isset($this->settings[$shortcode_id]) ? $this->settings[$shortcode_id] : TRUE,
        '#description' => isset($shortcode_info['description']) ? $shortcode_info['description'] : $this
          ->t('Enable or disable this shortcode in this input format'),
      ];
    }
  }
  return $settings;
}