You are here

class RouteProcessorCurrent in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php \Drupal\Core\RouteProcessor\RouteProcessorCurrent

Provides a route processor to replace <current>.

Hierarchy

Expanded class hierarchy of RouteProcessorCurrent

1 string reference to 'RouteProcessorCurrent'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses RouteProcessorCurrent
route_processor_current in core/core.services.yml
Drupal\Core\RouteProcessor\RouteProcessorCurrent

File

core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php, line 17
Contains \Drupal\Core\RouteProcessor\RouteProcessorCurrent.

Namespace

Drupal\Core\RouteProcessor
View source
class RouteProcessorCurrent implements OutboundRouteProcessorInterface {

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

  /**
   * Constructs a new RouteProcessorCurrent.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function processOutbound($route_name, Route $route, array &$parameters, BubbleableMetadata $bubbleable_metadata = NULL) {
    if ($route_name === '<current>') {
      if ($current_route = $this->routeMatch
        ->getRouteObject()) {
        $requirements = $current_route
          ->getRequirements();

        // Setting _method and _schema is deprecated since 2.7. Using
        // setMethods() and setSchemes() are now the recommended ways.
        unset($requirements['_method']);
        unset($requirements['_schema']);
        $route
          ->setRequirements($requirements);
        $route
          ->setPath($current_route
          ->getPath());
        $route
          ->setSchemes($current_route
          ->getSchemes());
        $route
          ->setMethods($current_route
          ->getMethods());
        $route
          ->setOptions($current_route
          ->getOptions());
        $route
          ->setDefaults($current_route
          ->getDefaults());
        $parameters = array_merge($parameters, $this->routeMatch
          ->getRawParameters()
          ->all());
        if ($bubbleable_metadata) {
          $bubbleable_metadata
            ->addCacheContexts([
            'route',
          ]);
        }
      }
      else {

        // If we have no current route match available, point to the frontpage.
        $route
          ->setPath('/');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteProcessorCurrent::$routeMatch protected property The current route match.
RouteProcessorCurrent::processOutbound public function Processes the outbound route. Overrides OutboundRouteProcessorInterface::processOutbound
RouteProcessorCurrent::__construct public function Constructs a new RouteProcessorCurrent.