By dawehner on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0.0-beta8
Description:
Drupal 7:
url('path', array('query' => drupal_get_destination());
Drupal 8:
In procedural code you can access the redirect.destination service as follow:
Url::fromRoute('route_name', [], ['query' => \Drupal::service('redirect.destination')->getAsArray()]);
// or
Url::fromRoute('route_name', [], ['query' => \Drupal::destination()->getAsArray()]);
For application-level code, such as classes that would implement ContainerInjectionInterface was introduced the RedirectDestinationTrait.
FormBase and ControllerBase already make use of it.
If your class is neither a controller or form, you can use the RedirectDestinationTrait
Example:
use Drupal\Core\Routing\RedirectDestinationTrait;
class MyClass {
use RedirectDestinationTrait;
/**
* Does something.
*/
public function doSomething() {
// ...
$destination = $this->getDestinationArray();
// ...
}
}
drupal_get_destination() remains for backwards compatibility and will be removed before 9.0.0
Impacts:
Module developers
Comments
But... how do you use the
But... how do you use the destination in your controller?
Here's an example that works
Here's an example that works for me:
$element['#url'] = Url::fromRoute('node.add', ['node_type' => $this->options['bundle']], ['query' => $this->getDestinationArray()]);