3 namespace Drupal\Core\Routing;
6 * Wrapper methods for the Redirect Destination.
8 * This utility trait should only be used in application-level code, such as
9 * classes that would implement ContainerInjectionInterface. Services registered
10 * in the Container should not use this trait but inject the appropriate service
11 * directly for easier testing.
13 trait RedirectDestinationTrait {
16 * The redirect destination service.
18 * @var \Drupal\Core\Routing\RedirectDestinationInterface
20 protected $redirectDestination;
23 * Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
25 * @see \Drupal\Core\Routing\RedirectDestinationInterface::getAsArray()
28 * An associative array containing the key:
29 * - destination: The value of the current request's 'destination' query
30 * parameter, if present. This can be either a relative or absolute URL.
31 * However, for security, redirection to external URLs is not performed.
32 * If the query parameter isn't present, then the URL of the current
33 * request is returned.
35 protected function getDestinationArray() {
36 return $this->getRedirectDestination()->getAsArray();
40 * Returns the redirect destination service.
42 * @return \Drupal\Core\Routing\RedirectDestinationInterface
43 * The redirect destination helper.
45 protected function getRedirectDestination() {
46 if (!isset($this->redirectDestination)) {
47 $this->redirectDestination = \Drupal::destination();
50 return $this->redirectDestination;
54 * Sets the redirect destination service.
56 * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
57 * The redirect destination service.
61 public function setRedirectDestination(RedirectDestinationInterface $redirect_destination) {
62 $this->redirectDestination = $redirect_destination;