Loading modules/layout_builder_restrictions_by_region/src/Plugin/LayoutBuilderRestriction/EntityViewModeRestrictionByRegion.php +11 −4 Original line number Diff line number Diff line Loading @@ -304,6 +304,7 @@ class EntityViewModeRestrictionByRegion extends LayoutBuilderRestrictionBase { $third_party_settings = $view_display->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', []); $allowlisted_blocks = (isset($third_party_settings['allowlisted_blocks'])) ? $third_party_settings['allowlisted_blocks'] : []; $denylisted_blocks = (isset($third_party_settings['denylisted_blocks'])) ? $third_party_settings['denylisted_blocks'] : []; $restricted_categories = (isset($third_party_settings['restricted_categories'])) ? $third_party_settings['restricted_categories'] : []; $layout_id = $section_storage->getSection($delta)->getLayoutId(); Loading @@ -312,18 +313,24 @@ class EntityViewModeRestrictionByRegion extends LayoutBuilderRestrictionBase { if (isset($third_party_settings['allowlisted_blocks'][$layout_id]['all_regions']) || isset($third_party_settings['denylisted_blocks'][$layout_id]['all_regions']) || isset($third_party_settings['restricted_categories'][$layout_id]['all_regions'])) { $region = 'all_regions'; } $inline_blocks_category = 'Inline blocks'; // Check if the whole category is restricted. if (in_array($inline_blocks_category, $restricted_categories[$layout_id][$region] ?? [])) { return []; } // Check if allowed inline blocks are defined in config. if (isset($allowlisted_blocks[$layout_id][$region]['Inline blocks'])) { return $allowlisted_blocks[$layout_id][$region]['Inline blocks']; if (isset($allowlisted_blocks[$layout_id][$region][$inline_blocks_category])) { return $allowlisted_blocks[$layout_id][$region][$inline_blocks_category]; } // If not, then allow some inline blocks and check for denylisting. else { $inline_blocks = $this->getInlineBlockPlugins(); if (isset($denylisted_blocks[$layout_id][$region]['Inline blocks'])) { if (isset($denylisted_blocks[$layout_id][$region][$inline_blocks_category])) { foreach ($inline_blocks as $key => $block) { // Unset explicitly denylisted inline blocks. if (in_array($block, $denylisted_blocks[$layout_id][$region]['Inline blocks'])) { if (in_array($block, $denylisted_blocks[$layout_id][$region][$inline_blocks_category])) { unset($inline_blocks[$key]); } } Loading modules/layout_builder_restrictions_by_region/tests/src/FunctionalJavascript/BlockPlacementCategoryRestrictionTest.php +40 −0 Original line number Diff line number Diff line Loading @@ -189,6 +189,46 @@ class BlockPlacementCategoryRestrictionTest extends WebDriverTestBase { $assert_session->assertWaitOnAjaxRequest(); $assert_session->linkExists('Basic'); $assert_session->linkExists('Alternate'); // Restrict Inline blocks categorically $this->drupalGet("$field_ui_prefix/display/default"); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section"]/summary'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-per-region"]'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]//a'); $element->click(); $assert_session->assertWaitOnAjaxRequest(); // Impose Custom Block type restrictions. $assert_session->checkboxChecked('Allow all existing & new Inline blocks blocks.'); $assert_session->checkboxNotChecked('Restrict specific Inline blocks blocks:'); // Set Inline blocks category to be restricted. $element = $page->find('xpath', '//*[contains(@class, "form-item-allowed-blocks-inline-blocks-restriction")]/input[@value="restrict_all"]'); $element->click(); $element = $page->find('xpath', '//*[starts-with(@id,"edit-submit--")]'); $element->click(); $assert_session->assertWaitOnAjaxRequest(); $page->pressButton('Save'); $this->drupalGet("$field_ui_prefix/display/default"); // Verify the UI reflects the restriction: the "first" section should not read // "Unrestricted," while the second region *should* still read "Unrestricted." $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section"]/summary'); $element->click(); $assert_session->elementNotContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]', 'Unrestricted'); $assert_session->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]', 'Unrestricted'); $this->clickLink('Manage layout'); $assert_session->addressEquals("$field_ui_prefix/display/default/layout"); // Select 'Add block' link in First region. $element = $page->find('xpath', '//*[contains(@class, "layout__region--first")]//a'); $element->click(); $assert_session->assertWaitOnAjaxRequest(); // Inline block types are restricted. $assert_session->linkNotExists('Create custom block'); } /** Loading tests/src/FunctionalJavascript/CategoryRestrictionsTest.php 0 → 100644 +80 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\layout_builder_restrictions\FunctionalJavascript; /** * Demonstrate that blocks can be restricted by category. * * @group layout_builder_restrictions */ class CategoryRestrictionsTest extends LayoutBuilderRestrictionsTestBase { /** * Verify that the UI can restrict layouts in Layout Builder settings tray. */ public function testLayoutRestriction() { $this->getSession()->resizeWindow(1200, 4000); // Create 2 custom block types, with 3 block instances. $blocks = $this->generateTestBlocks(); $node_id = $this->generateTestNode(); $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); // Baseline: Inline blocks are allowed. // Establish baseline behavior prior to any restrictions. $this->navigateToNodeSettingsTray($node_id); // Initially, the body field is available. $assert_session->linkExists('Body'); // Initially, custom blocks instances are available. $assert_session->linkExists('Basic Block 1'); $assert_session->linkExists('Basic Block 2'); $assert_session->linkExists('Alternate Block 1'); // Initially, all inline block types are allowed. $this->clickLink('Create custom block'); $this->assertNotEmpty($assert_session->waitForText('Add a new custom block')); $assert_session->linkExists('Basic'); $assert_session->linkExists('Alternate'); $this->navigateToManageDisplay(); $element = $page->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layout-restriction-all"]'); $assert_session->checkboxChecked('edit-layout-builder-restrictions-allowed-layouts-layout-restriction-all'); $assert_session->checkboxNotChecked('edit-layout-builder-restrictions-allowed-layouts-layout-restriction-restricted'); // Restrict all Inline blocks as a category. $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-inline-blocks-restriction-restrict-all"]'); $element->click(); $page->pressButton('Save'); // Verify Inline blocks, as a category, are restricted. $this->navigateToNodeSettingsTray($node_id); // Existing custom block instances are still present // because we did not restrict 'Custom block types'. $assert_session->linkExists('Body'); $assert_session->linkExists('Basic Block 1'); $assert_session->linkExists('Basic Block 2'); $assert_session->linkExists('Alternate Block 1'); // New inline blocks of any type cannot be created. $assert_session->linkNotExists('Create custom block'); $this->navigateToManageDisplay(); $element = $page->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layout-restriction-all"]'); // Restrict existing Custom blocks as a category. $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-custom-block-types-restriction-restrict-all"]'); $element->click(); $page->pressButton('Save'); // Existing Custom blocks are restricted categorically. $this->navigateToNodeSettingsTray($node_id); $assert_session->linkNotExists('Basic Block 1'); $assert_session->linkNotExists('Basic Block 2'); $assert_session->linkNotExists('Alternate Block 1'); // Content fields are still allowed. $assert_session->linkExists('Body'); } } Loading
modules/layout_builder_restrictions_by_region/src/Plugin/LayoutBuilderRestriction/EntityViewModeRestrictionByRegion.php +11 −4 Original line number Diff line number Diff line Loading @@ -304,6 +304,7 @@ class EntityViewModeRestrictionByRegion extends LayoutBuilderRestrictionBase { $third_party_settings = $view_display->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', []); $allowlisted_blocks = (isset($third_party_settings['allowlisted_blocks'])) ? $third_party_settings['allowlisted_blocks'] : []; $denylisted_blocks = (isset($third_party_settings['denylisted_blocks'])) ? $third_party_settings['denylisted_blocks'] : []; $restricted_categories = (isset($third_party_settings['restricted_categories'])) ? $third_party_settings['restricted_categories'] : []; $layout_id = $section_storage->getSection($delta)->getLayoutId(); Loading @@ -312,18 +313,24 @@ class EntityViewModeRestrictionByRegion extends LayoutBuilderRestrictionBase { if (isset($third_party_settings['allowlisted_blocks'][$layout_id]['all_regions']) || isset($third_party_settings['denylisted_blocks'][$layout_id]['all_regions']) || isset($third_party_settings['restricted_categories'][$layout_id]['all_regions'])) { $region = 'all_regions'; } $inline_blocks_category = 'Inline blocks'; // Check if the whole category is restricted. if (in_array($inline_blocks_category, $restricted_categories[$layout_id][$region] ?? [])) { return []; } // Check if allowed inline blocks are defined in config. if (isset($allowlisted_blocks[$layout_id][$region]['Inline blocks'])) { return $allowlisted_blocks[$layout_id][$region]['Inline blocks']; if (isset($allowlisted_blocks[$layout_id][$region][$inline_blocks_category])) { return $allowlisted_blocks[$layout_id][$region][$inline_blocks_category]; } // If not, then allow some inline blocks and check for denylisting. else { $inline_blocks = $this->getInlineBlockPlugins(); if (isset($denylisted_blocks[$layout_id][$region]['Inline blocks'])) { if (isset($denylisted_blocks[$layout_id][$region][$inline_blocks_category])) { foreach ($inline_blocks as $key => $block) { // Unset explicitly denylisted inline blocks. if (in_array($block, $denylisted_blocks[$layout_id][$region]['Inline blocks'])) { if (in_array($block, $denylisted_blocks[$layout_id][$region][$inline_blocks_category])) { unset($inline_blocks[$key]); } } Loading
modules/layout_builder_restrictions_by_region/tests/src/FunctionalJavascript/BlockPlacementCategoryRestrictionTest.php +40 −0 Original line number Diff line number Diff line Loading @@ -189,6 +189,46 @@ class BlockPlacementCategoryRestrictionTest extends WebDriverTestBase { $assert_session->assertWaitOnAjaxRequest(); $assert_session->linkExists('Basic'); $assert_session->linkExists('Alternate'); // Restrict Inline blocks categorically $this->drupalGet("$field_ui_prefix/display/default"); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section"]/summary'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-restriction-behavior-per-region"]'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]//a'); $element->click(); $assert_session->assertWaitOnAjaxRequest(); // Impose Custom Block type restrictions. $assert_session->checkboxChecked('Allow all existing & new Inline blocks blocks.'); $assert_session->checkboxNotChecked('Restrict specific Inline blocks blocks:'); // Set Inline blocks category to be restricted. $element = $page->find('xpath', '//*[contains(@class, "form-item-allowed-blocks-inline-blocks-restriction")]/input[@value="restrict_all"]'); $element->click(); $element = $page->find('xpath', '//*[starts-with(@id,"edit-submit--")]'); $element->click(); $assert_session->assertWaitOnAjaxRequest(); $page->pressButton('Save'); $this->drupalGet("$field_ui_prefix/display/default"); // Verify the UI reflects the restriction: the "first" section should not read // "Unrestricted," while the second region *should* still read "Unrestricted." $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section"]/summary'); $element->click(); $assert_session->elementNotContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="first"]', 'Unrestricted'); $assert_session->elementContains('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-twocol-section-table"]/tbody/tr[@data-region="second"]', 'Unrestricted'); $this->clickLink('Manage layout'); $assert_session->addressEquals("$field_ui_prefix/display/default/layout"); // Select 'Add block' link in First region. $element = $page->find('xpath', '//*[contains(@class, "layout__region--first")]//a'); $element->click(); $assert_session->assertWaitOnAjaxRequest(); // Inline block types are restricted. $assert_session->linkNotExists('Create custom block'); } /** Loading
tests/src/FunctionalJavascript/CategoryRestrictionsTest.php 0 → 100644 +80 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\layout_builder_restrictions\FunctionalJavascript; /** * Demonstrate that blocks can be restricted by category. * * @group layout_builder_restrictions */ class CategoryRestrictionsTest extends LayoutBuilderRestrictionsTestBase { /** * Verify that the UI can restrict layouts in Layout Builder settings tray. */ public function testLayoutRestriction() { $this->getSession()->resizeWindow(1200, 4000); // Create 2 custom block types, with 3 block instances. $blocks = $this->generateTestBlocks(); $node_id = $this->generateTestNode(); $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); // Baseline: Inline blocks are allowed. // Establish baseline behavior prior to any restrictions. $this->navigateToNodeSettingsTray($node_id); // Initially, the body field is available. $assert_session->linkExists('Body'); // Initially, custom blocks instances are available. $assert_session->linkExists('Basic Block 1'); $assert_session->linkExists('Basic Block 2'); $assert_session->linkExists('Alternate Block 1'); // Initially, all inline block types are allowed. $this->clickLink('Create custom block'); $this->assertNotEmpty($assert_session->waitForText('Add a new custom block')); $assert_session->linkExists('Basic'); $assert_session->linkExists('Alternate'); $this->navigateToManageDisplay(); $element = $page->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layout-restriction-all"]'); $assert_session->checkboxChecked('edit-layout-builder-restrictions-allowed-layouts-layout-restriction-all'); $assert_session->checkboxNotChecked('edit-layout-builder-restrictions-allowed-layouts-layout-restriction-restricted'); // Restrict all Inline blocks as a category. $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-inline-blocks-restriction-restrict-all"]'); $element->click(); $page->pressButton('Save'); // Verify Inline blocks, as a category, are restricted. $this->navigateToNodeSettingsTray($node_id); // Existing custom block instances are still present // because we did not restrict 'Custom block types'. $assert_session->linkExists('Body'); $assert_session->linkExists('Basic Block 1'); $assert_session->linkExists('Basic Block 2'); $assert_session->linkExists('Alternate Block 1'); // New inline blocks of any type cannot be created. $assert_session->linkNotExists('Create custom block'); $this->navigateToManageDisplay(); $element = $page->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary'); $element->click(); $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layout-restriction-all"]'); // Restrict existing Custom blocks as a category. $element = $page->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-custom-block-types-restriction-restrict-all"]'); $element->click(); $page->pressButton('Save'); // Existing Custom blocks are restricted categorically. $this->navigateToNodeSettingsTray($node_id); $assert_session->linkNotExists('Basic Block 1'); $assert_session->linkNotExists('Basic Block 2'); $assert_session->linkNotExists('Alternate Block 1'); // Content fields are still allowed. $assert_session->linkExists('Body'); } }