You are here

public function LayoutBuilderBrowserEventSubscriber::onView in Gin Layout Builder 8.2

Add layout-builder-browser class layout_builder.choose_block build block.

File

modules/wingsuit_lb/src/EventSubscriber/LayoutBuilderBrowserEventSubscriber.php, line 30

Class

LayoutBuilderBrowserEventSubscriber
Class LayoutBuilderBrowserEventSubscriber.

Namespace

Drupal\wingsuit_lb\EventSubscriber

Code

public function onView(GetResponseForControllerResultEvent $event) {
  $request = $event
    ->getRequest();
  $route = $request->attributes
    ->get('_route');
  if ($route == 'layout_builder.choose_section') {
    $build = $event
      ->getControllerResult();
    $build['#attached']['library'][] = 'wingsuit_lb/core';
    $add_sections = [
      '#type' => 'details',
      '#title' => $this
        ->t('Sections'),
      '#attributes' => [
        'class' => [
          'ws-lb ws-lb-container',
        ],
      ],
    ];
    $add_sections['items'] = $build['layouts']['#items'];
    foreach ($add_sections['items'] as &$item) {
      $item['#title']['icon']['#theme'] = 'wingsuit_lb_icon';
      $item['#title']['label']['#attributes']['class'] = [
        'ws-lb-link__label',
      ];
      $item['#attributes']['class'][] = 'ws-lb-link';
    }
    $request = $event
      ->getRequest();

    /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage $section_storage */
    $section_storage = $request->attributes
      ->get('section_storage');
    $delta = $request->attributes
      ->get('delta');
    $add_library = [
      '#type' => 'details',
      '#title' => $this
        ->t('Library'),
      '#attributes' => [
        'class' => [
          'ws-lb ws-lb-container',
        ],
      ],
    ];
    $add_library['items'] = $this
      ->getLibrarySectionLinks($section_storage, $delta);
    $add_library['#access'] = count($add_library['items']) > 0;
    unset($build['layouts']);
    $build['layouts'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'ws-tabs',
        ],
      ],
    ];
    $build['layouts']['tabs'] = [
      '#type' => 'horizontal_tabs',
      'items' => [
        $add_sections,
        $add_library,
      ],
    ];
    $event
      ->setControllerResult($build);
  }
  if ($route == 'layout_builder.choose_block') {
    $build['#attached']['library'][] = 'wingsuit_lb/core';
    $build = $event
      ->getControllerResult();
    if (is_array($build) && !isset($build['add_block'])) {
      $build['block_categories']['#type'] = 'horizontal_tabs';
      foreach (Element::children($build['block_categories']) as $child) {
        foreach (Element::children($build['block_categories'][$child]['links']) as $link_id) {
          $link =& $build['block_categories'][$child]['links'][$link_id];
          $link['#attributes']['class'][] = 'ws-lb-link';
          $link['#title']['image']['#theme'] = 'wingsuit_lb_icon';
          $link['#title']['label']['#markup'] = '<div class="ws-lb-link__label">' . $link['#title']['label']['#markup'] . '</div>';
          if (($key = array_search('layout-builder-browser-block-item', $link['#attributes']['class'])) !== FALSE) {
            unset($link['#attributes']['class'][$key]);
          }
        }
      }
      if (($key = array_search('layout-builder-browser', $build['block_categories']['#attributes']['class'])) !== FALSE) {
        unset($build['block_categories']['#attributes']['class'][$key]);
      }
      $build['block_categories']['#attributes']['class'][] = 'ws-lb';
      $event
        ->setControllerResult($build);
    }
  }
}