LayoutBuilderIdsConfigureSection.php in Layout builder ids 2.0.x
File
src/EventSubscriber/LayoutBuilderIdsConfigureSection.php
View source
<?php
namespace Drupal\layout_builder_ids\EventSubscriber;
use Drupal\Component\Utility\Html;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\Core\Form\FormStateInterface;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\layout_builder_ids\Service\LayoutBuilderIdsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LayoutBuilderIdsConfigureSection implements EventSubscriberInterface {
protected $layoutBuilderIdsService;
public function __construct(LayoutBuilderIdsService $layoutBuilderIdsService) {
$this->layoutBuilderIdsService = $layoutBuilderIdsService;
}
public static function alterForm(FormAlterEvent $event) : void {
$form =& $event
->getForm();
if ($form['#form_id'] == 'layout_builder_configure_section') {
$form_state = $event
->getFormState();
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
$config = $event
->getFormState()
->getFormObject()
->getLayout()
->getConfiguration();
$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.'),
];
$form['#validate'][] = 'Drupal\\layout_builder_ids\\EventSubscriber\\LayoutBuilderIdsConfigureSection::layoutBuilderIdsConfigureSectionFormValidation';
array_unshift($form['#submit'], 'Drupal\\layout_builder_ids\\EventSubscriber\\LayoutBuilderIdsConfigureSection::layoutBuilderIdsConfigureSectionSubmitForm');
}
}
public static function layoutBuilderIdsConfigureSectionFormValidation(array &$form, FormStateInterface $form_state) {
$layout_builder_id = $form_state
->getValue([
'layout_settings',
'layout_builder_id',
]);
if ($layout_builder_id !== '') {
$layout_builder_id = Html::getId($layout_builder_id);
$found_id = layoutBuilderIdsService::layoutBuilderIdsCheckIds($layout_builder_id, $form_state, 'section');
if ($found_id) {
$form_state
->setError($form['layout_settings']['layout_builder_id'], 'There is already a block or section with the ID "' . $layout_builder_id . '".');
}
}
}
public static function layoutBuilderIdsConfigureSectionSubmitForm(array &$form, FormStateInterface $form_state) {
$layout_builder_id = $form_state
->getValue([
'layout_settings',
'layout_builder_id',
], NULL);
if ($layout_builder_id !== NULL) {
$formObject = $form_state
->getFormObject();
$layout = $formObject
->getLayout();
$configuration = $layout
->getConfiguration();
$configuration['layout_builder_id'] = Html::getId($layout_builder_id);
$layout
->setConfiguration($configuration);
}
}
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
];
}
}