diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php index 5763a7dc14..f8258e746d 100644 --- a/core/lib/Drupal/Core/Controller/ControllerBase.php +++ b/core/lib/Drupal/Core/Controller/ControllerBase.php @@ -8,8 +8,10 @@ use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Messenger\MessengerTrait; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Utility base class for thin controllers. @@ -282,6 +284,27 @@ protected function languageManager() { return $this->languageManager; } + /** + * Returns a redirect response object for the specified route. + * + * @param string $route_name + * The name of the route to which to redirect. + * @param array $route_parameters + * (optional) Parameters for the route. + * @param array $options + * (optional) An associative array of additional options. + * @param int $status + * (optional) The HTTP redirect status code for the redirect. The default is + * 302 Found. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect response object that may be returned by the controller. + */ + protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) { + $options['absolute'] = TRUE; + return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options), $status); + } + /** * Returns the service container. * diff --git a/core/lib/Drupal/Core/Entity/Controller/EntityController.php b/core/lib/Drupal/Core/Entity/Controller/EntityController.php index 46cc730b9e..bd99b0a7cd 100644 --- a/core/lib/Drupal/Core/Entity/Controller/EntityController.php +++ b/core/lib/Drupal/Core/Entity/Controller/EntityController.php @@ -16,7 +16,9 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Provides the add-page and title callbacks for entities. @@ -101,6 +103,27 @@ public static function create(ContainerInterface $container) { ); } + /** + * Returns a redirect response object for the specified route. + * + * @param string $route_name + * The name of the route to which to redirect. + * @param array $route_parameters + * (optional) Parameters for the route. + * @param array $options + * (optional) An associative array of additional options. + * @param int $status + * (optional) The HTTP redirect status code for the redirect. The default is + * 302 Found. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect response object that may be returned by the controller. + */ + protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) { + $options['absolute'] = TRUE; + return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options), $status); + } + /** * Displays add links for the available bundles. * diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php index 165cb3a8f0..e4b7f4a98c 100644 --- a/core/lib/Drupal/Core/Form/FormBase.php +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -10,7 +10,9 @@ use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RequestStack; use Drupal\Core\Messenger\MessengerTrait; @@ -194,6 +196,27 @@ protected function currentUser() { return \Drupal::currentUser(); } + /** + * Returns a redirect response object for the specified route. + * + * @param string $route_name + * The name of the route to which to redirect. + * @param array $route_parameters + * (optional) Parameters for the route. + * @param array $options + * (optional) An associative array of additional options. + * @param int $status + * (optional) The HTTP redirect status code for the redirect. The default is + * 302 Found. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect response object that may be returned by the controller. + */ + protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) { + $options['absolute'] = TRUE; + return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options), $status); + } + /** * Returns the service container. * diff --git a/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php b/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php index 2c510fff49..209030729b 100644 --- a/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php +++ b/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php @@ -58,8 +58,12 @@ protected function url($route_name, $route_parameters = [], $options = []) { * * @return \Symfony\Component\HttpFoundation\RedirectResponse * A redirect response object that may be returned by the controller. + * + * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. + * Use new RedirectResponse(Url::fromRoute()) instead. */ protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) { + @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::redirect() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use new RedirectResponse(Url::fromRoute()) instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED); $options['absolute'] = TRUE; $url = $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options); return new RedirectResponse($url, $status); @@ -70,8 +74,12 @@ protected function redirect($route_name, array $route_parameters = [], array $op * * @return \Drupal\Core\Routing\UrlGeneratorInterface * The URL generator service. + * + * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. + * Use the url_generator service instead. */ protected function getUrlGenerator() { + @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::getUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the url_generator service instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED); if (!$this->urlGenerator) { $this->urlGenerator = \Drupal::service('url_generator'); } @@ -85,8 +93,11 @@ protected function getUrlGenerator() { * The url generator service. * * @return $this + * + * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. */ public function setUrlGenerator(UrlGeneratorInterface $generator) { + @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::setUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED); $this->urlGenerator = $generator; return $this; diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTraitTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTraitTest.php index 06c470ba33..230f059581 100644 --- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTraitTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTraitTest.php @@ -7,12 +7,16 @@ /** * @coversDefaultClass \Drupal\Core\Routing\UrlGeneratorTrait * @group Routing + * @group legacy */ class UrlGeneratorTraitTest extends UnitTestCase { /** * @covers ::setUrlGenerator * @covers ::getUrlGenerator + * + * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::setUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/2614344 + * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::getUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the url_generator service instead. See https://www.drupal.org/node/2614344 */ public function testGetUrlGenerator() { $url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface'); @@ -28,6 +32,8 @@ public function testGetUrlGenerator() { /** * @covers ::redirect + * + * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::redirect() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use new RedirectResponse(Url::fromRoute()) instead. See https://www.drupal.org/node/2614344 */ public function testRedirect() { $route_name = 'some_route_name'; @@ -50,4 +56,29 @@ public function testRedirect() { $this->assertSame($generated_url, $result->getTargetUrl()); } + /** + * @covers ::url + * + * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::url() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Url::fromUri() instead. See https://www.drupal.org/node/2614344 + */ + public function testUrl() { + $route_name = 'some_route_name'; + $generated_url = 'some/generated/url'; + + $url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface'); + $url_generator->expects($this->once()) + ->method('generateFromRoute') + ->with($route_name, [], []) + ->willReturn($generated_url); + + $url_generator_trait_object = $this->getMockForTrait('Drupal\Core\Routing\UrlGeneratorTrait'); + $url_generator_trait_object->setUrlGenerator($url_generator); + + $url_generator_method = new \ReflectionMethod($url_generator_trait_object, 'url'); + $url_generator_method->setAccessible(TRUE); + + $result = $url_generator_method->invoke($url_generator_trait_object, $route_name); + $this->assertSame($generated_url, $result); + } + }