You are here

DraggableDashboardController.php in Draggable dashboard 8.2

Same filename and directory in other branches
  1. 8 src/Controller/DraggableDashboardController.php

File

src/Controller/DraggableDashboardController.php
View source
<?php

namespace Drupal\draggable_dashboard\Controller;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Plugin\Context\LazyContextRepository;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Core\Url;
use Drupal\draggable_dashboard\Entity\DashboardEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Controller routines for draggable dashboards.
 */
class DraggableDashboardController extends ControllerBase {

  /**
   * The block manager.
   *
   * @var \Drupal\Core\Block\BlockManagerInterface
   */
  protected $blockManager;

  /**
   * The context repository.
   *
   * @var \Drupal\Core\Plugin\Context\LazyContextRepository
   */
  protected $contextRepository;

  /**
   * The plugin form manager.
   *
   * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
   */
  protected $pluginFormFactory;

  /**
   * DraggableDashboardController constructor.
   *
   * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
   *   The block manager.
   * @param \Drupal\Core\Plugin\Context\LazyContextRepository $context_repository
   */
  public function __construct(BlockManagerInterface $block_manager, LazyContextRepository $context_repository, PluginFormFactoryInterface $plugin_form_manager) {
    $this->blockManager = $block_manager;
    $this->contextRepository = $context_repository;
    $this->pluginFormFactory = $plugin_form_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.block'), $container
      ->get('context.repository'), $container
      ->get('plugin_form.factory'));
  }

  /**
   * Shows a list of blocks that can be added to a theme's layout.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return array
   *   A render array as expected by the renderer.
   */
  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;
  }

}

Classes

Namesort descending Description
DraggableDashboardController Controller routines for draggable dashboards.