LayoutBuilderIdsConfigureBlock.php in Layout builder ids 8
File
src/EventSubscriber/LayoutBuilderIdsConfigureBlock.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 Drupal\Component\Utility\Html;
class LayoutBuilderIdsConfigureBlock implements EventSubscriberInterface {
public function alterForm(FormAlterEvent $event) {
$form =& $event
->getForm();
if (in_array($form['#form_id'], [
'layout_builder_add_block',
'layout_builder_update_block',
], TRUE)) {
$layout_builder_id =& $event
->getFormState()
->getFormObject()
->getCurrentComponent()
->get('layout_builder_id');
$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.'),
];
array_unshift($form['#submit'], [
$this,
'LayoutBuilderIdsSubmitForm',
]);
}
}
public function LayoutBuilderIdsSubmitForm(array &$form, FormStateInterface $form_state) {
$layout_builder_id = $form_state
->getValue([
'settings',
'layout_builder_id',
]);
if ($layout_builder_id !== NULL) {
$component = $form_state
->getFormObject()
->getCurrentComponent();
$component
->set('layout_builder_id', Html::getId($layout_builder_id));
}
}
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
];
}
}