You are here

public function PrivateMessageNotificationBlock::build in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/PrivateMessageNotificationBlock.php \Drupal\private_message\Plugin\Block\PrivateMessageNotificationBlock::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/PrivateMessageNotificationBlock.php, line 89

Class

PrivateMessageNotificationBlock
Provides the private message notification block.

Namespace

Drupal\private_message\Plugin\Block

Code

public function build() {
  if ($this->currentUser
    ->isAuthenticated() && $this->currentUser
    ->hasPermission('use private messaging system')) {
    $block = [
      '#theme' => 'private_message_notification_block',
      '#new_message_count' => $this->privateMessageService
        ->getUnreadThreadCount(),
    ];
    $url = Url::fromRoute('private_message.ajax_callback', [
      'op' => 'get_new_unread_thread_count',
    ]);
    $token = $this->csrfToken
      ->get($url
      ->getInternalPath());
    $url
      ->setOptions([
      'absolute' => TRUE,
      'query' => [
        'token' => $token,
      ],
    ]);
    $block['#attached']['drupalSettings']['privateMessageNotificationBlock']['newMessageCountCallback'] = $url
      ->toString();
    $config = $this
      ->getConfiguration();
    $block['#attached']['drupalSettings']['privateMessageNotificationBlock']['ajaxRefreshRate'] = $config['ajax_refresh_rate'];
    $block['#attached']['library'][] = 'private_message/notification_block';

    // Add the default classes, as these are not added when the block output
    // is overridden with a template.
    $block['#attributes']['class'][] = 'block';
    $block['#attributes']['class'][] = 'block-private-message';
    $block['#attributes']['class'][] = 'block-private-message-notification-block';
    return $block;
  }
}