Commit fe1fabdc authored by kevinn's avatar kevinn Committed by Michael Mol
Browse files

Issue #3021734 by leopaccanaro, jedsaet, kevinn, DeaOm, michaelmol,...

Issue #3021734 by leopaccanaro, jedsaet, kevinn, DeaOm, michaelmol, joegraduate, adalbertov: Use dependency injection for routeMatch() in CoffeeController
parent 52098328
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Menu\LocalTaskManagerInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -46,6 +47,20 @@ class CoffeeController extends ControllerBase {
   */
  protected $accessManager;

  /**
   * The url generator service.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

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

  /**
   * Constructs a new CoffeeController object.
   *
@@ -58,12 +73,13 @@ class CoffeeController extends ControllerBase {
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
   *   The access manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, MenuLinkTreeInterface $menu_link_tree, LocalTaskManagerInterface $local_task_manager, AccessManagerInterface $access_manager, UrlGeneratorInterface $url_generator) {
  public function __construct(ConfigFactoryInterface $config_factory, MenuLinkTreeInterface $menu_link_tree, LocalTaskManagerInterface $local_task_manager, AccessManagerInterface $access_manager, UrlGeneratorInterface $url_generator, RouteMatchInterface $route_match) {
    $this->config = $config_factory->get('coffee.configuration');
    $this->menuLinkTree = $menu_link_tree;
    $this->localTaskManager = $local_task_manager;
    $this->accessManager = $access_manager;
    $this->urlGenerator = $url_generator;
    $this->routeMatch = $route_match;
  }

  /**
@@ -75,7 +91,8 @@ class CoffeeController extends ControllerBase {
      $container->get('menu.link_tree'),
      $container->get('plugin.manager.menu.local_task'),
      $container->get('access_manager'),
      $container->get('coffee.url_generator')
      $container->get('coffee.url_generator'),
      $container->get('current_route_match')
    );
  }

@@ -184,7 +201,6 @@ class CoffeeController extends ControllerBase {
    $links = [];

    $tree = $this->localTaskManager->getLocalTasksForRoute($route_name);
    $route_match = \Drupal::routeMatch();

    foreach ($tree as $instances) {
      /* @var $instances \Drupal\Core\Menu\LocalTaskInterface[] */
@@ -192,13 +208,13 @@ class CoffeeController extends ControllerBase {
        $child_route_name = $child->getRouteName();
        // Merges the parent's route parameter with the child ones since you
        // calculate the local tasks outside of parent route context.
        $child_route_parameters = $child->getRouteParameters($route_match) + $route_parameters;
        $child_route_parameters = $child->getRouteParameters($this->routeMatch) + $route_parameters;

        if (strpos($child_route_name, 'config_translate') !== FALSE && $this->accessManager->checkNamedRoute($child_route_name, $child_route_parameters)) {
          $links[$child_route_name] = [
            'title' => $child->getTitle(),
            'url' => Url::fromRoute($child_route_name, $child_route_parameters),
            'localized_options' => $child->getOptions($route_match),
            'localized_options' => $child->getOptions($this->routeMatch),
          ];
        }
      }