LayoutBuilderIdsConfigureSection.php in Layout builder ids 8
File
src/EventSubscriber/LayoutBuilderIdsConfigureSection.php
View source
<?php
namespace Drupal\layout_builder_ids\EventSubscriber;
use Drupal\Core\Form\FormStateInterface;
use Drupal\hook_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
use Drupal\Component\Utility\Html;
class LayoutBuilderIdsConfigureSection implements EventSubscriberInterface {
public function alterForm(FormAlterEvent $event) {
$form =& $event
->getForm();
if ($form['#form_id'] == 'layout_builder_configure_section') {
$config = $event
->getFormState()
->getFormObject()
->getLayout()
->getConfiguration();
$form['layout_settings']['layout_builder_id'] = [
'#type' => 'textfield',
'#title' => 'Section ID',
'#weight' => 0,
'#default_value' => $config['layout_builder_id'] ?: NULL,
'#description' => t('Enter an ID for the section. IDs can contain letters, numbers, underscore, hyphen and period characters, and should start with a letter.'),
];
array_unshift($form['#submit'], [
$this,
'LayoutBuilderIdsSubmitForm',
]);
}
}
public function LayoutBuilderIdsSubmitForm(array &$form, FormStateInterface $form_state) {
$layout_builder_id = $form_state
->getValue([
'layout_settings',
'layout_builder_id',
], NULL);
if ($layout_builder_id !== NULL) {
$layout = $this
->getLayout($form_state);
$configuration = $layout
->getConfiguration();
$configuration['layout_builder_id'] = Html::getId($layout_builder_id);
$layout
->setConfiguration($configuration);
}
}
private function getLayout(FormStateInterface $form_state) {
$formObject = $form_state
->getFormObject();
return $formObject
->getLayout();
}
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
];
}
}