You are here

public function LayoutBuilderIdsConfigureBlock::alterForm in Layout builder ids 8

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

Alter form.

Parameters

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

File

src/EventSubscriber/LayoutBuilderIdsConfigureBlock.php, line 22

Class

LayoutBuilderIdsConfigureBlock
Class LayoutBuilderIdsConfigureBlock.

Namespace

Drupal\layout_builder_ids\EventSubscriber

Code

public function alterForm(FormAlterEvent $event) {

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

  // If we are on a configure section form, alter it.
  if (in_array($form['#form_id'], [
    'layout_builder_add_block',
    'layout_builder_update_block',
  ], TRUE)) {

    // Pull out the layout_builder_id from config.
    $layout_builder_id =& $event
      ->getFormState()
      ->getFormObject()
      ->getCurrentComponent()
      ->get('layout_builder_id');

    // Add the section id to the configure form.
    $form['settings']['layout_builder_id'] = [
      '#type' => 'textfield',
      '#title' => 'Block ID',
      '#weight' => 0,
      '#default_value' => $layout_builder_id ?: NULL,
      '#description' => t('Enter an ID for the block. IDs can contain letters, numbers, underscore, hyphen and period characters, and should start with a letter.'),
    ];

    // Add our custom submit function.
    array_unshift($form['#submit'], [
      $this,
      'LayoutBuilderIdsSubmitForm',
    ]);
  }
}