You are here

protected function PluginHelperTrait::getBlockDefinitions in Layout Builder Restrictions 8.2

Gets block definitions appropriate for an entity display.

Parameters

\Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display: The entity display being edited.

Return value

array[] Keys are category names, and values are arrays of which the keys are plugin IDs and the values are plugin definitions.

3 calls to PluginHelperTrait::getBlockDefinitions()
AllowedBlocksForm::buildForm in modules/layout_builder_restrictions_by_region/src/Form/AllowedBlocksForm.php
Form constructor.
FormAlter::alterEntityViewDisplayForm in src/Form/FormAlter.php
The actual form elements.
FormAlter::setAllowedBlockCategories in src/Form/FormAlter.php
Helper function to prepare saved block definition categories.

File

src/Traits/PluginHelperTrait.php, line 31

Class

PluginHelperTrait
Methods to help Layout Builder Restrictions plugins.

Namespace

Drupal\layout_builder_restrictions\Traits

Code

protected function getBlockDefinitions(LayoutEntityDisplayInterface $display) {

  // Check for 'load' method, which only exists in > 8.7.
  if (method_exists($this
    ->sectionStorageManager(), 'load')) {
    $section_storage = $this
      ->sectionStorageManager()
      ->load('defaults', [
      'display' => EntityContext::fromEntity($display),
    ]);
  }
  else {

    // BC for < 8.7.
    $section_storage = $this
      ->sectionStorageManager()
      ->loadEmpty('defaults')
      ->setSectionList($display);
  }

  // Do not use the plugin filterer here, but still filter by contexts.
  $definitions = $this
    ->blockManager()
    ->getDefinitions();

  // Create a list of block_content IDs for later filtering.
  $custom_blocks = [];
  foreach ($definitions as $key => $definition) {
    if ($definition['provider'] == 'block_content') {
      $custom_blocks[] = $key;
    }
  }

  // Allow filtering of available blocks by other parts of the system.
  $definitions = $this
    ->contextHandler()
    ->filterPluginDefinitionsByContexts($this
    ->getAvailableContexts($section_storage), $definitions);
  $grouped_definitions = $this
    ->getDefinitionsByUntranslatedCategory($definitions);

  // Create a new category of block_content blocks that meet the context.
  foreach ($grouped_definitions as $category => $data) {
    if (empty($data['definitions'])) {
      unset($grouped_definitions[$category]);
    }

    // Ensure all block_content definitions are included in the
    // 'Custom blocks' category.
    foreach ($data['definitions'] as $key => $definition) {
      if (in_array($key, $custom_blocks)) {
        if (!isset($grouped_definitions['Custom blocks'])) {
          $grouped_definitions['Custom blocks'] = [
            'label' => 'Custom blocks',
            'data' => [],
          ];
        }

        // Remove this block_content from its previous category so
        // that it is defined only in one place.
        unset($grouped_definitions[$category]['definitions'][$key]);
        $grouped_definitions['Custom blocks']['definitions'][$key] = $definition;
      }
    }
  }

  // Generate a list of custom block types under the
  // 'Custom block types' namespace.
  $custom_block_bundles = $this
    ->entityTypeBundleInfo()
    ->getBundleInfo('block_content');
  if ($custom_block_bundles) {
    $grouped_definitions['Custom block types'] = [
      'label' => 'Custom block types',
      'definitions' => [],
    ];
    foreach ($custom_block_bundles as $machine_name => $value) {
      $grouped_definitions['Custom block types']['definitions'][$machine_name] = [
        'admin_label' => $value['label'],
        'category' => $this
          ->t('Custom block types'),
      ];
    }
  }
  ksort($grouped_definitions);
  return $grouped_definitions;
}