Skip to content
Snippets Groups Projects
LayoutBuilderIdsConfigureSection.php 5.35 KiB
Newer Older
Eric Bremner's avatar
Eric Bremner committed
<?php

namespace Drupal\layout_builder_ids\EventSubscriber;

Liam Morland's avatar
Liam Morland committed
use Drupal\Component\Utility\Html;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
Liam Morland's avatar
Liam Morland committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\core_event_dispatcher\FormHookEvents;
use Drupal\layout_builder_ids\Service\LayoutBuilderIdsService;
Eric Bremner's avatar
Eric Bremner committed
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Eric Bremner's avatar
Eric Bremner committed
 * Add section id to layout builder sections.
Eric Bremner's avatar
Eric Bremner committed
 */
class LayoutBuilderIdsConfigureSection implements EventSubscriberInterface {

  /**
   * Layout builder ids service.
   *
   * @var \Drupal\layout_builder_ids\Service\LayoutBuilderIdsService
   */
  protected $layoutBuilderIdsService;

  /**
   * Constructor for event subscriber for configure block.
   *
   * @param \Drupal\layout_builder_ids\Service\LayoutBuilderIdsService $layoutBuilderIdsService
   *   Layout builder ids service.
   */
  public function __construct(LayoutBuilderIdsService $layoutBuilderIdsService) {
    $this->layoutBuilderIdsService = $layoutBuilderIdsService;
  }

Eric Bremner's avatar
Eric Bremner committed
  /**
   * Alter form.
   *
   * @param \Drupal\core_event_dispatcher\Event\Form\FormAlterEvent $event
Eric Bremner's avatar
Eric Bremner committed
   *   The event.
   */
  public static function alterForm(FormAlterEvent $event): void {
    // Ignore form alter according to module configuration.
    $config = \Drupal::config('layout_builder_ids.settings');
    if (!$config->get('section_id')) {
      return;
    }
Eric Bremner's avatar
Eric Bremner committed

    // 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']);

Eric Bremner's avatar
Eric Bremner committed
      // Get the config for the section.
Eric Bremner's avatar
Eric Bremner committed
      $config = $event->getFormState()->getFormObject()->getLayout()->getConfiguration();
Eric Bremner's avatar
Eric Bremner committed

      // Add the section id to the configure form.
      $form['layout_settings']['layout_builder_id'] = [
        '#type' => 'textfield',
        '#title' => 'Section ID',
        '#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, hyphens, and should be unique on the page (underscores will be replaced by hyphens, and periods will be removed from the rendered ID).'),
Eric Bremner's avatar
Eric Bremner committed
      ];

      // 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'
  public static function layoutBuilderIdsConfigureSectionFormValidation(array &$form, FormStateInterface $form_state) {
    // Get the layout builder id from the form.
    $layout_builder_id = $form_state->getValue(
      [
        'layout_settings',
        'layout_builder_id',
      ]
    // Ensure that we are not checking for blank layout builder id.
    if ($layout_builder_id !== '') {

      // Put the id through the HTML getId to make sure
      // that we form a valid id.
      $layout_builder_id = Html::getId($layout_builder_id);

      // Check if we have a duplicate id somewhere.
      $found_id = layoutBuilderIdsService::layoutBuilderIdsCheckIds($layout_builder_id, $form_state, 'section');
      // If we have a duplicate id, then set the form error.
      if ($found_id) {
        // Set the form error on the layout builder id form element.
        $form_state->setError($form['layout_settings']['layout_builder_id'], 'There is already a block or section with the ID "' . $layout_builder_id . '".');
      }
Eric Bremner's avatar
Eric Bremner committed
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function layoutBuilderIdsConfigureSectionSubmitForm(array &$form, FormStateInterface $form_state) {
Eric Bremner's avatar
Eric Bremner committed

    // Get the layout builder id from the form.
Eric Bremner's avatar
Eric Bremner committed
    $layout_builder_id = $form_state->getValue(
      [
        'layout_settings',
        'layout_builder_id',
      ],
      NULL
    );
Eric Bremner's avatar
Eric Bremner committed

    // If there is a layout builder id, store it.
    if ($layout_builder_id !== NULL) {

      // Get the form object from the form state.
      $formObject = $form_state->getFormObject();

Eric Bremner's avatar
Eric Bremner committed
      // Get the layout.
Eric Bremner's avatar
Eric Bremner committed

      // Load in the config for this section.
      $configuration = $layout->getConfiguration();

      // Set the layout builder id in config variable.
      $configuration['layout_builder_id'] = Html::getId($layout_builder_id);

      // Set the config for this section.
      $layout->setConfiguration($configuration);
    }

  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
Eric Bremner's avatar
Eric Bremner committed
    ];
  }
Eric Bremner's avatar
Eric Bremner committed

Eric Bremner's avatar
Eric Bremner committed
}