Skip to content
Snippets Groups Projects
Commit 95371724 authored by Sean Adams-Hiett's avatar Sean Adams-Hiett
Browse files

Added logic to respect layout builder restrictions, if enabled. A little bit...

Added logic to respect layout builder restrictions, if enabled. A little bit of code cleanup as well.
parent cf4b8b1f
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@ class LayoutBuilder implements TrustedCallbackInterface {
* #pre_render callback: Alters layout builder to use dropbuttons to add custom blocks.
*/
public static function preRender($element) {
$inline_blocks_category = (string) t('Inline blocks');
$lb = &$element['layout_builder'];
$section_storage = $element['#section_storage'];
......@@ -32,29 +33,50 @@ class LayoutBuilder implements TrustedCallbackInterface {
$storage_id = $section_storage->getStorageId();
$block_manager = \Drupal::getContainer()->get('plugin.manager.block');
// Get restriction definitions, if 'layout_builder_restrictions' is installed.
$restriction_plugins = NULL;
$layout_builder_restrictions_manager = NULL;
if (\Drupal::moduleHandler()->moduleExists('layout_builder_restrictions')) {
$layout_builder_restrictions_manager = \Drupal::service('plugin.manager.layout_builder_restriction');
// Retrieve defined Layout Builder Restrictions plugins.
$restriction_plugins = $layout_builder_restrictions_manager->getSortedPlugins();
}
// Loop through each section.
foreach (Element::children($lb) as $section) {
if (isset($lb[$section]['layout-builder__section'])) {
$sec = $lb[$section]['layout-builder__section'];
// Loop through each region.
foreach (Element::children($lb[$section]['layout-builder__section']) as $region) {
$reg = $lb[$section]['layout-builder__section'][$region];
// Based on Drupal\layout_builder\Controller\ChooseBlockController::inlineBlockList()
$definitions = $block_manager->getFilteredDefinitions('layout_builder', $contexts, [
'section_storage' => $section_storage,
'region' => $region,
'list' => 'inline_blocks',
]);
$blocks = $block_manager->getGroupedDefinitions($definitions);
$links = [];
$inline_blocks_category = (string) t('Inline blocks');
if (isset($blocks[$inline_blocks_category])) {
// Remove 'layout_builder_restrictions' filtered blocks, if applicable.
if ($restriction_plugins && $layout_builder_restrictions_manager) {
foreach (array_keys($restriction_plugins) as $id) {
$plugin = $layout_builder_restrictions_manager->createInstance($id);
$allowed_inline_blocks = $plugin->inlineBlocksAllowedinContext($section_storage, $delta, $region);
// Loop through links and remove those for disallowed inline block types.
foreach ($blocks[$inline_blocks_category] as $block_id => $block) {
if (!in_array($block_id, $allowed_inline_blocks)) {
unset($blocks[$inline_blocks_category][$block_id]);
}
}
}
}
foreach ($blocks[$inline_blocks_category] as $block_id => $block) {
$attributes = [
'class' => [
'use-ajax',
// 'layout-builder__link',
// 'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
......@@ -91,8 +113,6 @@ class LayoutBuilder implements TrustedCallbackInterface {
'attributes' => [
'class' => [
'use-ajax',
// 'layout-builder__link',
// 'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
......
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