You are here

public function Role::getRecipients in Mass Contact 8

Retrieve the list of users by category.

Parameters

array $categories: An array of category IDs for which to retrieve users. For instance, in the role grouping this would be an array of role IDs.

Return value

int[] An array of recipient user IDs.

Overrides GroupingInterface::getRecipients

File

src/Plugin/MassContact/GroupingMethod/Role.php, line 93

Class

Role
Select users by their role.

Namespace

Drupal\mass_contact\Plugin\MassContact\GroupingMethod

Code

public function getRecipients(array $categories) {
  if ($this->configuration['conjunction'] === 'OR') {
    $query = $this->entityTypeManager
      ->getStorage('user')
      ->getQuery();
    $query
      ->condition('status', 1);

    // If the authenticated role is not included, add the appropriate filters.
    // Otherwise, return all active users.
    if (!in_array(RoleInterface::AUTHENTICATED_ID, $categories)) {
      $query
        ->condition('roles', $categories, 'IN');
    }
    return $query
      ->execute();
  }
  else {

    // Must have all the roles if conjunction is set to AND.
    // Note that entity query doesn't appear to be able to handle multiple
    // conditions against the same field.
    $results = [];
    foreach ($categories as $id) {
      $query = $this->entityTypeManager
        ->getStorage('user')
        ->getQuery();
      $query
        ->condition('status', 1);
      if ($id !== RoleInterface::AUTHENTICATED_ID) {
        $query
          ->condition('roles', $id);
      }
      $results[$id] = $query
        ->execute();
    }
    return count($results) > 1 ? call_user_func_array('array_intersect', $results) : reset($results);
  }
}