You are here

public function DashboardViewBuilder::view in Dashboards with Layout Builder 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/DashboardViewBuilder.php \Drupal\dashboards\Entity\DashboardViewBuilder::view()

View entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Entity to display.

string $view_mode: View mode.

string $langcode: Language code.

Overrides EntityViewBuilder::view

File

src/Entity/DashboardViewBuilder.php, line 115

Class

DashboardViewBuilder
Dashboard Entity ViewBuilder.

Namespace

Drupal\dashboards\Entity

Code

public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {

  /**
   * @var \Drupal\dashboards\Entity\Dashboard $entity
   */
  $build_list = $this
    ->viewMultiple([
    $entity,
  ], $view_mode, $langcode);
  $build = $build_list[0];
  $build['#pre_render'][] = [
    $this,
    'build',
  ];
  $contexts[Dashboard::CONTEXT_TYPE] = $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
  $cacheability = new CacheableMetadata();
  $storage = $this->sectionStorageManager
    ->findByContext($contexts, $cacheability);
  if (is_a($storage, UserDashboardSectionStorage::class)) {
    $entity
      ->loadOverrides();
    $contexts[Dashboard::CONTEXT_TYPE] = $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
  }
  $this->moduleHandler
    ->alter('layout_builder_view_context', $contexts, $storage);
  $build = [];
  foreach ($storage
    ->getSections() as $delta => $section) {
    try {
      $build['sections'][$delta] = $section
        ->toRenderArray($contexts);
    } catch (ContextException $ex) {
    }
  }
  return [
    '#attached' => [
      'library' => [
        'dashboards/core',
      ],
      'drupalSettings' => [
        'dashboards' => [
          'colormap' => $this->config
            ->get('colormap') ? $this->config
            ->get('colormap') : 'summer',
          'alpha' => $this->config
            ->get('alpha') ? $this->config
            ->get('alpha') / 100 : 80,
          'shades' => $this->config
            ->get('shades') ? $this->config
            ->get('shades') : 15,
        ],
      ],
    ],
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'dashboards-container',
      ],
    ],
    'content' => $build,
    '#cache' => [
      'tags' => $entity
        ->getCacheTags(),
      'context' => $entity
        ->getCacheContexts(),
    ],
  ];
}