From c3e024da6bdbdbeb7e8a78e22bc75d5232229534 Mon Sep 17 00:00:00 2001 From: Joao Paulo Constantino <joaopauloctga@gmail.com> Date: Mon, 31 Mar 2025 19:48:27 -0300 Subject: [PATCH 1/5] Adding contextual user option to the Flag views relationship --- .../relationship/FlagViewsRelationship.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Plugin/views/relationship/FlagViewsRelationship.php b/src/Plugin/views/relationship/FlagViewsRelationship.php index 9aacd3a6..7f32bcb5 100644 --- a/src/Plugin/views/relationship/FlagViewsRelationship.php +++ b/src/Plugin/views/relationship/FlagViewsRelationship.php @@ -11,6 +11,7 @@ use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; use Drupal\flag\FlagServiceInterface; use Drupal\user\RoleInterface; +use Drupal\user\UserInterface; use Drupal\views\Plugin\views\relationship\RelationshipPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -120,10 +121,15 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD $form['flag']['#options'][$flag_id] = $flag->label(); } + $user_scope_options = [ + 'current' => $this->t('Current user'), + 'context' => $this->t('User from Url context'), + 'any' => $this->t('Any user'), + ]; $form['user_scope'] = [ '#type' => 'radios', '#title' => $this->t('By'), - '#options' => ['current' => $this->t('Current user'), 'any' => $this->t('Any user')], + '#options' => $user_scope_options, '#default_value' => $this->options['user_scope'], ]; @@ -157,10 +163,18 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD 'numeric' => TRUE, ]; - if ($this->options['user_scope'] == 'current' && !$flag->isGlobal()) { + if (in_array($this->options['user_scope'], ['current', 'any']) && !$flag->isGlobal()) { + $user_value = '***CURRENT_USER***'; + if ($this->options['user_scope']) { + $route_match = \Drupal::routeMatch(); + $context_user = $route_match->getParameter('user'); + if ($context_user instanceof UserInterface) { + $user_value = $context_user->id(); + } + } $this->definition['extra'][] = [ 'field' => 'uid', - 'value' => '***CURRENT_USER***', + 'value' => $user_value, 'numeric' => TRUE, ]; -- GitLab From a9e7a61c4ef446cea0013df3d9cb488dea9aa8ce Mon Sep 17 00:00:00 2001 From: Joao Paulo Constantino <joaopauloctga@gmail.com> Date: Mon, 31 Mar 2025 19:54:43 -0300 Subject: [PATCH 2/5] Adding contextual user option to the Flag views relationship --- .../relationship/FlagViewsRelationship.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Plugin/views/relationship/FlagViewsRelationship.php b/src/Plugin/views/relationship/FlagViewsRelationship.php index 7f32bcb5..17423c21 100644 --- a/src/Plugin/views/relationship/FlagViewsRelationship.php +++ b/src/Plugin/views/relationship/FlagViewsRelationship.php @@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\PageCache\ResponsePolicy\KillSwitch; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; use Drupal\flag\FlagServiceInterface; @@ -50,6 +51,13 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD */ protected $entityTypeManager; + /** + * The route match. + * + * @var \Drupal\Core\Routing\RouteMatchInterface + */ + protected $router; + /** * Constructs a FlagViewsRelationship object. * @@ -67,14 +75,16 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. + * @param \Drupal\Core\Routing\RouteMatchInterface */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, KillSwitch $page_cache_kill_switch, FlagServiceInterface $flag_service, AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, KillSwitch $page_cache_kill_switch, FlagServiceInterface $flag_service, AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $router) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->flagService = $flag_service; $this->pageCacheKillSwitch = $page_cache_kill_switch; $this->currentUser = $current_user; $this->entityTypeManager = $entity_type_manager; $this->definition = $plugin_definition + $configuration; + $this->router = $router; } /** @@ -85,7 +95,8 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD $page_cache_kill_switch = $container->get('page_cache_kill_switch'); $current_user = $container->get('current_user'); $entity_type_manager = $container->get('entity_type.manager'); - return new static($configuration, $plugin_id, $plugin_definition, $page_cache_kill_switch, $flag_service, $current_user, $entity_type_manager); + $router = $container->get('current_route_match'); + return new static($configuration, $plugin_id, $plugin_definition, $page_cache_kill_switch, $flag_service, $current_user, $entity_type_manager, $router); } /** @@ -166,8 +177,7 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD if (in_array($this->options['user_scope'], ['current', 'any']) && !$flag->isGlobal()) { $user_value = '***CURRENT_USER***'; if ($this->options['user_scope']) { - $route_match = \Drupal::routeMatch(); - $context_user = $route_match->getParameter('user'); + $context_user = $this->router->getParameter('user'); if ($context_user instanceof UserInterface) { $user_value = $context_user->id(); } -- GitLab From 457245c849946162460ee36b4a8858471fe4df71 Mon Sep 17 00:00:00 2001 From: Joao Paulo Constantino <joaopauloctga@gmail.com> Date: Mon, 31 Mar 2025 19:57:55 -0300 Subject: [PATCH 3/5] Adding contextual user option to the Flag views relationship --- src/Plugin/views/relationship/FlagViewsRelationship.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/views/relationship/FlagViewsRelationship.php b/src/Plugin/views/relationship/FlagViewsRelationship.php index 17423c21..2f2b4c49 100644 --- a/src/Plugin/views/relationship/FlagViewsRelationship.php +++ b/src/Plugin/views/relationship/FlagViewsRelationship.php @@ -174,9 +174,9 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD 'numeric' => TRUE, ]; - if (in_array($this->options['user_scope'], ['current', 'any']) && !$flag->isGlobal()) { + if (in_array($this->options['user_scope'], ['current', 'context']) && !$flag->isGlobal()) { $user_value = '***CURRENT_USER***'; - if ($this->options['user_scope']) { + if ($this->options['user_scope'] === 'context') { $context_user = $this->router->getParameter('user'); if ($context_user instanceof UserInterface) { $user_value = $context_user->id(); -- GitLab From 8b09936b278ab1a0466cd46d5f3f0e2b763b558e Mon Sep 17 00:00:00 2001 From: Joao Paulo Constantino <joaopauloctga@gmail.com> Date: Mon, 31 Mar 2025 20:06:54 -0300 Subject: [PATCH 4/5] Adding contextual user option to the Flag views relationship --- src/Plugin/views/relationship/FlagViewsRelationship.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/views/relationship/FlagViewsRelationship.php b/src/Plugin/views/relationship/FlagViewsRelationship.php index 2f2b4c49..a0d62634 100644 --- a/src/Plugin/views/relationship/FlagViewsRelationship.php +++ b/src/Plugin/views/relationship/FlagViewsRelationship.php @@ -191,7 +191,7 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD $roles = $this->entityTypeManager->getStorage('user_role')->loadMultiple(); $flag_roles = array_filter($roles, fn(RoleInterface $role) => $role->hasPermission('flag ' . $flag->id())); - if (isset($flag_roles[RoleInterface::ANONYMOUS_ID]) && $this->currentUser->isAnonymous()) { + if (isset($flag_roles[RoleInterface::ANONYMOUS_ID]) && $this->currentUser->isAnonymous() && $this->options['user_scope'] !== 'context') { // Disable page caching for anonymous users. $this->pageCacheKillSwitch->trigger(); -- GitLab From fbc379a8723d9912229303849b12839457fd7990 Mon Sep 17 00:00:00 2001 From: Joao Paulo Constantino <joaopauloctga@gmail.com> Date: Tue, 1 Apr 2025 09:33:05 -0300 Subject: [PATCH 5/5] Adding contextual user option to the Flag views relationship --- src/Plugin/views/relationship/FlagViewsRelationship.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Plugin/views/relationship/FlagViewsRelationship.php b/src/Plugin/views/relationship/FlagViewsRelationship.php index a0d62634..7b12dd8a 100644 --- a/src/Plugin/views/relationship/FlagViewsRelationship.php +++ b/src/Plugin/views/relationship/FlagViewsRelationship.php @@ -75,7 +75,8 @@ class FlagViewsRelationship extends RelationshipPluginBase implements CacheableD * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. - * @param \Drupal\Core\Routing\RouteMatchInterface + * @param \Drupal\Core\Routing\RouteMatchInterface $router + * The route match. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, KillSwitch $page_cache_kill_switch, FlagServiceInterface $flag_service, AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $router) { parent::__construct($configuration, $plugin_id, $plugin_definition); -- GitLab