Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

In Drupal 7, if you wanted to access the current Drupal path, you could do so by either calling current_path() or accessing $_GET['q']. In Drupal 8, the Drupal path is no longer stored in $_GET['q'], so modules and themes must be changed to call current_path() instead.

Drupal 7:

function contact_mail($key, &$message, $params) {
  ...
  $variables = array(
    ...
    '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
  ...
}

Drupal 8:

function contact_mail($key, &$message, $params) {
  ...
  $variables = array(
    ...
    '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
  ...
}

In Drupal 7, some modules would occasionally set $_GET['q']. For example:

function comment_permalink($cid) {
  ...
  $_GET['q'] = 'node/' . $node->nid;
  ...
}

In Drupal 8, this is not allowed (or rather, doing so, does not affect the current Drupal path). Instead, a subrequest can be invoked as documented in the Symfony request handling change record.

Impacts: 
Module developers
Themers

Comments

krknth’s picture

Druapl - 8.0.0-beta15
Look like current_path() function no longer exists. Alternatively we can use

$current_path = \Drupal::url('<current>', [], ['absolute' => FALSE]);
// For absolute url
$current_path = \Drupal::url('<current>', [], ['absolute' => TRUE]);
harika gujjula’s picture

Grt!! Thanks

niccottrell’s picture

Looks like this has changed again from the Drupal 8 beta. This currently works in 8.1.3:

$current_path = \Drupal::service('path.current')->getPath()