You are here

public function SiteHeaderBlock::build in Opigno dashboard 3.x

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

1 method overrides SiteHeaderBlock::build()
UserStatisticsBlock::build in src/Plugin/Block/UserStatisticsBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/SiteHeaderBlock.php, line 126

Class

SiteHeaderBlock
The site header block.

Namespace

Drupal\opigno_dashboard\Plugin\Block

Code

public function build() {

  // Don't cache if the user can't be loaded.
  if (!$this->user instanceof UserInterface) {
    return [
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }

  // Get the logo path and the main menu.
  $role = $this->statsManager
    ->getUserRole();
  $logo = theme_get_setting('logo.url');
  $parameters = $this->menuTree
    ->getCurrentRouteMenuTreeParameters('main');
  $tree = $this->menuTree
    ->load('main', $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $menu = $this->menuTree
    ->transform($tree, $manipulators);

  // Notifications.
  try {
    $notifications = $this->notificationsManager
      ->getUserHeaderNotifications();
  } catch (InvalidPluginDefinitionException|PluginNotFoundException $e) {
    watchdog_exception('opigno_dashboard_exception', $e);
    $notifications = [];
  }
  return [
    '#theme' => 'opigno_site_header',
    '#logo' => $logo,
    '#menu' => $this->menuTree
      ->build($menu),
    '#is_anonymous' => $this->user
      ->isAnonymous(),
    '#is_user_page' => $this->isUserPage,
    '#user_name' => $this->user
      ->getDisplayName(),
    '#user_url' => Url::fromRoute('entity.user.canonical', [
      'user' => (int) $this->user
        ->id(),
    ])
      ->toString(),
    '#user_picture' => UserStatisticsManager::getUserPicture($this->user),
    '#notifications_count' => count($notifications),
    '#notifications' => $this->notificationsManager
      ->renderUserHeaderNotifications($notifications),
    '#messages_count' => $this->pmService
      ->getUnreadThreadCount(),
    '#dropdown_menu' => $this
      ->buildUserDropdownMenu($role),
    '#cache' => [
      'contexts' => $this
        ->getCacheContexts(),
      'tags' => $this
        ->getCacheTags(),
    ],
    '#attached' => [
      'library' => [
        'opigno_notification/opigno_notification',
      ],
      'drupalSettings' => [
        'opignoNotifications' => [
          'updateUrl' => Url::fromRoute('opigno_notification.get_messages')
            ->toString(),
        ],
      ],
    ],
  ];
}