Commit c4223052 authored by catch's avatar catch
Browse files

Issue #2294157 by tim.plunkett, dawehner: Switch getOptions() and...

Issue #2294157 by tim.plunkett, dawehner: Switch getOptions() and getRouteParameters() within LocalActionInterface and LocalTaskInterface to use RouteMatch
parent abe83bfb
Loading
Loading
Loading
Loading
+2 −2
Changes for core/core.services.yml: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -367,10 +367,10 @@ services:
    arguments: ['@menu.link_tree', '@entity.manager', '@string_translation']
  plugin.manager.menu.local_action:
    class: Drupal\Core\Menu\LocalActionManager
    arguments: ['@controller_resolver', '@request_stack', '@router.route_provider', '@module_handler', '@cache.discovery', '@language_manager', '@access_manager', '@current_user']
    arguments: ['@controller_resolver', '@request_stack', '@current_route_match', '@router.route_provider', '@module_handler', '@cache.discovery', '@language_manager', '@access_manager', '@current_user']
  plugin.manager.menu.local_task:
    class: Drupal\Core\Menu\LocalTaskManager
    arguments: ['@controller_resolver', '@request_stack', '@router.route_provider', '@router.builder', '@module_handler', '@cache.discovery', '@language_manager', '@access_manager', '@current_user']
    arguments: ['@controller_resolver', '@request_stack', '@current_route_match', '@router.route_provider', '@router.builder', '@module_handler', '@cache.discovery', '@language_manager', '@access_manager', '@current_user']
  plugin.manager.menu.contextual_link:
    class: Drupal\Core\Menu\ContextualLinkManager
    arguments: ['@controller_resolver', '@module_handler', '@cache.discovery', '@language_manager', '@access_manager', '@current_user', '@request_stack']
