You are here

public function OptOut::getOptOutAccounts in Mass Contact 8

Finds a list of users that have opted out of emails.

Parameters

\Drupal\mass_contact\Entity\MassContactCategoryInterface[] $categories: An array of categories.

Return value

int[] An array of account IDs that have opted out. This includes global opt-out as well. If opt-outs are disabled, this method always returns an empty array.

Overrides OptOutInterface::getOptOutAccounts

File

src/OptOut.php, line 44

Class

OptOut
The user opt out service.

Namespace

Drupal\mass_contact

Code

public function getOptOutAccounts(array $categories = []) {

  // Get the latest configs.
  $config = $this->configFactory
    ->get('mass_contact.settings');
  if ($config
    ->get('optout_enabled') === MassContactInterface::OPT_OUT_DISABLED) {

    // Opt-out is completely disabled, return empty.
    return [];
  }
  $query = $this->entityManager
    ->getStorage('user')
    ->getQuery();
  $query
    ->condition('status', 1);
  if ($config
    ->get('optout_enabled') === MassContactInterface::OPT_OUT_GLOBAL) {

    // Any user with a value here has opted out.
    $query
      ->condition(MassContactInterface::OPT_OUT_FIELD_ID, 0, '<>');
  }
  else {
    $category_ids = array_map(function (MassContactCategoryInterface $category) {
      return $category
        ->id();
    }, $categories);
    $group = $query
      ->orConditionGroup()
      ->condition(MassContactInterface::OPT_OUT_FIELD_ID, $category_ids, 'IN')
      ->condition(MassContactInterface::OPT_OUT_FIELD_ID, '1');
    $query
      ->condition($group);
  }
  return $query
    ->execute();
}