Commit 1150b503 authored by Peter Droogmans's avatar Peter Droogmans
Browse files

Issue #3131545 by attiks: Show and filter machine name on place block

parent ff89485b
Loading
Loading
Loading
Loading
+5
Changes for dream_block_manager.services.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
services:
  dream_block_manager.route_subscriber:
    class: Drupal\dream_block_manager\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }
+3 −3
Changes for js/dream_block_manager.admin.js: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
(function ($, Drupal, debounce) {
  Drupal.behaviors.extendedBlockManagerFilter = {
    attach: function attach(context, settings) {
      var $input = $('[name="block_filter"]').once('block_filter');
      var $table = $('#blocks');
      var $input = $('input.block-filter-query').once('block-filter-query');
      var $table = $($input.attr('data-element'));

      function filterRows(e) {
        var query = $(e.target).val().toLowerCase();

        if (query.length >= 2) {
          // Select 4 first td to match.
          $table.find('tr.draggable').each(function ($index, row) {
          $table.find('tbody tr:not(.region-title)').each(function ($index, row) {
            if ($(row).find('td:lt(4)').text().toLowerCase().indexOf(query) === -1) {
              row.classList.add('filtered-out');
            }
+13 −2
Changes for src/BlockFilterListBuilder.php: 13 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\dream_block_manager;

use Drupal\block\BlockListBuilder;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

@@ -19,9 +20,15 @@ class BlockFilterListBuilder extends BlockListBuilder {
    $form['#attached']['library'][] = 'dream_block_manager/dream_block_manager.admin';

    $form['block_filter'] = [
      '#type' => 'textfield',
      '#title' => 'Filter',
      '#type' => 'search',
      '#weight' => -100,
      '#title' => $this->t('Filter'),
      '#size' => 30,
      '#placeholder' => $this->t('Filter'),
      '#attributes' => [
        'class' => ['block-filter-query'],
        'data-element' => '#blocks',
      ],
    ];

    return $form;
@@ -106,6 +113,10 @@ class BlockFilterListBuilder extends BlockListBuilder {
      array_splice($form['#header'], 2, 0, [$this->t('Path')]);
      // Increase colspan.
      foreach (Element::children($form) as $child) {
        // Change modal width.
        if (isset($form[$child]['title']['#attributes']['data-dialog-options'])) {
          $form[$child]['title']['#attributes']['data-dialog-options'] = Json::encode(['width' => 1100]);
        }
        foreach (Element::children($form[$child]) as $gchild) {
          if (isset($form[$child][$gchild]['#wrapper_attributes']['colspan'])) {
            $form[$child][$gchild]['#wrapper_attributes']['colspan'] =
+137
Changes for src/Controller/BlockPlaceBlockController.php: 137 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\dream_block_manager\Controller;

use Drupal\block\Controller\BlockLibraryController;

use Drupal\Component\Serialization\Json;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;

/**
 * Provides a list of block plugins to be added to the layout.
 */
class BlockPlaceBlockController extends BlockLibraryController {

  /**
   * Shows a list of blocks that can be added to a theme's layout.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param string $theme
   *   Theme key of the block list.
   *
   * @return array
   *   A render array as expected by the renderer.
   */
  public function listBlocks(Request $request, $theme) {
    // Since modals do not render any other part of the page, we need to render
    // them manually as part of this listing.
    if ($request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT) === 'drupal_modal') {
      $build['local_actions'] = $this->buildLocalActions();
    }

    $headers = [
      ['data' => $this->t('Block')],
      ['data' => $this->t('Machine name')],
      ['data' => $this->t('Category')],
      ['data' => $this->t('Operations')],
    ];

    $region = $request->query->get('region');
    $weight = $request->query->get('weight');

    // Only add blocks which work without any available context.
    $definitions = $this->blockManager->getFilteredDefinitions('block_ui', $this->contextRepository->getAvailableContexts(), [
      'theme' => $theme,
      'region' => $region,
    ]);
    // Order by category, and then by admin label.
    $definitions = $this->blockManager->getSortedDefinitions($definitions);
    // Filter out definitions that are not intended to be placed by the UI.
    $definitions = array_filter($definitions, function (array $definition) {
      return empty($definition['_block_ui_hidden']);
    });

    $rows = [];
    foreach ($definitions as $plugin_id => $plugin_definition) {
      $row = [];
      $row['title']['data'] = [
        '#type' => 'inline_template',
        '#template' => '<div class="block-filter-text-source">{{ label }}</div>',
        '#context' => [
          'label' => $plugin_definition['admin_label'],
        ],
      ];
      $row['machine_name']['data'] = $plugin_id;
      $row['category']['data'] = $plugin_definition['category'];
      $links['add'] = [
        'title' => $this->t('Place block'),
        'url' => Url::fromRoute('block.admin_add', ['plugin_id' => $plugin_id, 'theme' => $theme]),
        'attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 1100,
          ]),
        ],
      ];
      if ($region) {
        $links['add']['query']['region'] = $region;
      }
      if (isset($weight)) {
        $links['add']['query']['weight'] = $weight;
      }
      $row['operations']['data'] = [
        '#type' => 'operations',
        '#links' => $links,
      ];
      $rows[] = $row;
    }

    $build['#attached']['library'][] = 'dream_block_manager/dream_block_manager.admin';

    $build['block_filter'] = [
      '#type' => 'search',
      '#title' => $this->t('Filter'),
      '#title_display' => 'invisible',
      '#size' => 30,
      '#placeholder' => $this->t('Filter'),
      '#attributes' => [
        'class' => ['block-filter-query'],
        'data-element' => '.block-add-table',
      ],
    ];

    $build['blocks'] = [
      '#type' => 'table',
      '#header' => $headers,
      '#rows' => $rows,
      '#empty' => $this->t('No blocks available.'),
      '#attributes' => [
        'class' => ['block-add-table'],
      ],
    ];

    return $build;
  }

  /**
   * Builds the local actions for this listing.
   *
   * @return array
   *   An array of local actions for this listing.
   */
  protected function buildLocalActions() {
    $build = $this->localActionManager->getActionsForRoute($this->routeMatch->getRouteName());
    // Without this workaround, the action links will be rendered as <li> with
    // no wrapping <ul> element.
    if (!empty($build)) {
      $build['#prefix'] = '<ul class="action-links">';
      $build['#suffix'] = '</ul>';
    }
    return $build;
  }

}
+22
Changes for src/Routing/RouteSubscriber.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\dream_block_manager\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('block.admin_library')) {
      $route->setDefault('_controller', '\Drupal\dream_block_manager\Controller\BlockPlaceBlockController::listBlocks');
    }
  }

}