You are here

public function DraggableBlock::build in Draggable dashboard 8

Same name and namespace in other branches
  1. 8.2 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 145

Class

DraggableBlock
Provides a draggable block with a simple text.

Namespace

Drupal\draggable_dashboard\Plugin\Block

Code

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

    // Create dashboard columns.
    for ($i = 1; $i <= $this->dashboard
      ->get('columns'); $i++) {
      $blocks = [];
      if (!empty($all_blocks)) {
        foreach ($all_blocks as $relation) {
          if ($relation['cln'] == $i) {
            $blocks[] = $relation;
          }
        }
      }
      if (!empty($blocks)) {
        if ($max_blocks < count($blocks)) {
          $max_blocks = count($blocks);
        }
        foreach ($blocks as $relation) {

          /** @var \Drupal\block\BlockInterface $block */
          $block = $this->entityTypeManager
            ->getStorage('block')
            ->load($relation['bid']);
          if (empty($block)) {
            continue;
          }

          // You can hard code configuration or you load from settings.
          $config = $block
            ->getPlugin()
            ->getConfiguration();
          $isTitleVisible = empty($config['label_display']) ? FALSE : TRUE;
          $config['label_display'] = 0;
          $plugin_block = $this->blockManager
            ->createInstance($block
            ->getPluginId(), $config);
          if ($plugin_block instanceof TitleBlockPluginInterface) {
            $pageTitle = $this->titleResolver
              ->getTitle($this->requestStack
              ->getCurrentRequest(), $this->routeMatch
              ->getRouteObject());
            if ($pageTitle) {
              $plugin_block
                ->setTitle($pageTitle);
            }
          }

          // Some blocks might implement access check.
          // Return empty render array if user doesn't have access.
          if ($plugin_block
            ->access($this->currentUser)) {
            $render = $this->entityTypeManager
              ->getViewBuilder('block')
              ->view($block);
            if (!isset($render['#lazy_builder'])) {
              unset($render['#pre_render']);
              $content = $plugin_block
                ->build();
              $render['content'] = $content;
            }
            else {
              unset($render['#lazy_builder']);
              $content = $plugin_block
                ->build();
              $render['content'] = $content;
            }
            $columns[$i][] = [
              'id' => $relation['bid'],
              'title' => $isTitleVisible ? $config['label'] : '',
              'view' => [
                'data' => $render,
              ],
            ];
          }
        }
      }
    }
  }
  $build = [
    '#theme' => 'draggable_dashboard_block',
    '#dashboard' => $this->dashboard,
    '#columns' => $columns,
    '#cache' => [
      'max-age' => 0,
    ],
    '#attached' => [
      'library' => [
        'draggable_dashboard/draggable_dashboard.frontend',
      ],
    ],
  ];
  if ($this->currentUser
    ->hasPermission('administer_draggable_dashboard')) {
    $build['#attached']['library'][] = 'draggable_dashboard/draggable_dashboard.draggable';
  }
  return $build;
}