+7 −5
Changes for core/lib/Drupal/Core/Menu/LocalActionDefault.php: 7 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
@@ -88,7 +89,7 @@ public function getWeight() {
  /**
   * {@inheritdoc}
   */
  public function getRouteParameters(Request $request) {
  public function getRouteParameters(RouteMatchInterface $route_match) {
    $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : array();
    $route = $this->routeProvider->getRouteByName($this->getRouteName());
    $variables = $route->compile()->getVariables();
@@ -99,7 +100,7 @@ public function getRouteParameters(Request $request) {
    // slugs in the path patterns. For example, if the route's path pattern is
    // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
    // $raw_variables->get('filter_format') == 'plain_text'.
    $raw_variables = $request->attributes->get('_raw_variables');
    $raw_variables = $route_match->getRawParameters();

    foreach ($variables as $name) {
      if (isset($parameters[$name])) {
@@ -109,8 +110,8 @@ public function getRouteParameters(Request $request) {
      if ($raw_variables && $raw_variables->has($name)) {
        $parameters[$name] = $raw_variables->get($name);
      }
      elseif ($request->attributes->has($name)) {
        $parameters[$name] = $request->attributes->get($name);
      elseif ($value = $route_match->getRawParameter($name)) {
        $parameters[$name] = $value;
      }
    }
    // The UrlGenerator will throw an exception if expected parameters are
@@ -121,7 +122,8 @@ public function getRouteParameters(Request $request) {
  /**
   * {@inheritdoc}
   */
  public function getOptions(Request $request) {
  public function getOptions(RouteMatchInterface $route_match) {
    return (array) $this->pluginDefinition['options'];
  }

}
+7 −7
Changes for core/lib/Drupal/Core/Menu/LocalActionInterface.php: 7 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@

namespace Drupal\Core\Menu;

use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Defines an interface for menu local actions.
@@ -25,13 +25,13 @@ public function getRouteName();
  /**
   * Returns the route parameters needed to render a link for the local action.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The HttpRequest object representing the current request.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   *
   * @return array
   *   An array of parameter names and values.
   */
  public function getRouteParameters(Request $request);
  public function getRouteParameters(RouteMatchInterface $route_match);

  /**
   * Returns the weight for the local action.
@@ -43,13 +43,13 @@ public function getWeight();
  /**
   * Returns options for rendering a link for the local action.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The HTTP request object representing the current request.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   *
   * @return array
   *   An associative array of options.
   */
  public function getOptions(Request $request);
  public function getOptions(RouteMatchInterface $route_match);

  /**
   * Returns the localized title to be shown for this action.
+15 −4
Changes for core/lib/Drupal/Core/Menu/LocalActionManager.php: 15 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -64,6 +65,13 @@ class LocalActionManager extends DefaultPluginManager implements LocalActionMana
   */
  protected $requestStack;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The route provider to load routes by name.
   *
@@ -99,6 +107,8 @@ class LocalActionManager extends DefaultPluginManager implements LocalActionMana
   *   An object to use in introspecting route methods.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
@@ -112,7 +122,7 @@ class LocalActionManager extends DefaultPluginManager implements LocalActionMana
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   */
  public function __construct(ControllerResolverInterface $controller_resolver, RequestStack $request_stack, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, AccountInterface $account) {
  public function __construct(ControllerResolverInterface $controller_resolver, RequestStack $request_stack, RouteMatchInterface $route_match, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, AccountInterface $account) {
    // Skip calling the parent constructor, since that assumes annotation-based
    // discovery.
    $this->discovery = new YamlDiscovery('links.action', $module_handler->getModuleDirectories());
@@ -120,6 +130,7 @@ public function __construct(ControllerResolverInterface $controller_resolver, Re
    $this->factory = new ContainerFactory($this, 'Drupal\Core\Menu\LocalActionInterface');
    $this->controllerResolver = $controller_resolver;
    $this->requestStack = $request_stack;
    $this->routeMatch = $route_match;
    $this->routeProvider = $route_provider;
    $this->accessManager = $access_manager;
    $this->moduleHandler = $module_handler;
@@ -159,16 +170,16 @@ public function getActionsForRoute($route_appears) {
      }
    }
    $links = array();
    $request = $this->requestStack->getCurrentRequest();
    /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */
    foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
      $route_name = $plugin->getRouteName();
      $route_parameters = $plugin->getRouteParameters($request);
      $route_parameters = $plugin->getRouteParameters($this->routeMatch);
      $links[$plugin_id] = array(
        '#theme' => 'menu_local_action',
        '#link' => array(
          'title' => $this->getTitle($plugin),
          'url' => Url::fromRoute($route_name, $route_parameters),
          'localized_options' => $plugin->getOptions($request),
          'localized_options' => $plugin->getOptions($this->routeMatch),
        ),
        '#access' => $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account),
        '#weight' => $plugin->getWeight(),
+6 −5
Changes for core/lib/Drupal/Core/Menu/LocalTaskDefault.php: 6 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
namespace Drupal\Core\Menu;

use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;

/**
@@ -39,7 +40,7 @@ public function getRouteName() {
  /**
   * {@inheritdoc}
   */
  public function getRouteParameters(Request $request) {
  public function getRouteParameters(RouteMatchInterface $route_match) {
    $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : array();
    $route = $this->routeProvider()->getRouteByName($this->getRouteName());
    $variables = $route->compile()->getVariables();
@@ -51,7 +52,7 @@ public function getRouteParameters(Request $request) {
    // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
    // $raw_variables->get('filter_format') == 'plain_text'.

    $raw_variables = $request->attributes->get('_raw_variables');
    $raw_variables = $route_match->getRawParameters();

    foreach ($variables as $name) {
      if (isset($parameters[$name])) {
@@ -61,8 +62,8 @@ public function getRouteParameters(Request $request) {
      if ($raw_variables && $raw_variables->has($name)) {
        $parameters[$name] = $raw_variables->get($name);
      }
      elseif ($request->attributes->has($name)) {
        $parameters[$name] = $request->attributes->get($name);
      elseif ($value = $route_match->getRawParameter($name)) {
        $parameters[$name] = $value;
      }
    }
    // The UrlGenerator will throw an exception if expected parameters are
@@ -109,7 +110,7 @@ public function getWeight() {
  /**
   * {@inheritdoc}
   */
  public function getOptions(Request $request) {
  public function getOptions(RouteMatchInterface $route_match) {
    $options = $this->pluginDefinition['options'];
    if ($this->active) {
      if (empty($options['attributes']['class']) || !in_array('active', $options['attributes']['class'])) {
Loading