You are here

public function DraggableBlock::build in Draggable dashboard 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Block/DraggableBlock.php \Drupal\draggable_dashboard\Plugin\Block\DraggableBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/DraggableBlock.php, line 137

Class

DraggableBlock
Provides a draggable block with a simple text.

Namespace

Drupal\draggable_dashboard\Plugin\Block

Code

public function build() {
  $dashboard = $this
    ->getDashboard();
  $columns = [];
  if (!empty($dashboard)) {
    $all_blocks = $dashboard
      ->get('blocks');
    $max_blocks = 0;

    // Create dashboard columns.
    for ($i = 1; $i <= $dashboard
      ->get('columns'); $i++) {
      $blocks = [];
      if (!empty($all_blocks)) {
        foreach ($all_blocks as $key => $relation) {
          if ($relation['column'] == $i) {
            $blocks[] = $relation + [
              'block_id' => $key,
            ];
          }
        }
      }
      if (!empty($blocks)) {
        if ($max_blocks < count($blocks)) {
          $max_blocks = count($blocks);
        }
        foreach ($blocks as $delta => $relation) {
          $block_instance = $this->blockManager
            ->createInstance($relation['settings']['id'], $relation['settings']);
          if ($block_instance instanceof TitleBlockPluginInterface) {
            $pageTitle = $this->titleResolver
              ->getTitle($this->requestStack
              ->getCurrentRequest(), $this->routeMatch
              ->getRouteObject());
            if ($pageTitle) {
              $block_instance
                ->setTitle($pageTitle);
            }
          }

          // Some blocks might implement access check.
          // Return empty render array if user doesn't have access.
          if ($block_instance
            ->access($this->currentUser)) {

            // See \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
            // See template_preprocess_block()
            $element = [
              '#theme' => 'block',
              '#attributes' => [
                'id' => "block-" . $relation['block_id'],
                'data-id' => $relation['block_id'],
              ],
              '#configuration' => $block_instance
                ->getConfiguration(),
              '#plugin_id' => $block_instance
                ->getPluginId(),
              '#base_plugin_id' => $block_instance
                ->getBaseId(),
              '#derivative_plugin_id' => $block_instance
                ->getDerivativeId(),
              '#draggable_dashboard' => true,
              'content' => $block_instance
                ->build(),
              'title' => [
                '#markup' => $block_instance
                  ->label(),
              ],
              'id' => [
                '#markup' => $relation['block_id'],
              ],
            ];
            CacheableMetadata::createFromRenderArray($element)
              ->merge(CacheableMetadata::createFromRenderArray($element['content']))
              ->addCacheableDependency($block_instance)
              ->applyTo($element);
            $columns[$i][] = $element;
          }
        }
      }
    }
  }
  return [
    '#theme' => 'draggable_dashboard_block',
    '#attributes' => [
      'data-id' => $dashboard
        ->id(),
    ],
    '#dashboard' => $dashboard,
    '#columns' => $columns,
    '#attached' => [
      'library' => [
        'draggable_dashboard/frontend',
        'draggable_dashboard/draggable',
      ],
    ],
  ];
}