You are here

public function UserRole::alterQuery in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Condition/UserRole.php \Drupal\rng\Plugin\Condition\UserRole::alterQuery()
  2. 8 src/Plugin/Condition/UserRole.php \Drupal\rng\Plugin\Condition\UserRole::alterQuery()

Modify a query with condition configuration.

This does not rely on any contexts, only valid configuration.

Parameters

\Drupal\Core\Entity\Query\QueryInterface $query: The query object.

Overrides RNGConditionInterface::alterQuery

File

src/Plugin/Condition/UserRole.php, line 59

Class

UserRole
Provides a user role condition where all roles are matched.

Namespace

Drupal\rng\Plugin\Condition

Code

public function alterQuery(&$query) {
  if ($query
    ->getEntityTypeId() != 'user') {
    throw new \Exception('Query only operates on user entity type.');
  }

  // Ensure roles in configuration are still existing or valid roles.
  $roles = array_intersect_key($this
    ->getRoles(), $this->configuration['roles']);
  if (count($roles)) {
    foreach (array_keys($roles) as $role) {
      $group = $query
        ->andConditionGroup();
      $group
        ->condition('roles', $role, '=');
      $query
        ->condition($group);
    }
  }
}