improvements-2.x-dev/src/Plugin/Menu/LocalAction/LocalActionWithCurrentQuery.php
src/Plugin/Menu/LocalAction/LocalActionWithCurrentQuery.php
<?php namespace Drupal\improvements\Plugin\Menu\LocalAction; use Drupal\Core\Cache\Cache; use Drupal\Core\Menu\LocalActionDefault; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteProviderInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; /** * @TODO Add test */ class LocalActionWithCurrentQuery extends LocalActionDefault { protected Request $request; /** * {@inheritdoc} */ public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, Request $request) { parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider); $this->request = $request; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('router.route_provider'), $container->get('request_stack')->getCurrentRequest() ); } /** * {@inheritdoc} */ public function getOptions(RouteMatchInterface $route_match): array { $options = parent::getOptions($route_match); $options['query'] = $this->request->query->all(); return $options; } /** * {@inheritdoc} */ public function getCacheContexts(): array { return Cache::mergeContexts(parent::getCacheContexts(), ['url.query_args']); } }