Change record status: 
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

rob230’s picture

But... how do you use the destination in your controller?

mandclu’s picture

Here's an example that works for me:
$element['#url'] = Url::fromRoute('node.add', ['node_type' => $this->options['bundle']], ['query' => $this->getDestinationArray()]);