Commit 4014b744 authored by alexpott's avatar alexpott

Issue #2344491 by tim.plunkett: Move ControllerBase::redirect() to UrlGeneratorTrait.

parent d111ef32
......@@ -12,7 +12,6 @@
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Utility base class for thin controllers.
......@@ -271,21 +270,4 @@ private function container() {
return \Drupal::getContainer();
}
/**
* 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
* Parameters for the route.
* @param int $status
* 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.
*/
public function redirect($route_name, array $route_parameters = array(), $status = 302) {
$url = $this->url($route_name, $route_parameters, ['absolute' => TRUE]);
return new RedirectResponse($url, $status);
}
}
......@@ -2,11 +2,13 @@
/**
* @file
* Contains Drupal\Core\Routing\UrlGeneratorTrait.
* Contains \Drupal\Core\Routing\UrlGeneratorTrait.
*/
namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Wrapper methods for the Url Generator.
*
......@@ -37,6 +39,28 @@ protected function url($route_name, $route_parameters = array(), $options = arra
return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
}
/**
* 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;
$url = $this->url($route_name, $route_parameters, $options);
return new RedirectResponse($url, $status);
}
/**
* Returns the URL generator service.
*
......
......@@ -75,7 +75,7 @@ public function testFeed($use_last_modified, $use_etag, Request $request) {
* A response that redirects users to the test feed.
*/
public function testRedirect() {
return $this->redirect('aggregator_test.feed', array(), 301);
return $this->redirect('aggregator_test.feed', [], [], 301);
}
}
......@@ -13,7 +13,6 @@
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Builds a confirmation form for enabling modules with dependencies.
......@@ -108,7 +107,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// Redirect to the modules list page if the key value store is empty.
if (!$this->modules) {
return new RedirectResponse($this->url('system.modules_list', [], ['absolute' => TRUE]));
return $this->redirect('system.modules_list');
}
$items = array();
......
......@@ -11,12 +11,10 @@
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\user\TempStoreFactory;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Provides a confirmation form for cancelling multiple user accounts.
......@@ -108,7 +106,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
->get('user_user_operations_cancel')
->get($this->currentUser()->id());
if (!$accounts) {
return new RedirectResponse($this->url('user.admin_account', [], ['absolute' => TRUE]));
return $this->redirect('user.admin_account');
}
$form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
......@@ -132,7 +130,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
drupal_set_message($message, $redirect ? 'error' : 'warning');
// If only user 1 was selected, redirect to the overview.
if ($redirect) {
return new RedirectResponse($this->url('user.admin_account', [], ['absolute' => TRUE]));
return $this->redirect('user.admin_account');
}
}
......
......@@ -31,4 +31,28 @@ public function testGetUrlGenerator() {
$this->assertEquals($url_generator, $result);
}
/**
* @covers ::redirect
*/
public function testRedirect() {
$route_name = 'some_route_name';
$generated_url = 'some/generated/url';
$url_generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
$url_generator->expects($this->once())
->method('generateFromRoute')
->with($route_name, [], ['absolute' => TRUE])
->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, 'redirect');
$url_generator_method->setAccessible(TRUE);
$result = $url_generator_method->invoke($url_generator_trait_object, $route_name);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
$this->assertSame($generated_url, $result->getTargetUrl());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment