function PathChangedHelper::__construct

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Routing/PathChangedHelper.php \Drupal\Core\Routing\PathChangedHelper::__construct()

Constructs a PathChangedHelper object.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: A route match object, used for the route name and the parameters.

\Symfony\Component\HttpFoundation\Request $request: A request object, used for the query parameters.

Throws

\InvalidArgumentException The route name from $route_match must end with ".bc".

File

core/lib/Drupal/Core/Routing/PathChangedHelper.php, line 63

Class

PathChangedHelper
Provides helper functions for handling path changes.

Namespace

Drupal\Core\Routing

Code

public function __construct(RouteMatchInterface $route_match, Request $request) {
    $bc_route_name = $route_match->getRouteName();
    if (!str_ends_with($bc_route_name, '.bc')) {
        throw new \InvalidArgumentException(__CLASS__ . ' expects a route name that ends with ".bc".');
    }
    // Strip '.bc' from the end of the route name.
    $route_name = substr($bc_route_name, 0, -3);
    $args = $route_match->getRawParameters()
        ->all();
    $options = [
        'absolute' => TRUE,
        'query' => array_diff_key($request->query
            ->all(), [
            'destination' => '',
        ]),
    ];
    $this->newUrl = Url::fromRoute($route_name, $args, $options);
    $this->oldUrl = Url::fromRoute($bc_route_name, $args, $options);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.