You are here

public function FilterMarkdown::processSubform in Markdown 8.2

Process callback for constructing markdown settings for this filter.

Parameters

array $element: The element being processed.

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

array $complete_form: The complete form, passed by reference.

Return value

array The processed element.

File

src/Plugin/Filter/FilterMarkdown.php, line 301

Class

FilterMarkdown
Provides a filter for Markdown.

Namespace

Drupal\markdown\Plugin\Filter

Code

public function processSubform(array $element, FormStateInterface $form_state, array &$complete_form) {

  // Create a subform state.
  $subform_state = SubformState::createForSubform($element, $complete_form, $form_state);

  // If the triggering element is the parser select element, clear out any
  // parser values other than the identifier. This is necessary since the
  // parser has switched and the previous parser settings may not translate
  // correctly to the new parser.
  if (($trigger = $form_state
    ->getTriggeringElement()) && isset($trigger['#ajax']['callback']) && $trigger['#ajax']['callback'] === '\\Drupal\\markdown\\Plugin\\Filter\\FilterMarkdown::ajaxChangeParser' && ($parserId = $subform_state
    ->getValue('id'))) {
    $parents = $subform_state
      ->createParents();
    $input =& NestedArray::getValue($form_state
      ->getUserInput(), $parents);
    $values =& NestedArray::getValue($form_state
      ->getValues(), $parents);
    if ($trigger['#type'] === 'select') {
      $input = [
        'id' => $parserId,
        'override' => (int) (!empty($input['override'])),
      ];
      $values = [
        'id' => $parserId,
        'override' => (int) (!empty($values['override'])),
      ];
    }
    $configuration = $this
      ->getConfiguration();
    $configuration['settings'] = $values;
    $this
      ->setConfiguration($configuration);
  }
  $parser = $this
    ->getParser();
  $parserId = $parser
    ->getPluginId();

  // Attempt to build the parser form, catch any exceptions.
  try {
    $element = ParserConfigurationForm::create()
      ->setFilter($this)
      ->setParser($parser)
      ->processSubform($element, $form_state, $complete_form);
  } catch (\Exception $exception) {

    // Intentionally left blank.
  } catch (\Throwable $exception) {

    // Intentionally left blank.
  }
  if (isset($exception)) {
    $element['error'] = static::createInlineMessage([
      'error' => [
        Markup::create($exception
          ->getMessage()),
      ],
    ]);
  }
  $markdownAjaxId = $form_state
    ->get('markdownAjaxId');

  // Add enabled parsers.
  $labels = [];
  foreach ($this->parserManager
    ->enabled() as $name => $enabledParser) {
    $labels[$name] = $enabledParser
      ->getLabel(TRUE);
  }

  // Check if parser exists and, if not, append an option showing it missing.
  if (!$this->parserManager
    ->hasDefinition($parserId)) {
    $labels[$parserId] = new FormattableMarkup('@label (missing)', [
      '@label' => $parserId,
    ]);
  }
  $parserElement =& $element['parser'];
  $parserElement['status'] = $parser
    ->buildStatus();
  $parserElement['status']['#weight'] = -10;
  $parserElement['id'] = static::createElement([
    '#weight' => -9,
    '#type' => 'select',
    '#options' => $labels,
    '#default_value' => $parserId,
    '#description' => $parser
      ->getDescription(),
    '#attributes' => [
      'data' => [
        'markdownSummary' => 'parser',
        'markdownId' => $parserId,
        'markdownInstalled' => $parser
          ->isInstalled(),
      ],
    ],
    '#ajax' => [
      'callback' => '\\Drupal\\markdown\\Plugin\\Filter\\FilterMarkdown::ajaxChangeParser',
      'event' => 'change',
      'wrapper' => $markdownAjaxId,
    ],
  ]);
  $override = !empty($this
    ->getConfiguration()['settings']['override']);
  $parserElement['override'] = static::createElement([
    '#weight' => -8,
    '#type' => 'radios',
    '#options' => [
      0 => $this
        ->t('Inherit site-wide settings'),
      1 => $this
        ->t('Override site-wide settings'),
    ],
    '#default_value' => (int) $override,
    '#ajax' => [
      'callback' => '\\Drupal\\markdown\\Plugin\\Filter\\FilterMarkdown::ajaxChangeParser',
      'event' => 'change',
      'wrapper' => $markdownAjaxId,
    ],
  ]);
  if (($markdownParserEditUrl = Url::fromRoute('markdown.parser.edit', [
    'parser' => $parser,
  ])) && $markdownParserEditUrl
    ->access()) {
    $parserElement['override']['#description'] = $this
      ->t('Site-wide markdown settings can be adjusted by visiting the site-wide <a href=":markdown.parser.edit" target="_blank">@label</a> parser.', [
      '@label' => $parser
        ->getLabel(FALSE),
      ':markdown.parser.edit' => $markdownParserEditUrl
        ->toString(),
    ]);
  }
  else {
    $parserElement['override']['#description'] = $this
      ->t('Site-wide markdown settings can only be adjusted by administrators.');
  }
  if (!$override) {
    $parserElement['settings']['#access'] = FALSE;
    $parserElement['extensions']['#access'] = FALSE;
  }

  // Append a "more info" link to the parser in the description.
  if ($url = $parser
    ->getUrl()) {
    $parserElement['id']['#description'] = $this
      ->moreInfo($parserElement['id']['#description'], $url);
  }
  return $element;
}