You are here

public function DraggableDashboardController::listBlocks in Draggable dashboard 8.2

Shows a list of blocks that can be added to a theme's layout.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

array A render array as expected by the renderer.

1 string reference to 'DraggableDashboardController::listBlocks'
draggable_dashboard.routing.yml in ./draggable_dashboard.routing.yml
draggable_dashboard.routing.yml

File

src/Controller/DraggableDashboardController.php, line 77

Class

DraggableDashboardController
Controller routines for draggable dashboards.

Namespace

Drupal\draggable_dashboard\Controller

Code

public function listBlocks(Request $request, DashboardEntityInterface $dashboard_entity, $region) {
  $headers = [
    [
      'data' => $this
        ->t('Block'),
    ],
    [
      'data' => $this
        ->t('Category'),
    ],
    [
      'data' => $this
        ->t('Operations'),
    ],
  ];

  // Only add blocks which work without any available context.
  $definitions = $this->blockManager
    ->getFilteredDefinitions('block_ui', $this->contextRepository
    ->getAvailableContexts(), [
    '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['category']['data'] = $plugin_definition['category'];
    $links['add'] = [
      'title' => $this
        ->t('Place block'),
      'url' => Url::fromRoute('draggable_dashboard.block_add', [
        'dashboard_entity' => $dashboard_entity
          ->id(),
        'plugin_id' => $plugin_id,
        'region' => $region,
      ]),
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
    ];
    $row['operations']['data'] = [
      '#type' => 'operations',
      '#links' => $links,
    ];
    $rows[] = $row;
  }
  $build['#attached']['library'][] = 'block/drupal.block.admin';
  $build['filter'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => $this
      ->t('Filter by block name'),
    '#attributes' => [
      'class' => [
        'block-filter-text',
      ],
      'data-element' => '.block-add-table',
      'title' => $this
        ->t('Enter a part of the block name to filter by.'),
    ],
  ];
  $build['blocks'] = [
    '#type' => 'table',
    '#header' => $headers,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No blocks available.'),
    '#attributes' => [
      'class' => [
        'block-add-table',
      ],
    ],
  ];
  return $build;
}