diff core/modules/layout_builder/src/Controller/ChooseBlockController.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
line wrap: on
line diff
--- a/core/modules/layout_builder/src/Controller/ChooseBlockController.php	Thu Feb 28 13:21:36 2019 +0000
+++ b/core/modules/layout_builder/src/Controller/ChooseBlockController.php	Thu May 09 15:33:08 2019 +0100
@@ -6,9 +6,11 @@
 use Drupal\Core\Block\BlockManagerInterface;
 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\Core\Url;
 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
+use Drupal\layout_builder\LayoutBuilderHighlightTrait;
 use Drupal\layout_builder\SectionStorageInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -16,11 +18,13 @@
  * Defines a controller to choose a new block.
  *
  * @internal
+ *   Controller classes are internal.
  */
 class ChooseBlockController implements ContainerInjectionInterface {
 
   use AjaxHelperTrait;
   use LayoutBuilderContextTrait;
+  use LayoutBuilderHighlightTrait;
   use StringTranslationTrait;
 
   /**
@@ -38,16 +42,30 @@
   protected $entityTypeManager;
 
   /**
+   * The current user.
+   *
+   * @var \Drupal\Core\Session\AccountInterface
+   */
+  protected $currentUser;
+
+  /**
    * ChooseBlockController constructor.
    *
    * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
    *   The block manager.
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
    *   The entity type manager.
+   * @param \Drupal\Core\Session\AccountInterface $current_user
+   *   The current user.
    */
-  public function __construct(BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager) {
+  public function __construct(BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user = NULL) {
     $this->blockManager = $block_manager;
     $this->entityTypeManager = $entity_type_manager;
+    if (!$current_user) {
+      @trigger_error('The current_user service must be passed to ChooseBlockController::__construct(), it is required before Drupal 9.0.0.', E_USER_DEPRECATED);
+      $current_user = \Drupal::currentUser();
+    }
+    $this->currentUser = $current_user;
   }
 
   /**
@@ -56,7 +74,8 @@
   public static function create(ContainerInterface $container) {
     return new static(
       $container->get('plugin.manager.block'),
-      $container->get('entity_type.manager')
+      $container->get('entity_type.manager'),
+      $container->get('current_user')
     );
   }
 
@@ -74,7 +93,6 @@
    *   A render array.
    */
   public function build(SectionStorageInterface $section_storage, $delta, $region) {
-    $build['#title'] = $this->t('Choose a block');
     if ($this->entityTypeManager->hasDefinition('block_content_type') && $types = $this->entityTypeManager->getStorage('block_content_type')->loadMultiple()) {
       if (count($types) === 1) {
         $type = reset($types);
@@ -105,13 +123,28 @@
             '@entity_type' => $this->entityTypeManager->getDefinition('block_content')->getSingularLabel(),
           ]),
           '#attributes' => $this->getAjaxAttributes(),
+          '#access' => $this->currentUser->hasPermission('create and edit custom blocks'),
         ];
         $build['add_block']['#attributes']['class'][] = 'inline-block-create-button';
       }
     }
 
+    $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.
@@ -125,6 +158,7 @@
     $grouped_definitions = $this->blockManager->getGroupedDefinitions($definitions);
     foreach ($grouped_definitions as $category => $blocks) {
       $block_categories[$category]['#type'] = 'details';
+      $block_categories[$category]['#attributes']['class'][] = 'js-layout-builder-category';
       $block_categories[$category]['#open'] = TRUE;
       $block_categories[$category]['#title'] = $category;
       $block_categories[$category]['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks);
@@ -154,8 +188,9 @@
     ]);
     $blocks = $this->blockManager->getGroupedDefinitions($definitions);
     $build = [];
-    if (isset($blocks['Inline blocks'])) {
-      $build['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks['Inline blocks']);
+    $inline_blocks_category = (string) $this->t('Inline blocks');
+    if (isset($blocks[$inline_blocks_category])) {
+      $build['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks[$inline_blocks_category]);
       $build['links']['#attributes']['class'][] = 'inline-block-list';
       foreach ($build['links']['#links'] as &$link) {
         $link['attributes']['class'][] = 'inline-block-list__item';
@@ -174,6 +209,7 @@
         '#attributes' => $this->getAjaxAttributes(),
       ];
     }
+    $build['links']['#attributes']['data-layout-builder-target-highlight-id'] = $this->blockAddHighlightId($delta, $region);
     return $build;
   }
 
@@ -195,6 +231,8 @@
   protected function getBlockLinks(SectionStorageInterface $section_storage, $delta, $region, array $blocks) {
     $links = [];
     foreach ($blocks as $block_id => $block) {
+      $attributes = $this->getAjaxAttributes();
+      $attributes['class'][] = 'js-layout-builder-block-link';
       $link = [
         'title' => $block['admin_label'],
         'url' => Url::fromRoute('layout_builder.add_block',
@@ -206,7 +244,7 @@
             'plugin_id' => $block_id,
           ]
         ),
-        'attributes' => $this->getAjaxAttributes(),
+        'attributes' => $attributes,
       ];
 
       $links[] = $link;