Newer
Older
<?php
namespace Drupal\layout_builder_ids\EventSubscriber;
use Drupal\Core\Form\FormStateInterface;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\layout_builder_ids\Service\LayoutBuilderIdsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Component\Utility\Html;
/**
*/
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;
}
* @param \Drupal\core_event_dispatcher\Event\Form\FormAlterEvent $event
public 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') {
// 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.'),
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
array_unshift(
$form['#submit'],
[
$this,
'layoutBuilderIdsConfigureSectionSubmitForm',
]
);
// Add the form validation for configure section.
$form['#validate'][] = [
$this,
'layoutBuilderIdsConfigureSectionFormValidation',
];
}
}
/**
* {@inheritdoc}
*/
public function layoutBuilderIdsConfigureSectionFormValidation(array &$form, FormStateInterface $form_state) {
// Get the layout builder id from the form,
// also put the id through the HTML getId to make sure
// that we form a valid id.
$layout_builder_id = Html::getId(
$form_state->getValue(
[
'layout_settings',
'layout_builder_id',
],
NULL
)
);
// Check if we have a duplicate id somewhere.
$found_id = $this->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 . '".');
public function layoutBuilderIdsConfigureSectionSubmitForm(array &$form, FormStateInterface $form_state) {
$layout_builder_id = $form_state->getValue(
[
'layout_settings',
'layout_builder_id',
],
NULL
);
// If there is a layout builder id, store it.
if ($layout_builder_id !== NULL) {
// Get the layout.
$layout = $this->getLayout($form_state);
// 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);
}
}
/**
* Get the layout object.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
private function getLayout(FormStateInterface $form_state) {
// Get the form object.
$formObject = $form_state->getFormObject();
return $formObject->getLayout();
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
];
}