You are here

public function Markdown::settingsForm in Markdown 3.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/Markdown.php \Drupal\markdown\Plugin\Filter\Markdown::settingsForm()

@todo Refactor before release.

Overrides FilterBase::settingsForm

File

src/Plugin/Filter/Markdown.php, line 109

Class

Markdown
Provides a filter for Markdown.

Namespace

Drupal\markdown\Plugin\Filter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $parents = $form['#parents'];
  $defaultParser = $form_state
    ->getValue(array_merge($parents, [
    'parser',
  ]), $this
    ->getParser()
    ->getPluginId());
  if ($labels = $this->parserManager
    ->getLabels()) {
    $id = Html::getUniqueId('markdown-parser-ajax');

    // Build a wrapper for the ajax response.
    $form['ajax'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => $id,
      ],
      '#parents' => $parents,
    ];
    $form['ajax']['parser'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Parser'),
      '#options' => $labels,
      '#default_value' => $defaultParser,
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxChangeParser',
        ],
        'event' => 'change',
        'wrapper' => $id,
      ],
    ];
  }
  else {
    $form['ajax']['parser'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('No Markdown Parsers Found'),
      '#description' => $this
        ->t('Visit the <a href=":system.status">@system.status</a> page for more details.', [
        '@system.status' => $this
          ->t('Status report'),
        ':system.status' => \Drupal::urlGenerator()
          ->generate('system.status'),
      ]),
    ];
  }
  if ($defaultParser && ($parser = $this->parserManager
    ->createInstance($defaultParser, [
    'filter' => $this,
  ])) && $parser instanceof ExtensibleMarkdownParserInterface && ($extensions = $parser
    ->getExtensions())) {

    // @todo Add parser specific settings.
    $form['ajax']['parser_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Extensions'),
    ];

    // Add any specific extension settings.
    $form['ajax']['parser_settings']['extensions'] = [
      '#type' => 'container',
    ];
    foreach ($extensions as $pluginId => $extension) {

      // Extension Details.
      $form['ajax']['parser_settings']['extensions'][$pluginId] = [
        '#type' => 'details',
        '#title' => ($url = $extension
          ->getUrl()) ? Link::fromTextAndUrl($extension
          ->getLabel(), $url) : $extension
          ->getLabel(),
        '#description' => $extension
          ->getDescription(),
        '#open' => $extension
          ->isEnabled(),
        '#array_parents' => array_merge($parents, [
          'parser_settings',
          'extensions',
        ]),
      ];

      // Extension enabled checkbox.
      $form['ajax']['parser_settings']['extensions'][$pluginId]['enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enabled'),
        '#default_value' => $extension
          ->isEnabled(),
      ];

      // Extension settings.
      $selector = $this
        ->getSatesSelector(array_merge($parents, [
        'parser_settings',
        'extensions',
        $pluginId,
      ]), 'enabled');
      $form['ajax']['parser_settings']['extensions'][$pluginId]['settings'] = [
        '#type' => 'container',
        '#states' => [
          'visible' => [
            $selector => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['ajax']['parser_settings']['extensions'][$pluginId]['settings'] = $extension
        ->settingsForm($form['ajax']['parser_settings']['extensions'][$pluginId]['settings'], $form_state, $this);
    }
  }
  return $form;
}