You are here

function layout_builder_styles_update_8003 in Layout Builder Styles 8

Allow block styles per block type for any defined reusable block instances.

File

./layout_builder_styles.install, line 108
Layout Builder Styles install file.

Code

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();
  }
}