You are here

public function BlockListingForm::loadCategories in Layout Builder Browser 8

Loads block categories and blocks, grouped by block categories.

Return value

\Drupal\Core\Config\Entity\ConfigEntityInterface[][] An associative array with two keys:

  • categories: All available block categories, each followed by all blocks attached to it.
  • hidden_blocks: All blocks that aren't attached to any block categories.
1 call to BlockListingForm::loadCategories()
BlockListingForm::buildForm in src/Form/BlockListingForm.php
Form constructor.

File

src/Form/BlockListingForm.php, line 283

Class

BlockListingForm
Builds a listing of block entities.

Namespace

Drupal\layout_builder_browser\Form

Code

public function loadCategories() {
  $block_categories = $this->entityTypeManager
    ->getStorage('layout_builder_browser_blockcat')
    ->loadMultiple();
  uasort($block_categories, [
    'Drupal\\Core\\Config\\Entity\\ConfigEntityBase',
    'sort',
  ]);
  $block_categories_group = [];
  foreach ($block_categories as $key => $block_category) {
    $block_categories_group[$key]['category'] = $block_category;
    $block_categories_group[$key]['blocks'] = [];
    $blocks = \Drupal::entityTypeManager()
      ->getStorage('layout_builder_browser_block')
      ->loadByProperties([
      'category' => $key,
    ]);
    uasort($blocks, [
      'Drupal\\Core\\Config\\Entity\\ConfigEntityBase',
      'sort',
    ]);
    foreach ($blocks as $block) {
      $blockdefinition = \Drupal::service('plugin.manager.block')
        ->getDefinition($block->block_id);
      $item = [];
      $item['id'] = $block->id;
      $item['weight'] = $block->weight;
      $item['label'] = $block
        ->label();
      $item['block_provider'] = $blockdefinition['admin_label'] . " - " . $blockdefinition["category"];
      $item['block_id'] = $block->block_id;
      $block_categories_group[$key]['blocks'][] = $item;
    }
  }
  return $block_categories_group;
}