3 namespace Drupal\Core\Routing;
5 use Drupal\Component\Utility\UrlHelper;
6 use Symfony\Component\HttpFoundation\RequestStack;
9 * Provides helpers for redirect destinations.
11 class RedirectDestination implements RedirectDestinationInterface {
16 * @var \Symfony\Component\HttpFoundation\RequestStack
18 protected $requestStack;
23 * @var \Drupal\Core\Routing\UrlGeneratorInterface
25 protected $urlGenerator;
28 * The destination used by the current request.
32 protected $destination;
35 * Constructs a new RedirectDestination instance.
37 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
39 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
42 public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator) {
43 $this->requestStack = $request_stack;
44 $this->urlGenerator = $url_generator;
50 public function getAsArray() {
51 return ['destination' => $this->get()];
57 public function get() {
58 if (!isset($this->destination)) {
59 $query = $this->requestStack->getCurrentRequest()->query;
60 if (UrlHelper::isExternal($query->get('destination'))) {
61 $this->destination = '/';
63 elseif ($query->has('destination')) {
64 $this->destination = $query->get('destination');
67 $this->destination = $this->urlGenerator->generateFromRoute('<current>', [], ['query' => UrlHelper::filterQueryParameters($query->all())]);
71 return $this->destination;
77 public function set($new_destination) {
78 $this->destination = $new_destination;