4.6.x bootstrap.inc drupal_get_path_alias($path)
4.7.x path.inc drupal_get_path_alias($path)
5.x path.inc drupal_get_path_alias($path)
6.x path.inc drupal_get_path_alias($path, $path_language = '')
7.x path.inc drupal_get_path_alias($path = NULL, $path_language = NULL)

Given an internal Drupal path, return the alias set by the administrator.

If no path is provided, the function will return the alias of the current page.

Parameters

$path: An internal Drupal path.

$path_language: An optional language code to look up the path in.

Return value

An aliased path if one was found, or the original path if no alias was found.

6 calls to drupal_get_path_alias()
block_block_list_alter in modules/block/block.module
Implements hook_block_list_alter().
path_admin_overview in modules/path/path.admin.inc
Returns a listing of all defined URL aliases.
system_site_information_settings in modules/system/system.admin.inc
Form builder; The general site information form.
url in includes/common.inc
Generates an internal or external URL.
_shortcut_link_form_elements in modules/shortcut/shortcut.admin.inc
Helper function for building a form for adding or editing shortcut links.

... See full list

File

includes/path.inc, line 235
Functions to handle paths in Drupal, including path aliasing.

Code

function drupal_get_path_alias($path = NULL, $path_language = NULL) {
  // If no path is specified, use the current page's path.
  if ($path == NULL) {
    $path = $_GET['q'];
  }
  $result = $path;
  if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
    $result = $alias;
  }
  return $result;
}

Comments

becw’s picture

The opposite of this function is drupal_get_normal_path() -- given an alias, it returns the internal path.

swirt’s picture

I was doing a bit of research into which was faster, url() or drupal_lookup_path() or drupal_get_path_alias.
I found no declared speed winner, but I noticed that only the url() function is documented out to D8.

kingandy’s picture

They're different functions for different jobs. url() generates a whole path suitable for dropping directly into the HREF property of an A tag, including base paths (usually "/" but could be longer for subfolder installations). If the site doesn't have rewriting enabled, it could also be formatted as a query string (eg "/?q=node/1"). url() uses drupal_get_path_alias() (which itself uses drupal_lookup_path()) to get its path, but then it does some more with it; if all you want is the literal alias, you'd need to use drupal_get_path_alias().

For reference, the Drupal 8 equivalent to drupal_get_path_alias() involves the path.alias_manager service:

$path_alias = \Drupal::service('path.alias_manager.cached')->getPathAlias($system_path, $langcode);

... which is, evidently, much easier to read and remember.

charles1988’s picture

drupal_get_path_alias('node/add/register-case-study');

getlegalhelp’s picture

I am just trying to create a link to my Spanish version of the site located at mydomain.com/Spanish so that if the visitor is at mydomain.com/services, the link will dynamically add "services." (ie. a <href=/Spanish/[dynamic page name that is the same on both sites]>") Currently, no matter what page they are on, the link is static to take them to the Spanish homepage. (ie. <a href=/Spanish>) How can I tell drupal to grab the alias of the visited page (services) and add it to the link? (<a href=/Spanish/services>)

mattyy21’s picture

Same Problem. Have you resolve?

Coletain’s picture

This code will create a link to the current page in Spanish, given that your language code for spanish is "es"

print l(t('Se Habla Español'), '/es/' . drupal_get_path_alias(current_path(), 'es'));

Getting back to the default language is a bit trickier, since l() will automatically prepend the current language prefix. There might be a better way but this works (again assuming your language prefix for your default language is 'en':

print '<a href="/' . drupal_get_path_alias(current_path(), 'en') . '">View English Site</a>';

sarathkm’s picture

To get the full url with alias for a node use it like this.

url(drupal_get_path_alias('node/' . $node->nid))

squall3d’s picture

$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($system_path, $langcode);

Checkout https://www.drupal.org/node/1853148