Commit f5e236b9 authored by Mark Fullmer's avatar Mark Fullmer Committed by Mark Fullmer
Browse files

Issue #3068722 by mark_fullmer, twfahey, bkosborne: Don't allow block...

Issue #3068722 by mark_fullmer, twfahey, bkosborne: Don't allow block restrictions for individual content blocks
parent ed683307
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@

use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder_styles\LayoutBuilderStyleInterface;

/**
 * Migrate away from using section component TPS to the old additional property.
@@ -93,3 +94,40 @@ function layout_builder_styles_update_8002() {
  $config->set('form_type', 'checkboxes');
  $config->save();
}

/**
 * Allow block styles per block type for any defined reusable block instances.
 */
function layout_builder_styles_update_8003() {
  // Per #3068722, block restrictions/allowances will no longer be supported
  // for individual block instances. Find any defined instance allowances
  // and re-save that allowance as a block type whitelist.
  // This will effectively maintain allowances for those block instances, while
  // theoretically adding allowances for other block instances which had
  // been restricted.
  $styles = \Drupal::entityTypeManager()
    ->getStorage('layout_builder_style')
    ->loadByProperties([
      'type' => LayoutBuilderStyleInterface::TYPE_COMPONENT,
    ]);
  foreach ($styles as $style) {
    $restrictions = $style->getBlockRestrictions();
    foreach ($restrictions as $key => $allowed) {
      // If this is a reusable block, retrieve the block bundle.
      if (strpos($allowed, "block_content:") === 0) {
        $uuid = str_replace('block_content:', '', $allowed);
        $bundle = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid)
          ->bundle();
        if ($bundle && !in_array('inline_block:' . $bundle, $restrictions)) {
          // Add a block type allowance if it doesn't exist.
          array_push($restrictions, 'inline_block:' . $bundle);
        }
        // Remove the now defunct block instance allowance.
        unset($restrictions[$key]);
      }
    }
    // Re-save style restrictions.
    $style->set('block_restrictions', $restrictions);
    $style->save();
  }
}
+11 −5
Original line number Diff line number Diff line
@@ -99,11 +99,17 @@ class LayoutBuilderStyleForm extends EntityForm implements ContainerInjectionInt
    $blockDefinitions = $this->blockManager->getDefinitions();
    $blockDefinitions = $this->blockManager->getGroupedDefinitions($blockDefinitions);

    // Remove individual reusable blocks from list.
    unset($blockDefinitions['Custom']);

    if (isset($blockDefinitions['Inline blocks'])) {
      // Relabel the inline block type listing as generic "Custom block types".
      // This category will apply to inline blocks & reusable blocks.
      $blockDefinitions['Custom block types'] = $blockDefinitions['Inline blocks'];
      unset($blockDefinitions['Inline blocks']);
      ksort($blockDefinitions);
    }


    $form['block_restrictions'] = [
      '#type' => 'details',
+6 −0
Original line number Diff line number Diff line
@@ -309,6 +309,12 @@ class LayoutBuilderStyleTest extends BrowserTestBase {
      'block_restrictions' => ['inline_block:alternate', 'field_block:node:bundle_with_section_field:promote'],
    ])->save();

    // Block instances are not allowed to be restricted.
    $this->drupalGet('admin/config/content/layout_builder_style/unrestricted/edit');
    foreach ($blocks as $label => $uuid) {
      $assert_session->elementNotExists('css', 'input[name="block_restrictions[block_content:' . $uuid . ']"]');
    }

    // Set the configuration to allow multiple styles per block.
    $this->drupalGet('/admin/config/content/layout_builder_style/config');
    $page->selectFieldOption('edit-multiselect-multiple', 'multiple');