You are here

public static function LayoutBuilderIdsConfigureSection::alterForm in Layout builder ids 2.0.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/LayoutBuilderIdsConfigureSection.php \Drupal\layout_builder_ids\EventSubscriber\LayoutBuilderIdsConfigureSection::alterForm()

Alter form.

Parameters

\Drupal\core_event_dispatcher\Event\Form\FormAlterEvent $event: The event.

File

src/EventSubscriber/LayoutBuilderIdsConfigureSection.php, line 40

Class

LayoutBuilderIdsConfigureSection
Add section id to layout builder sections.

Namespace

Drupal\layout_builder_ids\EventSubscriber

Code

public static function alterForm(FormAlterEvent $event) : void {

  // Get the form from the event.
  $form =& $event
    ->getForm();

  // If we are on a configure section form, alter it.
  if ($form['#form_id'] == 'layout_builder_configure_section') {

    // These next two lines are needed until this issue gets fixed:
    // https://www.drupal.org/project/drupal/issues/3103812.
    // Once this issue gets fixed in core then we can use the
    // proper validate procedures.  Until then we need to add the
    // form id without the random value.
    $form_state = $event
      ->getFormState();
    $form['#id'] = Html::getId($form_state
      ->getBuildInfo()['form_id']);

    // Get the config for the section.
    $config = $event
      ->getFormState()
      ->getFormObject()
      ->getLayout()
      ->getConfiguration();

    // Add the section id to the configure form.
    $form['layout_settings']['layout_builder_id'] = [
      '#type' => 'textfield',
      '#title' => 'Section ID',
      '#weight' => 99,
      '#default_value' => $config['layout_builder_id'] ?? NULL,
      '#description' => t('Section ID is an optional setting which is used to support an anchor link to this block. For example, entering "feature" lets you link directly to this section by adding "#feature" to the end of the URL.</br>IDs should start with a letter, may only contain letters, numbers, underscores, hyphens, and periods, and should be unique on the page.'),
    ];

    // Add the form validation for configure block.
    $form['#validate'][] = 'Drupal\\layout_builder_ids\\EventSubscriber\\LayoutBuilderIdsConfigureSection::layoutBuilderIdsConfigureSectionFormValidation';

    // Add our custom submit function.
    array_unshift($form['#submit'], 'Drupal\\layout_builder_ids\\EventSubscriber\\LayoutBuilderIdsConfigureSection::layoutBuilderIdsConfigureSectionSubmitForm');
  }
}