Skip to content
Snippets Groups Projects
Commit c392d9e7 authored by Eric Bremner's avatar Eric Bremner
Browse files

Issue #3173862: checking for duplicate ids on blocks only, still have to work on sections

parent eae8e542
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ services:
- { name: 'event_subscriber' }
layout_builder_ids.configure_block_form:
class: '\Drupal\layout_builder_ids\EventSubscriber\LayoutBuilderIdsConfigureBlock'
arguments: ['@entity_type.manager', '@current_route_match']
tags:
- { name: 'event_subscriber' }
layout_builder_ids.route_subscriber:
......
......@@ -2,7 +2,9 @@
namespace Drupal\layout_builder_ids\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -13,6 +15,33 @@ use Drupal\Component\Utility\Html;
*/
class LayoutBuilderIdsConfigureBlock implements EventSubscriberInterface {
/**
* Entity type manager from core.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The variable for route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructor for event subscriber for configure block.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Route match service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, RouteMatchInterface $routeMatch) {
$this->entityTypeManager = $entityTypeManager;
$this->routeMatch = $routeMatch;
}
/**
* Alter form.
*
......@@ -45,11 +74,93 @@ class LayoutBuilderIdsConfigureBlock implements EventSubscriberInterface {
'#description' => t('Block 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 block 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'][] = [$this, 'layoutBuilderIdsFormValidation'];
// Add our custom submit function.
array_unshift($form['#submit'], [$this, 'layoutBuilderIdsSubmitForm']);
}
}
/**
* {@inheritdoc}
*/
public function layoutBuilderIdsFormValidation(array &$form, FormStateInterface $form_state) {
// Get the layout builder id from the form state.
$layout_builder_id = $form_state->getValue(['settings', 'layout_builder_id']);
// If there is a block id specified, check that there are
// no duplicate ids.
if ($layout_builder_id !== '') {
// Get the current node from the route match, it is in
// the form node.<nid>.
$nid = $this->routeMatch->getRawParameters()->get('section_storage');
// If there is an nid, load the node.
if (isset($nid)) {
// Get the nid which is currently in the format node.<nid>.
$nid = (integer) str_replace('node.', '', $nid);
// Load the node.
$node = $this->entityTypeManager->getStorage('node')->load($nid);
}
// Get the layout from the node.
$layout = $node->layout_builder__layout;
// A boolean variable to check if we have found a duplicate id.
$found_id = FALSE;
// Step through each section and check for a duplicate.
foreach ($layout as $section) {
// Get the components of each section.
$components = $section->section->getComponents();
// Step through each of the components.
foreach ($components as $component) {
// Get the additional settings from the component.
$additional = $component->get('additional');
// If the additional settings are set, check if
// specified id is a duplicate.
if (isset($additional['layout_builder_id'])) {
// If there is already an id with the one specified,
// set the flag.
if ($additional['layout_builder_id'] == $layout_builder_id) {
// Set the flag that we have found a duplicate id.
$found_id = TRUE;
// Break from the loop to save computational time.
break;
}
}
// If we have already found a duplicate id, break from
// the outer loop to save computational time.
if ($found_id) {
break;
}
}
}
}
// @todo Have to look through all the sections and check
// for duplicate ids as well. This only does blocks.
// If we have found a duplicate id, set the form error.
if ($found_id) {
// Set the form error on the block ID element.
$form_state->setError($form['settings']['layout_builder_id'], 'There is already a block or section with the ID "' . $layout_builder_id . '".');
}
}
/**
* {@inheritdoc}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment