You are here

protected function ParserConfigurationForm::buildParserExtensions in Markdown 8.2

Builds the extension settings for a specific parser.

Parameters

\Drupal\markdown\Plugin\Markdown\ParserInterface $parser: The parser.

array $element: An element in a render array.

\Drupal\markdown\Form\SubformStateInterface $form_state: The form state.

Return value

array The $element passed, modified to include the parser extension elements.

1 call to ParserConfigurationForm::buildParserExtensions()
ParserConfigurationForm::buildParser in src/Form/ParserConfigurationForm.php
Builds the parser form elements.

File

src/Form/ParserConfigurationForm.php, line 360

Class

ParserConfigurationForm
Form for modifying parser configuration.

Namespace

Drupal\markdown\Form

Code

protected function buildParserExtensions(ParserInterface $parser, array $element, SubformStateInterface $form_state) {

  // Immediately return if parser isn't extensible.
  if (!$parser instanceof ExtensibleParserInterface) {
    return $element;
  }
  $markdownGroup = $form_state
    ->get('markdownGroup');
  $extensions = $parser
    ->extensions();
  if (!$extensions) {
    return $element;
  }
  $parents = $element['#parents'];
  $element['extensions'] = [
    '#type' => 'container',
  ];

  // Add any specific extension settings.
  foreach ($extensions as $extensionId => $extension) {
    $label = $extension
      ->getLabel(FALSE);
    $url = $extension
      ->getUrl();
    $element['extensions'][$extensionId] = [
      '#type' => 'details',
      '#title' => $label,
      '#group' => $markdownGroup,
      '#parents' => array_merge($parents, [
        'extensions',
        $extensionId,
      ]),
    ];

    /* @var array $extensionElement */
    $extensionElement =& $element['extensions'][$extensionId];
    $extensionSubform = SubformState::createForSubform($extensionElement, $element, $form_state);
    $bundled = in_array($extensionId, $parser
      ->getBundledExtensionIds(), TRUE);
    $installed = $extension
      ->isInstalled();
    $enabled = $extensionSubform
      ->getValue('enabled', $extension
      ->isEnabled());
    if ($experimental = $extension
      ->getExperimental()) {
      $extensionElement['experimental'] = static::createInlineMessage([
        'info' => [
          $experimental === TRUE ? $this
            ->t('This is an experimental extension. Not all features or functionality may work.') : $experimental,
        ],
      ]);
    }
    $extensionElement['libraries'] = $extension
      ->buildStatus(!$installed);

    // Extension enabled checkbox.
    $extensionElement['enabled'] = static::createElement([
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable'),
      '#description' => $this
        ->moreInfo($extension
        ->getDescription(), $url),
      '#attributes' => [
        'data' => [
          'markdownElement' => 'extension',
          'markdownSummary' => 'extension',
          'markdownId' => $extensionId,
          'markdownLabel' => $label,
          'markdownInstalled' => $installed,
          'markdownBundle' => $bundled ? $parser
            ->getLabel(FALSE) : FALSE,
          'markdownRequires' => $extension
            ->requires(),
          'markdownRequiredBy' => $extension
            ->requiredBy(),
        ],
      ],
      '#default_value' => $bundled || $enabled,
      '#disabled' => $bundled || !$installed,
    ]);

    // Installed extension settings.
    if ($installed && $extension instanceof PluginFormInterface) {
      $extensionElement['settings'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Settings'),
        '#open' => TRUE,
      ];
      $extensionSettingsElement =& $extensionElement['settings'];
      $extensionSettingsSubform = SubformState::createForSubform($extensionSettingsElement, $extensionElement, $extensionSubform);
      $extensionSubform
        ->addElementState($extensionSettingsElement, 'visible', 'enabled', [
        'checked' => TRUE,
      ]);
      $extensionSettingsElement = $extension
        ->buildConfigurationForm($extensionSettingsElement, $extensionSettingsSubform);
      $extensionSettingsElement['#access'] = !!Element::getVisibleChildren($extensionSettingsElement);
    }
  }

  // Only show extensions if there are extensions.
  $element['extensions']['#access'] = !!Element::getVisibleChildren($element['extensions']);
  return $element;
}