You are here

class LayoutBuilderIdsConfigureBlock 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

Class LayoutBuilderIdsConfigureBlock.

Hierarchy

  • class \Drupal\layout_builder_ids\EventSubscriber\LayoutBuilderIdsConfigureBlock implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LayoutBuilderIdsConfigureBlock

1 string reference to 'LayoutBuilderIdsConfigureBlock'
layout_builder_ids.services.yml in ./layout_builder_ids.services.yml
layout_builder_ids.services.yml
1 service uses LayoutBuilderIdsConfigureBlock
layout_builder_ids.configure_block_form in ./layout_builder_ids.services.yml
\Drupal\layout_builder_ids\EventSubscriber\LayoutBuilderIdsConfigureBlock

File

src/EventSubscriber/LayoutBuilderIdsConfigureBlock.php, line 14

Namespace

Drupal\layout_builder_ids\EventSubscriber
View source
class LayoutBuilderIdsConfigureBlock implements EventSubscriberInterface {

  /**
   * Alter form.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Form\FormAlterEvent $event
   *   The event.
   */
  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',
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function LayoutBuilderIdsSubmitForm(array &$form, FormStateInterface $form_state) {

    // Load in the layout_builder_id.
    $layout_builder_id = $form_state
      ->getValue([
      'settings',
      'layout_builder_id',
    ]);

    // If there is in id, save it in config.
    if ($layout_builder_id !== NULL) {

      // Load in the component/block.
      $component = $form_state
        ->getFormObject()
        ->getCurrentComponent();

      // Set the layout_builder_id.
      $component
        ->set('layout_builder_id', Html::getId($layout_builder_id));
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
    ];
  }

}

Members