Loading core/lib/Drupal/Core/PathProcessor/OutboundPathProcessorInterface.php +4 −0 Original line number Diff line number Diff line Loading @@ -37,6 +37,10 @@ interface OutboundPathProcessorInterface { * dependent URL requires so. * - 'route': The route object for the given path. It will be set by * \Drupal\Core\Routing\UrlGenerator::generateFromRoute(). * - 'route_name': The name of the route for the given path. It will be set * by \Drupal\Core\Routing\UrlGenerator::generateFromRoute(). * - 'route_parameters': An array of route parameters for the given path. It * will be set by \Drupal\Core\Routing\UrlGenerator::generateFromRoute(). * @param \Symfony\Component\HttpFoundation\Request $request * The HttpRequest object representing the current request. * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata Loading core/lib/Drupal/Core/Routing/UrlGenerator.php +4 −2 Original line number Diff line number Diff line Loading @@ -308,9 +308,11 @@ public function generateFromRoute(string $name, array $parameters = [], array $o $this->processRoute($name, $route, $parameters, $generated_url); $path = $this->getInternalPathFromRoute($name, $route, $parameters, $options['query']); // Outbound path processors might need the route object for the path, e.g. // to get the path pattern. // Outbound path processors might need the route information for the path, // e.g. to get the path pattern. $options['route'] = $route; $options['route_name'] = $name; $options['route_parameters'] = $parameters; if ($options['path_processing']) { $path = $this->processPath($path, $options, $generated_url); } Loading core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php +28 −0 Original line number Diff line number Diff line Loading @@ -574,4 +574,32 @@ protected function assertGenerateFromRoute(string $route_name, array $route_para $this->assertEquals($expected_bubbleable_metadata, BubbleableMetadata::createFromObject($generated_url)); } /** * Tests that route name and parameters are passed to path processors. * * @legacy-covers ::generateFromRoute */ public function testGenerateWithRouteNameInOptions(): void { $path_processor = $this->createMock(OutboundPathProcessorInterface::class); $path_processor->expects($this->atLeastOnce()) ->method('processOutbound') ->willReturnCallback(function ($path, &$options = []) { // Assert that 'route_name' exists in options and is the expected value. $this->assertArrayHasKey('route_name', $options); $this->assertEquals('test_1', $options['route_name']); // Assert that 'route_parameters' exists in options and // is the expected value. $this->assertArrayHasKey('route_parameters', $options); $this->assertEquals(['node' => 1], $options['route_parameters']); return $path; }); $this->processorManager->addOutbound($path_processor); $options = []; $this->assertGenerateFromRoute('test_1', ['node' => 1], $options, '/hello/world?node=1', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT)); } } Loading
core/lib/Drupal/Core/PathProcessor/OutboundPathProcessorInterface.php +4 −0 Original line number Diff line number Diff line Loading @@ -37,6 +37,10 @@ interface OutboundPathProcessorInterface { * dependent URL requires so. * - 'route': The route object for the given path. It will be set by * \Drupal\Core\Routing\UrlGenerator::generateFromRoute(). * - 'route_name': The name of the route for the given path. It will be set * by \Drupal\Core\Routing\UrlGenerator::generateFromRoute(). * - 'route_parameters': An array of route parameters for the given path. It * will be set by \Drupal\Core\Routing\UrlGenerator::generateFromRoute(). * @param \Symfony\Component\HttpFoundation\Request $request * The HttpRequest object representing the current request. * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata Loading
core/lib/Drupal/Core/Routing/UrlGenerator.php +4 −2 Original line number Diff line number Diff line Loading @@ -308,9 +308,11 @@ public function generateFromRoute(string $name, array $parameters = [], array $o $this->processRoute($name, $route, $parameters, $generated_url); $path = $this->getInternalPathFromRoute($name, $route, $parameters, $options['query']); // Outbound path processors might need the route object for the path, e.g. // to get the path pattern. // Outbound path processors might need the route information for the path, // e.g. to get the path pattern. $options['route'] = $route; $options['route_name'] = $name; $options['route_parameters'] = $parameters; if ($options['path_processing']) { $path = $this->processPath($path, $options, $generated_url); } Loading
core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php +28 −0 Original line number Diff line number Diff line Loading @@ -574,4 +574,32 @@ protected function assertGenerateFromRoute(string $route_name, array $route_para $this->assertEquals($expected_bubbleable_metadata, BubbleableMetadata::createFromObject($generated_url)); } /** * Tests that route name and parameters are passed to path processors. * * @legacy-covers ::generateFromRoute */ public function testGenerateWithRouteNameInOptions(): void { $path_processor = $this->createMock(OutboundPathProcessorInterface::class); $path_processor->expects($this->atLeastOnce()) ->method('processOutbound') ->willReturnCallback(function ($path, &$options = []) { // Assert that 'route_name' exists in options and is the expected value. $this->assertArrayHasKey('route_name', $options); $this->assertEquals('test_1', $options['route_name']); // Assert that 'route_parameters' exists in options and // is the expected value. $this->assertArrayHasKey('route_parameters', $options); $this->assertEquals(['node' => 1], $options['route_parameters']); return $path; }); $this->processorManager->addOutbound($path_processor); $options = []; $this->assertGenerateFromRoute('test_1', ['node' => 1], $options, '/hello/world?node=1', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT)); } }