Commit d48d9c92 authored by alexpott's avatar alexpott
Browse files

Issue #2448605 by martin107, willzyx, tim.plunkett, klausi, dpopdan: Replace...

Issue #2448605 by martin107, willzyx, tim.plunkett, klausi, dpopdan: Replace usages of drupal_get_destination() with the redirect destination service
parent 6ffc4cd8
......@@ -989,7 +989,7 @@ services:
class: Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber
tags:
- { name: event_subscriber }
arguments: ['@http_kernel', '@logger.channel.php']
arguments: ['@http_kernel', '@logger.channel.php', '@redirect.destination']
exception.default:
class: Drupal\Core\EventSubscriber\DefaultExceptionSubscriber
tags:
......@@ -1009,7 +1009,7 @@ services:
class: Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber
tags:
- { name: event_subscriber }
arguments: ['@config.factory', '@path.alias_manager', '@http_kernel', '@logger.channel.php']
arguments: ['@config.factory', '@path.alias_manager', '@http_kernel', '@logger.channel.php', '@redirect.destination']
exception.fast_404_html:
class: Drupal\Core\EventSubscriber\Fast404ExceptionHtmlSubscriber
tags:
......
......@@ -9,6 +9,7 @@
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -35,8 +36,10 @@
* @ingroup menu
*/
abstract class ControllerBase implements ContainerInjectionInterface {
use StringTranslationTrait;
use LinkGeneratorTrait;
use RedirectDestinationTrait;
use StringTranslationTrait;
use UrlGeneratorTrait;
/**
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
......@@ -44,9 +45,11 @@ class CustomPageExceptionHtmlSubscriber extends DefaultExceptionHtmlSubscriber {
* The HTTP Kernel service.
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
*/
public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, HttpKernelInterface $http_kernel, LoggerInterface $logger) {
parent::__construct($http_kernel, $logger);
public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, HttpKernelInterface $http_kernel, LoggerInterface $logger, RedirectDestinationInterface $redirect_destination) {
parent::__construct($http_kernel, $logger, $redirect_destination);
$this->configFactory = $config_factory;
$this->aliasManager = $alias_manager;
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Routing\AccessAwareRouterInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
......@@ -35,6 +36,13 @@ class DefaultExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
*/
protected $logger;
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* Constructs a new DefaultExceptionHtmlSubscriber.
*
......@@ -42,10 +50,13 @@ class DefaultExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
* The HTTP kernel.
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
*/
public function __construct(HttpKernelInterface $http_kernel, LoggerInterface $logger) {
public function __construct(HttpKernelInterface $http_kernel, LoggerInterface $logger, RedirectDestinationInterface $redirect_destination) {
$this->httpKernel = $http_kernel;
$this->logger = $logger;
$this->redirectDestination = $redirect_destination;
}
/**
......@@ -105,10 +116,10 @@ protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $st
if ($url != $request->getBasePath() . '/' && $url != $current_url) {
if ($request->getMethod() === 'POST') {
$sub_request = Request::create($url, 'POST', $this->drupalGetDestination() + ['_exception_statuscode' => $status_code] + $request->request->all(), $request->cookies->all(), [], $request->server->all());
$sub_request = Request::create($url, 'POST', $this->redirectDestination->getAsArray() + ['_exception_statuscode' => $status_code] + $request->request->all(), $request->cookies->all(), [], $request->server->all());
}
else {
$sub_request = Request::create($url, 'GET', $request->query->all() + $this->drupalGetDestination() + ['_exception_statuscode' => $status_code], $request->cookies->all(), [], $request->server->all());
$sub_request = Request::create($url, 'GET', $request->query->all() + $this->redirectDestination->getAsArray() + ['_exception_statuscode' => $status_code], $request->cookies->all(), [], $request->server->all());
}
try {
......@@ -137,11 +148,4 @@ protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $st
}
}
/**
* Wraps drupal_get_destination().
*/
protected function drupalGetDestination() {
return drupal_get_destination();
}
}
......@@ -11,6 +11,7 @@
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -22,9 +23,11 @@
* @ingroup form_api
*/
abstract class FormBase implements FormInterface, ContainerInjectionInterface {
use StringTranslationTrait;
use DependencySerializationTrait;
use LinkGeneratorTrait;
use RedirectDestinationTrait;
use StringTranslationTrait;
use UrlGeneratorTrait;
/**
......
......@@ -63,7 +63,7 @@ public static function preRenderCompactLink($element) {
$element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', array('mode' => 'off'));
$element['#options'] = array(
'attributes' => array('title' => t('Expand layout to include descriptions.')),
'query' => drupal_get_destination(),
'query' => \Drupal::destination()->getAsArray()
);
}
else {
......@@ -71,7 +71,7 @@ public static function preRenderCompactLink($element) {
$element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', array('mode' => 'on'));
$element['#options'] = array(
'attributes' => array('title' => t('Compress layout by hiding descriptions.')),
'query' => drupal_get_destination(),
'query' => \Drupal::destination()->getAsArray(),
);
}
......
<?php
/**
* @file
* Contains Drupal\Core\Routing\RedirectDestinationTrait.
*/
namespace Drupal\Core\Routing;
/**
* Wrapper methods for the Redirect Destination.
*
* This utility trait should only be used in application-level code, such as
* classes that would implement ContainerInjectionInterface. Services registered
* in the Container should not use this trait but inject the appropriate service
* directly for easier testing.
*/
trait RedirectDestinationTrait {
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
*
* @see \Drupal\Core\Routing\RedirectDestinationInterface::getAsArray()
*
* @return array
* An associative array containing the key:
* - destination: The value of the current request's 'destination' query
* parameter, if present. This can be either a relative or absolute URL.
* However, for security, redirection to external URLs is not performed.
* If the query parameter isn't present, then the URL of the current
* request is returned.
*/
protected function getDestinationArray() {
return $this->getRedirectDestination()->getAsArray();
}
/**
* Returns the redirect destination service.
*
* @return \Drupal\Core\Routing\RedirectDestinationInterface
* The redirect destination helper.
*/
protected function getRedirectDestination() {
if (!isset($this->redirectDestination)) {
$this->redirectDestination = \Drupal::destination();
}
return $this->redirectDestination;
}
/**
* Sets the redirect destination service.
*
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
*
* @return $this
*/
public function setRedirectDestination(RedirectDestinationInterface $redirect_destination) {
$this->redirectDestination = $redirect_destination;
return $this;
}
}
......@@ -206,7 +206,7 @@ protected function bookAdminTableTree(array $tree, array &$form) {
$delta = ($count < 30) ? 15 : intval($count / 2) + 1;
$access = \Drupal::currentUser()->hasPermission('administer nodes');
$destination = drupal_get_destination();
$destination = $this->getDestinationArray();
foreach ($tree as $data) {
$nid = $data['link']['nid'];
......
......@@ -159,7 +159,7 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
}
if ($this->authenticatedCanPostComments) {
// We cannot use drupal_get_destination() because these links
// We cannot use the redirect.destination service here because these links
// sometimes appear on /node and taxonomy listing pages.
if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == CommentItemInterface::FORM_SEPARATE_PAGE) {
$comment_reply_parameters = [
......
......@@ -167,7 +167,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $type = '
// Build a table listing the appropriate comments.
$options = array();
$destination = drupal_get_destination();
$destination = $this->getDestinationArray();
$commented_entity_ids = array();
$commented_entities = array();
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Url;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -24,6 +25,8 @@
*/
class Link extends FieldPluginBase {
use RedirectDestinationTrait;
/**
* Entity Manager service.
*
......
......@@ -53,7 +53,7 @@ protected function renderLink($data, ResultRow $values) {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = Url::fromRoute('comment.approve', ['comment' => $comment->id()]);
$this->options['alter']['query'] = drupal_get_destination() + array('token' => \Drupal::csrfToken()->get($this->options['alter']['url']->toString()));
$this->options['alter']['query'] = $this->getDestinationArray() + array('token' => \Drupal::csrfToken()->get($this->options['alter']['url']->toString()));
return $text;
}
......
......@@ -44,7 +44,7 @@ protected function renderLink($data, ResultRow $values) {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $comment->urlInfo('delete-form');
$this->options['alter']['query'] = drupal_get_destination();
$this->options['alter']['query'] = $this->getDestinationArray();
return $text;
}
......
......@@ -60,7 +60,7 @@ protected function renderLink($data, ResultRow $values) {
unset($this->options['alter']['fragment']);
if (!empty($this->options['destination'])) {
$this->options['alter']['query'] = drupal_get_destination();
$this->options['alter']['query'] = $this->getDestinationArray();
}
$this->options['alter']['url'] = $comment->urlInfo('edit-form');
......
......@@ -336,7 +336,7 @@ function content_translation_form_field_config_edit_form_alter(array &$form, For
// Provide helpful pointers for administrators.
if (\Drupal::currentUser()->hasPermission('administer content translation') && !$bundle_is_translatable) {
$toggle_url = \Drupal::url('language.content_settings_page', array(), array(
'query' => drupal_get_destination(),
'query' => \Drupal::destination()->getAsArray(),
));
$form['translatable']['#description'] = t('To configure translation for this field, <a href="@language-settings-url">enable language support</a> for this type.', array(
'@language-settings-url' => $toggle_url,
......
......@@ -11,6 +11,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
......@@ -24,6 +25,8 @@
*/
class ContextualLinks extends FieldPluginBase {
use RedirectDestinationTrait;
/**
* {@inheritdoc}
*/
......@@ -119,7 +122,7 @@ public function render(ResultRow $values) {
'title' => $title,
);
if (!empty($this->options['destination'])) {
$links[$field]['query'] = drupal_get_destination();
$links[$field]['query'] = $this->getDestinationArray();
}
}
}
......
......@@ -417,7 +417,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
}
if ($destinations) {
$destination = drupal_get_destination();
$destination = $this->getDestinationArray();
$destinations[] = $destination['destination'];
$form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
}
......
......@@ -290,7 +290,7 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
'#theme' => 'menu_local_action',
'#link' => array(
'title' => $this->t('Log in to post new content in the forum.'),
'url' => Url::fromRoute('user.login', [], ['query' => $this->getDestination()]),
'url' => Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]),
),
];
}
......@@ -298,11 +298,4 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
return $links;
}
/**
* Wraps drupal_get_destination().
*/
protected function getDestination() {
return drupal_get_destination();
}
}
......@@ -120,5 +120,5 @@ function template_preprocess_locale_translation_last_check(array &$variables) {
$last = $variables['last'];
$variables['last_checked'] = ($last != NULL);
$variables['time'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $last);
$variables['link'] = \Drupal::l(t('Check manually'), new Url('locale.check_translation', array(), array('query' => drupal_get_destination())));
$variables['link'] = \Drupal::l(t('Check manually'), new Url('locale.check_translation', array(), array('query' => \Drupal::destination()->getAsArray())));
}
......@@ -383,7 +383,7 @@ protected function buildOverviewTreeForm($tree, $delta) {
if ($edit_route) {
$operations['edit']['url'] = $edit_route;
// Bring the user back to the menu overview.
$operations['edit']['query'] = drupal_get_destination();
$operations['edit']['query'] = $this->getDestinationArray();
}
else {
// Fall back to the standard edit link.
......@@ -400,7 +400,7 @@ protected function buildOverviewTreeForm($tree, $delta) {
}
elseif ($delete_link = $link->getDeleteRoute()) {
$operations['delete']['url'] = $delete_link;
$operations['delete']['query'] = drupal_get_destination();
$operations['delete']['query'] = $this->getDestinationArray();
$operations['delete']['title'] = $this->t('Delete');
}
if ($link->isTranslatable()) {
......
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