class UserHasRole in Business Rules 8
Same name and namespace in other branches
- 2.x src/Plugin/BusinessRulesCondition/UserHasRole.php \Drupal\business_rules\Plugin\BusinessRulesCondition\UserHasRole
Class UserHasRole.
@package Drupal\business_rules\Plugin\BusinessRulesCondition
Plugin annotation
@BusinessRulesCondition(
id = "user_has_role",
label = @Translation("Logged user has role"),
group = @Translation("User"),
description = @Translation("Check if logged user has role."),
isContextDependent = FALSE,
reactsOnIds = {},
hasTargetEntity = FALSE,
hasTargetBundle = FALSE,
hasTargetField = FALSE,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase implements BusinessRulesItemPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesConditionPlugin implements BusinessRulesConditionPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesCondition\UserHasRole
- class \Drupal\business_rules\Plugin\BusinessRulesConditionPlugin implements BusinessRulesConditionPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase implements BusinessRulesItemPluginInterface
Expanded class hierarchy of UserHasRole
1 file declares its use of UserHasRole
File
- src/
Plugin/ BusinessRulesCondition/ UserHasRole.php, line 28
Namespace
Drupal\business_rules\Plugin\BusinessRulesConditionView source
class UserHasRole extends BusinessRulesConditionPlugin {
/**
* {@inheritdoc}
*/
public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
$settings['roles'] = [
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#required' => TRUE,
'#options' => $this->util
->getUserRolesOptions(),
'#default_value' => is_array($item
->getSettings('roles')) ? $item
->getSettings('roles') : [],
];
$settings['criteria'] = [
'#type' => 'select',
'#title' => t('Match criteria'),
'#description' => t('The condition will check if current user has all selected roles or at least one role?'),
'#required' => TRUE,
'#default_value' => $item
->getSettings('criteria'),
'#options' => [
'all' => t('All roles'),
'one' => t('At least one role'),
],
];
return $settings;
}
/**
* {@inheritdoc}
*/
public function processSettings(array $settings, ItemInterface $item) {
foreach ($settings['roles'] as $key => $role) {
if ($role === 0) {
unset($settings['roles'][$key]);
}
}
return $settings;
}
/**
* {@inheritdoc}
*/
public function process(ConditionInterface $condition, BusinessRulesEvent $event) {
$roles = $condition
->getSettings('roles');
$criteria = $condition
->getSettings('criteria');
/** @var \Drupal\user\Entity\User $current_user */
$current_user = $this->util->container
->get('current_user');
$user_roles = array_values($current_user
->getRoles());
$result = FALSE;
if (is_array($roles) && is_array($user_roles)) {
if ($criteria == 'all') {
$result = count(array_intersect($roles, $user_roles)) == count($roles);
}
elseif ($criteria == 'one') {
$result = !empty(array_intersect($user_roles, $roles));
}
}
return $result;
}
}