You are here

public function NotificationsWidgetBlock::build in Notifications widget 8

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/NotificationsWidgetBlock.php, line 115

Class

NotificationsWidgetBlock
Provides a block with list of notification items.

Namespace

Drupal\notifications_widget\Plugin\Block

Code

public function build() {
  $connection = $this->database;
  $config = $this
    ->getConfiguration();

  // Get logged user session.
  $currentUser = $this->currentUser;
  $uid = $currentUser
    ->id();
  $notificationType = 0;
  $totalCount = 0;
  $unreadCount = 0;
  $notificationList = [];
  $clerallQuery = $connection
    ->select('notifications_clear_all', 'nca');
  $clerallQuery
    ->fields('nca');
  $clerallQuery
    ->condition('nca.uid', $uid);
  $clerallQuery
    ->orderBy('nca.id', 'DESC');
  $ncaRes = $clerallQuery
    ->execute()
    ->fetchObject();
  $startingNotiId = isset($ncaRes->notification_id) ? $ncaRes->notification_id : 0;
  $query = $connection
    ->select('notifications', 'n');
  $query
    ->fields('n', [
    'id',
    'message',
  ]);
  $query
    ->condition('n.id', $startingNotiId, '>');
  if (isset($config['block_notification_type']) && $config['block_notification_type'] == 1 && $config['block_notification_logs_display'] == 1) {
    $query
      ->condition('n.entity_uid', $uid);
    $query
      ->condition('n.uid', $uid, '<>');
    $notificationType = 1;
  }
  elseif (isset($config['block_notification_type']) && $config['block_notification_type'] == 1 && $config['block_notification_logs_display'] == 0) {
    $query
      ->condition('n.entity_uid', $uid);
    $notificationType = 1;
  }
  elseif (isset($config['block_notification_type']) && $config['block_notification_type'] == 0 && $config['block_notification_logs_display'] == 1) {
    $query
      ->condition('n.uid', $uid, '<>');
  }
  $query
    ->orderBy('n.created', 'DESC');
  $res = $query
    ->execute();
  while ($notification = $res
    ->fetchObject()) {
    if (!empty($notification->message)) {
      $nasQuery = $connection
        ->select('notifications_actions', 'nas');
      $nasQuery
        ->fields('nas');
      $nasQuery
        ->condition('nas.uid', $uid);
      $nasQuery
        ->condition('nas.notification_id', $notification->id);
      $nasRes = $nasQuery
        ->execute()
        ->fetchObject();
      $nasId = isset($nasRes->id) ? $nasRes->id : '';
      $status = isset($nasRes->status) ? $nasRes->status : 0;
      if ($status == 2) {
        continue;
      }
      $notificationList[] = [
        'id' => $notification->id,
        'nas_id' => $nasId,
        'message' => $notification->message,
        'status' => $status,
      ];
      if ($status == 0) {
        $unreadCount++;
      }
      $totalCount++;
    }
  }
  return [
    '#theme' => 'notifications_widget',
    '#uid' => $uid,
    '#notification_type' => $notificationType,
    '#total' => $totalCount,
    '#unread' => $unreadCount,
    '#notification_list' => $notificationList,
  ];
}