Loading context_profile_role.info.yml +2 −1 Original line number Diff line number Diff line name: 'Context Profile Role' type: module description: 'Provide block condition on the role of the user profile viewed.' core_version_requirement: ^8.8 || ^9 core_version_requirement: ^9.2 || ^10 package: 'Context' php: 7.4 dependencies: - drupal:user src/ContextProvider/UserProfileRouteContext.php +3 −4 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; * Sets the user profile as a context on user routes. */ class UserProfileRouteContext implements ContextProviderInterface { use StringTranslationTrait; /** Loading @@ -24,10 +23,10 @@ class UserProfileRouteContext implements ContextProviderInterface { * * @var \Drupal\Core\Routing\RouteMatchInterface */ protected $routeMatch; protected RouteMatchInterface $routeMatch; /** * Constructs a new NodeRouteContext. * Constructor. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object. Loading @@ -44,7 +43,7 @@ class UserProfileRouteContext implements ContextProviderInterface { $context_definition = EntityContextDefinition::create('user')->setRequired(FALSE); $value = NULL; $route_object = $this->routeMatch->getRouteObject(); if (!is_null($route_object)) { if ($route_object !== NULL) { /** @var array $route_contexts */ $route_contexts = $route_object->getOption('parameters'); Loading src/Plugin/Condition/UserProfileRole.php +19 −21 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\user\RoleStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** Loading @@ -18,7 +19,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * id = "user_profile_role", * label = @Translation("User Profile Role"), * context_definitions = { * "user_profile" = @ContextDefinition("entity:user", label = @Translation("User Profile Role")) * "user_profile": @ContextDefinition("entity:user", label = @Translation("User Profile Role")) * }, * ); */ Loading @@ -29,16 +30,13 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu * * @var \Drupal\user\RoleStorageInterface */ protected $roleStorage; protected RoleStorageInterface $roleStorage; /** * Creates a new UserRole instance. * Constructor. * * @param array $configuration * The plugin configuration, i.e. an array with configuration values keyed * by configuration option name. The special key 'context' may be used to * initialize the defined contexts by setting it to an array of context * values keyed by context names. * The plugin configuration. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition Loading Loading @@ -94,7 +92,7 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { /** @var array $form_state_roles_value */ $form_state_roles_value = $form_state->getValue('roles'); $this->configuration['roles'] = array_filter($form_state_roles_value); $this->configuration['roles'] = \array_filter($form_state_roles_value); parent::submitConfigurationForm($form, $form_state); } Loading @@ -102,16 +100,16 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu * {@inheritdoc} */ public function summary(): MarkupInterface { if (count($this->configuration['roles']) > 1) { if (\count($this->configuration['roles']) > 1) { $roles = $this->configuration['roles']; $last = array_pop($roles); $roles = implode(', ', $roles); $last = \array_pop($roles); $roles = \implode(', ', $roles); return $this->t( 'The user role is @roles or @last', ['@roles' => $roles, '@last' => $last] ); } $bundle = reset($this->configuration['roles']); $bundle = \reset($this->configuration['roles']); return $this->t('The user role is @bundle', ['@bundle' => $bundle]); } Loading @@ -123,13 +121,13 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu return TRUE; } $user_profile_has_role = FALSE; /** @var \Drupal\user\Entity\User|null $user_profile */ /** @var \Drupal\user\UserInterface|null $user_profile */ $user_profile = $this->getContextValue('user_profile'); if ($user_profile) { $user_profile_roles = $user_profile->getRoles(); // Search if the user profile has one of the authorized roles. foreach ($this->configuration['roles'] as $expected_role) { if (in_array($expected_role, $user_profile_roles)) { if (\in_array($expected_role, $user_profile_roles, TRUE)) { $user_profile_has_role = TRUE; break; } Loading tests/src/Kernel/UserProfileRoleConditionTest.php +7 −4 Original line number Diff line number Diff line Loading @@ -18,12 +18,15 @@ class UserProfileRoleConditionTest extends EntityKernelTestBase { /** * {@inheritdoc} */ public static $modules = ['context_profile_role', 'user']; public static $modules = [ 'context_profile_role', 'user', ]; /** * Tests conditions. */ public function testConditions() { public function testConditions(): void { /** @var \Drupal\Core\Condition\ConditionManager $manager */ $manager = $this->container->get('plugin.manager.condition'); Loading Loading @@ -66,8 +69,8 @@ class UserProfileRoleConditionTest extends EntityKernelTestBase { // Grab the user profile role condition and configure it to check against // role 1 on user 1. /** @var \Drupal\context_profile_role\Plugin\Condition\UserProfileRole $condition */ $condition = $manager->createInstance('user_profile_role') ->setConfig('roles', ['role_1' => 'role_1']) $condition = $manager->createInstance('user_profile_role'); $condition->setConfig('roles', ['role_1' => 'role_1']) ->setContextValue('user_profile', $user_1); $this->assertTrue($condition->execute(), 'Condition is TRUE for user 1 with the role 1 against role 1.'); Loading Loading
context_profile_role.info.yml +2 −1 Original line number Diff line number Diff line name: 'Context Profile Role' type: module description: 'Provide block condition on the role of the user profile viewed.' core_version_requirement: ^8.8 || ^9 core_version_requirement: ^9.2 || ^10 package: 'Context' php: 7.4 dependencies: - drupal:user
src/ContextProvider/UserProfileRouteContext.php +3 −4 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; * Sets the user profile as a context on user routes. */ class UserProfileRouteContext implements ContextProviderInterface { use StringTranslationTrait; /** Loading @@ -24,10 +23,10 @@ class UserProfileRouteContext implements ContextProviderInterface { * * @var \Drupal\Core\Routing\RouteMatchInterface */ protected $routeMatch; protected RouteMatchInterface $routeMatch; /** * Constructs a new NodeRouteContext. * Constructor. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object. Loading @@ -44,7 +43,7 @@ class UserProfileRouteContext implements ContextProviderInterface { $context_definition = EntityContextDefinition::create('user')->setRequired(FALSE); $value = NULL; $route_object = $this->routeMatch->getRouteObject(); if (!is_null($route_object)) { if ($route_object !== NULL) { /** @var array $route_contexts */ $route_contexts = $route_object->getOption('parameters'); Loading
src/Plugin/Condition/UserProfileRole.php +19 −21 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\user\RoleStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** Loading @@ -18,7 +19,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * id = "user_profile_role", * label = @Translation("User Profile Role"), * context_definitions = { * "user_profile" = @ContextDefinition("entity:user", label = @Translation("User Profile Role")) * "user_profile": @ContextDefinition("entity:user", label = @Translation("User Profile Role")) * }, * ); */ Loading @@ -29,16 +30,13 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu * * @var \Drupal\user\RoleStorageInterface */ protected $roleStorage; protected RoleStorageInterface $roleStorage; /** * Creates a new UserRole instance. * Constructor. * * @param array $configuration * The plugin configuration, i.e. an array with configuration values keyed * by configuration option name. The special key 'context' may be used to * initialize the defined contexts by setting it to an array of context * values keyed by context names. * The plugin configuration. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition Loading Loading @@ -94,7 +92,7 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { /** @var array $form_state_roles_value */ $form_state_roles_value = $form_state->getValue('roles'); $this->configuration['roles'] = array_filter($form_state_roles_value); $this->configuration['roles'] = \array_filter($form_state_roles_value); parent::submitConfigurationForm($form, $form_state); } Loading @@ -102,16 +100,16 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu * {@inheritdoc} */ public function summary(): MarkupInterface { if (count($this->configuration['roles']) > 1) { if (\count($this->configuration['roles']) > 1) { $roles = $this->configuration['roles']; $last = array_pop($roles); $roles = implode(', ', $roles); $last = \array_pop($roles); $roles = \implode(', ', $roles); return $this->t( 'The user role is @roles or @last', ['@roles' => $roles, '@last' => $last] ); } $bundle = reset($this->configuration['roles']); $bundle = \reset($this->configuration['roles']); return $this->t('The user role is @bundle', ['@bundle' => $bundle]); } Loading @@ -123,13 +121,13 @@ class UserProfileRole extends ConditionPluginBase implements ContainerFactoryPlu return TRUE; } $user_profile_has_role = FALSE; /** @var \Drupal\user\Entity\User|null $user_profile */ /** @var \Drupal\user\UserInterface|null $user_profile */ $user_profile = $this->getContextValue('user_profile'); if ($user_profile) { $user_profile_roles = $user_profile->getRoles(); // Search if the user profile has one of the authorized roles. foreach ($this->configuration['roles'] as $expected_role) { if (in_array($expected_role, $user_profile_roles)) { if (\in_array($expected_role, $user_profile_roles, TRUE)) { $user_profile_has_role = TRUE; break; } Loading
tests/src/Kernel/UserProfileRoleConditionTest.php +7 −4 Original line number Diff line number Diff line Loading @@ -18,12 +18,15 @@ class UserProfileRoleConditionTest extends EntityKernelTestBase { /** * {@inheritdoc} */ public static $modules = ['context_profile_role', 'user']; public static $modules = [ 'context_profile_role', 'user', ]; /** * Tests conditions. */ public function testConditions() { public function testConditions(): void { /** @var \Drupal\Core\Condition\ConditionManager $manager */ $manager = $this->container->get('plugin.manager.condition'); Loading Loading @@ -66,8 +69,8 @@ class UserProfileRoleConditionTest extends EntityKernelTestBase { // Grab the user profile role condition and configure it to check against // role 1 on user 1. /** @var \Drupal\context_profile_role\Plugin\Condition\UserProfileRole $condition */ $condition = $manager->createInstance('user_profile_role') ->setConfig('roles', ['role_1' => 'role_1']) $condition = $manager->createInstance('user_profile_role'); $condition->setConfig('roles', ['role_1' => 'role_1']) ->setContextValue('user_profile', $user_1); $this->assertTrue($condition->execute(), 'Condition is TRUE for user 1 with the role 1 against role 1.'); Loading