You are here

public function BrowserController::browse in Layout Builder Browser 8

Overrides the core ChooseBlockController::build.

1 string reference to 'BrowserController::browse'
layout_builder_browser.routing.yml in ./layout_builder_browser.routing.yml
layout_builder_browser.routing.yml

File

src/Controller/BrowserController.php, line 94

Class

BrowserController
Class BrowserController.

Namespace

Drupal\layout_builder_browser\Controller

Code

public function browse(SectionStorageInterface $section_storage, $delta, $region) {
  $config = $this
    ->config('layout_builder_browser.settings');
  $enabled_section_storages = $config
    ->get('enabled_section_storages');
  if (!in_array($section_storage
    ->getPluginId(), $enabled_section_storages)) {
    $default_choose_block_controller = new ChooseBlockController($this->blockManager, $this->entityTypeManager, $this->currentUser);
    return $default_choose_block_controller
      ->build($section_storage, $delta, $region);
  }
  $build['filter'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter by block name'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => $this
      ->t('Filter by block name'),
    '#attributes' => [
      'class' => [
        'js-layout-builder-filter',
      ],
      'title' => $this
        ->t('Enter a part of the block name to filter by.'),
    ],
  ];
  $block_categories['#type'] = 'container';
  $block_categories['#attributes']['class'][] = 'block-categories';
  $block_categories['#attributes']['class'][] = 'js-layout-builder-categories';
  $block_categories['#attributes']['data-layout-builder-target-highlight-id'] = $this
    ->blockAddHighlightId($delta, $region);

  // @todo Explicitly cast delta to an integer, remove this in
  //   https://www.drupal.org/project/drupal/issues/2984509.
  $delta = (int) $delta;
  $definitions = $this->blockManager
    ->getFilteredDefinitions('layout_builder', $this
    ->getAvailableContexts($section_storage), [
    'section_storage' => $section_storage,
    'delta' => $delta,
    'region' => $region,
    'list' => 'inline_blocks',
  ]);
  $blockcats = $this->entityTypeManager
    ->getStorage('layout_builder_browser_blockcat')
    ->loadMultiple();
  uasort($blockcats, [
    'Drupal\\Core\\Config\\Entity\\ConfigEntityBase',
    'sort',
  ]);
  foreach ($blockcats as $blockcat) {
    $blocks = [];
    $query = \Drupal::entityQuery('layout_builder_browser_block')
      ->condition('category', $blockcat->id);
    $items = $this->entityTypeManager
      ->getStorage('layout_builder_browser_block')
      ->loadMultiple($query
      ->execute());
    uasort($items, [
      'Drupal\\Core\\Config\\Entity\\ConfigEntityBase',
      'sort',
    ]);
    foreach ($items as $item) {
      $key = $item->block_id;
      if (isset($definitions[$key])) {
        $blocks[$key] = $definitions[$key];
        $blocks[$key]['layout_builder_browser_data'] = $item;
      }
    }
    $block_categories[$blockcat
      ->id()]['links'] = $this
      ->getBlocks($section_storage, $delta, $region, $blocks);
    if ($block_categories[$blockcat
      ->id()]['links']) {

      // Only add the information if the category has links.
      $block_categories[$blockcat
        ->id()]['#type'] = 'details';
      $block_categories[$blockcat
        ->id()]['#attributes']['class'][] = 'js-layout-builder-category';
      $block_categories[$blockcat
        ->id()]['#open'] = TRUE;
      $block_categories[$blockcat
        ->id()]['#title'] = Html::escape($blockcat
        ->label());
    }
    else {

      // Since the category doesn't have links, remove it to avoid confusion.
      unset($block_categories[$blockcat
        ->id()]);
    }
  }

  // Special case for auto adding of reusable content blocks by bundle.
  $auto_added_reusable_bundles = $config
    ->get('auto_added_reusable_block_content_bundles') ?? [];
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo('block_content');
  $existing_blocks = $this->entityTypeManager
    ->getStorage('layout_builder_browser_block')
    ->loadMultiple();
  $existing_blocks_ids = array_column($existing_blocks, 'block_id');
  foreach ($auto_added_reusable_bundles as $machine_name) {
    $blocks = [];
    $content_blocks = $this->entityTypeManager
      ->getStorage('block_content')
      ->loadByProperties([
      'type' => $machine_name,
      'reusable' => TRUE,
    ]);
    foreach ($content_blocks as $block) {
      $block_plugin_id = 'block_content:' . $block
        ->uuid();

      // Only blocks available in layout definition and not selected in the
      // browser categories yet.
      if (!empty($definitions[$block_plugin_id]) && !in_array($block_plugin_id, $existing_blocks_ids)) {
        $blocks[$block_plugin_id] = $definitions[$block_plugin_id];
      }
    }
    if ($blocks) {
      $block_links = $this
        ->getBlocks($section_storage, $delta, $region, $blocks);
      if ($block_links) {
        $bundle_label = $bundles[$machine_name]['label'];

        // Only add the information if the category has links.
        $block_categories[$bundle_label]['links'] = $block_links;
        $block_categories[$bundle_label]['#type'] = 'details';
        $block_categories[$bundle_label]['#attributes']['class'][] = 'js-layout-builder-category';
        $block_categories[$bundle_label]['#open'] = TRUE;
        $block_categories[$bundle_label]['#title'] = $this
          ->t('Reusable @block_type_label', [
          '@block_type_label' => $bundle_label,
        ]);
      }
    }
  }
  $build['block_categories'] = $block_categories;
  $build['#attached']['library'][] = 'layout_builder_browser/browser';
  if ($config
    ->get('use_modal')) {
    $build['#attached']['library'][] = 'layout_builder_browser/modal';
  }
  return $build;
}