You are here

public function TableOfContentsExtension::buildConfigurationForm in Markdown 8.2

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/Markdown/CommonMark/Extension/TableOfContentsExtension.php, line 90

Class

TableOfContentsExtension
Plugin annotation @MarkdownExtension( id = "commonmark-table-of-contents", label = @Translation("Table Of Contents"), description = @Translation("Automatically inserts a table of contents into your document with links to the various…

Namespace

Drupal\markdown\Plugin\Markdown\CommonMark\Extension

Code

public function buildConfigurationForm(array $element, FormStateInterface $form_state) {

  /** @var \Drupal\markdown\Form\SubformStateInterface $form_state */
  $element += $this
    ->createSettingElement('html_class', [
    '#type' => 'textfield',
    '#title' => $this
      ->t('HTML Class'),
    '#description' => $this
      ->t("Sets the <code>&lt;ul&gt;</code> or <code>&lt;ol&gt;</code> tag's class attribute."),
  ], $form_state);
  $headings = array_combine(range(1, 6), array_map(function ($value) {
    return "h{$value}";
  }, range(1, 6)));
  $element += $this
    ->createSettingElement('min_heading_level', [
    '#type' => 'select',
    '#title' => $this
      ->t('Minimum Heading Level'),
    '#description' => $this
      ->t('Headings larger than this will be ignored, e.g. if set to <code>h2</code> then <code>h1</code> headings will be ignored.'),
    '#options' => $headings,
  ], $form_state);
  $element += $this
    ->createSettingElement('max_heading_level', [
    '#type' => 'select',
    '#title' => $this
      ->t('Maximum Heading Level'),
    '#description' => $this
      ->t('Headings smaller than this will be ignored, e.g. if set to <code>h5</code> then <code>h6</code> headings will be ignored.'),
    '#options' => $headings,
  ], $form_state);
  $element += $this
    ->createSettingElement('normalize', [
    '#type' => 'select',
    '#title' => $this
      ->t('Normalize'),
    '#description' => $this
      ->t('Strategy used when generating a (potentially-nested) list of headings.'),
    '#options' => [
      'as-is' => $this
        ->t('As Is'),
      'flat' => $this
        ->t('Flat'),
      'relative' => $this
        ->t('Relative'),
    ],
  ], $form_state);
  $positions = [
    'top' => $this
      ->t('Top'),
    'before-headings' => $this
      ->t('Before Headings'),
  ];
  if (static::featureExists('placeholder')) {
    $positions = [
      'placeholder' => $this
        ->t('Placeholder'),
    ] + $positions;
  }
  $element += $this
    ->createSettingElement('position', [
    '#type' => 'select',
    '#title' => $this
      ->t('Position'),
    '#description' => $this
      ->t('Where to place table of contents.'),
    '#options' => $positions,
  ], $form_state);
  $element += $this
    ->createSettingElement('placeholder', [
    '#access' => static::featureExists('placeholder'),
    '#title' => $this
      ->t('Placeholder'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The placeholder value that will be replaced with the Table of Contents. Any lines in your document that match this placeholder value will be replaced by the Table of Contents.'),
  ], $form_state);
  $form_state
    ->addElementState($element['placeholder'], 'visible', 'position', [
    'value' => 'placeholder',
  ]);
  $element += $this
    ->createSettingElement('style', [
    '#type' => 'select',
    '#title' => $this
      ->t('Style'),
    '#description' => $this
      ->t('HTML list style type to use when rendering the table of contents.'),
    '#options' => [
      'bullet' => $this
        ->t('Unordered (Bulleted)'),
      'ordered' => $this
        ->t('Ordered (Numbered)'),
    ],
  ], $form_state);
  return $element;
}