You are here

public function ParserConfigurationForm::processSubform in Markdown 8.2

Process callback for constructing markdown settings for a parser.

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.

Throws

\Drupal\Core\Form\EnforcedResponseException When an invalid parser or no parser is provided.

File

src/Form/ParserConfigurationForm.php, line 172

Class

ParserConfigurationForm
Form for modifying parser configuration.

Namespace

Drupal\markdown\Form

Code

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

  // Keep track of subform parents for the validation and submit handlers.
  $form_state
    ->set('markdownSubformParents', $parents = isset($element['#parents']) ? $element['#parents'] : []);
  $form_state
    ->set('markdownSubformArrayParents', $element['#array_parents']);

  // Add the markdown/admin library to update summaries in vertical tabs.
  $element['#attached']['library'][] = 'markdown/admin';

  // Check for installed parsers.
  if (!$this->parserManager
    ->installedDefinitions()) {
    $error = $this
      ->t('No markdown parsers installed.');
  }
  elseif (!($parser = $this
    ->getParser())) {
    $error = $this
      ->t('No markdown parser has been set. Unable to create the parser form.');
  }
  elseif ($parser
    ->getPluginId() === $this->parserManager
    ->getFallbackPluginId()) {
    $error = $this
      ->t('Unknown parser: %parser_id.', [
      '%parser_id' => $parser
        ->getOriginalPluginId(),
    ]);
  }

  // Add #validate and #submit handlers. These help validate and submit
  // the various markdown plugin forms for parsers and extensions.
  if ($validationHandlers = $form_state
    ->getValidateHandlers()) {
    if (!in_array([
      $this,
      'validateSubform',
    ], $validationHandlers)) {
      array_unshift($validationHandlers, [
        $this,
        'validateSubform',
      ]);
      $form_state
        ->setValidateHandlers($validationHandlers);
    }
  }
  else {
    $complete_form['#validate'][] = [
      $this,
      'validateSubform',
    ];
  }

  // Build a wrapper for the ajax response.
  $form_state
    ->set('markdownAjaxId', $markdownAjaxId = Html::getUniqueId('markdown-parser-ajax'));
  $element['ajax'] = static::createElement([
    '#type' => 'container',
    '#id' => $markdownAjaxId,
    '#attributes' => [
      'data' => [
        'markdownElement' => 'wrapper',
      ],
    ],
  ]);

  // Build vertical tabs that parser and extensions will go into.
  $element['ajax']['vertical_tabs'] = [
    '#type' => 'vertical_tabs',
    '#parents' => array_merge($parents, [
      'vertical_tabs',
    ]),
  ];

  // Determine the group that details should be referencing for vertical tabs.
  $form_state
    ->set('markdownGroup', $group = implode('][', array_merge($parents, [
    'vertical_tabs',
  ])));

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

  // Build the parser form.
  $element = $this
    ->buildParser($element, $subform_state);
  if (isset($error)) {
    if (($markdownOverview = Url::fromRoute('markdown.overview', [], [
      'absolute' => TRUE,
    ])) && $markdownOverview
      ->access()) {
      $error = $this
        ->t('@error Visit the <a href=":markdown.overview" target="_blank">Markdown Overview</a> page for more details.', [
        '@error' => $error,
        ':markdown.overview' => $markdownOverview
          ->toString(),
      ]);
    }
    else {
      $error = $this
        ->t('@error Ask your site administrator to install a <a href=":supported_parsers" target="_blank">supported markdown parser</a>.', [
        '@error' => $error,
        ':supported_parsers' => Markdown::DOCUMENTATION_URL . '/parsers',
      ]);
    }

    // If there's no filter associated, show the error after the redirect.
    if (!$this
      ->getFilter()) {
      $this
        ->messenger()
        ->addError($error);
    }
    throw new EnforcedResponseException($this
      ->redirect('markdown.overview'), $error);
  }
  return $element;
}