Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/Plugin/Condition/UserRole.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\Plugin\Condition; | |
4 | |
5 use Drupal\Core\Condition\ConditionPluginBase; | |
6 use Drupal\Core\Form\FormStateInterface; | |
7 | |
8 /** | |
9 * Provides a 'User Role' condition. | |
10 * | |
11 * @Condition( | |
12 * id = "user_role", | |
13 * label = @Translation("User Role"), | |
14 * context = { | |
15 * "user" = @ContextDefinition("entity:user", label = @Translation("User")) | |
16 * } | |
17 * ) | |
18 */ | |
19 class UserRole extends ConditionPluginBase { | |
20 | |
21 /** | |
22 * {@inheritdoc} | |
23 */ | |
24 public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
25 $form['roles'] = [ | |
26 '#type' => 'checkboxes', | |
27 '#title' => $this->t('When the user has the following roles'), | |
28 '#default_value' => $this->configuration['roles'], | |
29 '#options' => array_map('\Drupal\Component\Utility\Html::escape', user_role_names()), | |
30 '#description' => $this->t('If you select no roles, the condition will evaluate to TRUE for all users.'), | |
31 ]; | |
32 return parent::buildConfigurationForm($form, $form_state); | |
33 } | |
34 | |
35 /** | |
36 * {@inheritdoc} | |
37 */ | |
38 public function defaultConfiguration() { | |
39 return [ | |
40 'roles' => [], | |
41 ] + parent::defaultConfiguration(); | |
42 } | |
43 | |
44 /** | |
45 * {@inheritdoc} | |
46 */ | |
47 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
48 $this->configuration['roles'] = array_filter($form_state->getValue('roles')); | |
49 parent::submitConfigurationForm($form, $form_state); | |
50 } | |
51 | |
52 /** | |
53 * {@inheritdoc} | |
54 */ | |
55 public function summary() { | |
56 // Use the role labels. They will be sanitized below. | |
57 $roles = array_intersect_key(user_role_names(), $this->configuration['roles']); | |
58 if (count($roles) > 1) { | |
59 $roles = implode(', ', $roles); | |
60 } | |
61 else { | |
62 $roles = reset($roles); | |
63 } | |
64 if (!empty($this->configuration['negate'])) { | |
65 return $this->t('The user is not a member of @roles', ['@roles' => $roles]); | |
66 } | |
67 else { | |
68 return $this->t('The user is a member of @roles', ['@roles' => $roles]); | |
69 } | |
70 } | |
71 | |
72 /** | |
73 * {@inheritdoc} | |
74 */ | |
75 public function evaluate() { | |
76 if (empty($this->configuration['roles']) && !$this->isNegated()) { | |
77 return TRUE; | |
78 } | |
79 $user = $this->getContextValue('user'); | |
80 return (bool) array_intersect($this->configuration['roles'], $user->getRoles()); | |
81 } | |
82 | |
83 /** | |
84 * {@inheritdoc} | |
85 */ | |
86 public function getCacheContexts() { | |
87 // Optimize cache context, if a user cache context is provided, only use | |
88 // user.roles, since that's the only part this condition cares about. | |
89 $contexts = []; | |
90 foreach (parent::getCacheContexts() as $context) { | |
91 $contexts[] = $context == 'user' ? 'user.roles' : $context; | |
92 } | |
93 return $contexts; | |
94 } | |
95 | |
96 } |