UserRole.php in RNG - Events and Registrations 8
File
src/Plugin/Condition/UserRole.php
View source
<?php
namespace Drupal\rng\Plugin\Condition;
use Drupal\user\Plugin\Condition\UserRole as CoreUserRole;
use Drupal\rng\RNGConditionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;
class UserRole extends CoreUserRole implements RNGConditionInterface {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['roles']['#title'] = $this
->t('When the user has all of the following roles');
$form['roles']['#options'] = array_map('\\Drupal\\Component\\Utility\\SafeMarkup::checkPlain', $this
->getRoles());
$form['roles']['#description'] = $this
->t('If you select no roles, the condition will evaluate to TRUE for all logged-in users.');
$form['negate']['#access'] = FALSE;
return $form;
}
public function summary() {
$roles = array_intersect_key($this
->getRoles(), $this->configuration['roles']);
if (!count($roles)) {
return $this
->t('Any registered user');
}
return $this
->t(empty($this->configuration['negate']) ? 'User is a member of @roles' : 'User is not a member of @roles', [
'@roles' => count($roles) > 1 ? implode(' and ', $roles) : reset($roles),
]);
}
function alterQuery(&$query) {
if ($query
->getEntityTypeId() != 'user') {
throw new \Exception('Query only operates on user entity type.');
}
$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);
}
}
}
private function getRoles() {
$options = [];
foreach (Role::loadMultiple() as $role) {
if ($role
->getThirdPartySetting('rng', 'condition_rng_role', FALSE)) {
$options[$role
->id()] = $role
->label();
}
}
if (!count($options)) {
$options = user_role_names(TRUE);
}
unset($options[AccountInterface::ANONYMOUS_ROLE]);
unset($options[AccountInterface::AUTHENTICATED_ROLE]);
return $options;
}
}
Classes
Name |
Description |
UserRole |
Provides a user role condition where all roles are matched. |