You are here

public function Subscribers::getSubscribers in Message Subscribe 8

Retrieve a list of subscribers for a given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to retrieve subscribers for.

\Drupal\message\MessageInterface $message: The message entity.

array $options: (optional) An array of options with the same elements as the `$subscribe_options` array for `self::sendMessage()`.

array $context: (optional) The context array, passed by reference. This has the same elements as the `$context` paramater for `self::sendMessage()`.

Return value

\Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[] Array of delivery candidate objects keyed with the user IDs to send notifications to.

Overrides SubscribersInterface::getSubscribers

1 call to Subscribers::getSubscribers()
Subscribers::sendMessage in src/Subscribers.php
Process a message and send to subscribed users.

File

src/Subscribers.php, line 272

Class

Subscribers
A message subscribers service.

Namespace

Drupal\message_subscribe

Code

public function getSubscribers(EntityInterface $entity, MessageInterface $message, array $options = [], array &$context = []) {
  $context = !empty($context) ? $context : $this
    ->getBasicContext($entity, !empty($options['skip context']), $context);
  $notify_message_owner = isset($options['notify message owner']) ? $options['notify message owner'] : $this->config
    ->get('notify_own_actions');
  $uids = [];

  // We don't use module_invoke_all() is we want to retain the array keys,
  // which are the user IDs.
  foreach ($this->moduleHandler
    ->getImplementations('message_subscribe_get_subscribers') as $module) {
    $function = $module . '_message_subscribe_get_subscribers';
    $result = $function($message, $options, $context);
    $this
      ->debug('Found @uids from @function', [
      '@uids' => implode(', ', array_keys($result)),
      '@function' => $function,
    ]);
    $uids += $result;
  }

  // If we're not notifying blocked users, exclude those users from the result
  // set now so that we avoid unnecessarily loading those users later.
  if (empty($options['notify blocked users']) && !empty($uids)) {
    $query = $this->entityTypeManager
      ->getStorage('user')
      ->getQuery();
    $results = $query
      ->condition('status', 1)
      ->condition('uid', array_keys($uids), 'IN')
      ->execute();
    if (!empty($results)) {
      $uids = array_intersect_key($uids, $results);
    }
    else {

      // There are no blocked users to notify.
      $uids = [];
    }
  }
  foreach ($uids as $uid => $values) {

    // See if the author of the entity gets notified.
    if (!$notify_message_owner && $this
      ->isEntityOwner($entity, $uid)) {
      $this
        ->debug('Removing @uid from recipient list since they are the entity owner.', [
        '@uid' => $uid,
      ]);
      unset($uids[$uid]);
    }
    if (!empty($options['entity access'])) {
      $account = $this->entityTypeManager
        ->getStorage('user')
        ->load($uid);
      if (!$entity
        ->access('view', $account)) {

        // User doesn't have access to view the entity.
        $this
          ->debug('Removing @uid from recipient list since they do not have view access.', [
          '@uid' => $uid,
        ]);
        unset($uids[$uid]);
      }
    }
  }
  $this
    ->debug('Recipients after access filter and entity owner filter: @uids', [
    '@uids' => implode(', ', array_keys($uids)),
  ]);
  $values = [
    'context' => $context,
    'entity_type' => $entity
      ->getEntityTypeId(),
    'entity' => $entity,
    'message' => $message,
    'subscribe_options' => $options,
  ];
  $this
    ->addDefaultNotifiers($uids);
  $this
    ->debug('Recipient list after default notifiers: @uids', [
    '@uids' => implode(', ', array_keys($uids)),
  ]);
  $this->moduleHandler
    ->alter('message_subscribe_get_subscribers', $uids, $values);
  ksort($uids);
  $this
    ->debug('Recipient list after ksort and alter hook: @uids', [
    '@uids' => implode(', ', array_keys($uids)),
  ]);
  return $uids;
}