You are here

public function OpignoNotificationManager::renderUserHeaderNotifications in Opigno notifications 3.x

Prepare the render array to display the list of user notifications.

Parameters

array $entities: The list of notification entities to be rendered. If empty, notifications will be gathered automatically.

Return value

array Render array to display the list of user notifications.

File

src/Services/OpignoNotificationManager.php, line 80

Class

OpignoNotificationManager
Opigno notification manager service.

Namespace

Drupal\opigno_notification\Services

Code

public function renderUserHeaderNotifications(array $entities = []) : array {
  $notifications = [];
  $entities = $entities ?: $this
    ->getUserHeaderNotifications();
  if (!$entities) {
    return $notifications;
  }

  // Count standard notifications.
  $notifications_count = 0;
  $options = [
    'attributes' => [
      'class' => [
        'notification-item-text',
      ],
    ],
  ];
  foreach ($entities as $entity) {
    if (!$entity instanceof EntityInterface) {
      continue;
    }
    $entity_type = $entity
      ->getEntityTypeId();
    if ($entity instanceof OpignoNotificationInterface) {
      $url = Url::fromUserInput($entity
        ->getUrl(), $options);
      $title = $entity
        ->getMessage();
      $notifications_count++;
    }
    else {
      $title = $entity
        ->getTitle();
      $url = Url::fromRoute("entity.{$entity_type}.canonical", [
        $entity_type => (int) $entity
          ->id(),
      ], $options);
    }
    $notifications[] = Link::fromTextAndUrl($title, $url);
  }
  return [
    '#theme' => 'opigno_notifications_header_dropdown',
    '#notifications' => $notifications,
    '#notifications_count' => $notifications_count,
  ];
}