What's the best way to get access to the current path in Views?

My use case is I want to use the current page path in a text area (i.e. I want to have a text field like More).

At the moment I'm handling it by using hook_views_re_view to rewrite a "Global: Custom Text" field value with drupal_get_path_alias().

Ideally I'd like something more portable, i.e. the ability to add a "Global: Current Path" field to my view.

What combination of hooks/handlers/plugins do I need to use?

4 Answers 4


Thanks a lot. I did go down the Views PHP route initially, but it struck me as a bit un-Drupally, and even Views PHP says to use handlers where possible.

In the end I used hook_views_data and did:

function mymodule_views_views_data() {
  $data['views']['current_path'] = array(
    'title' => t('Current Path'),
    'help' => t('Provide current path.'),
    'field' => array(
      'handler' => 'concept2_views_handler_field_current_path',
  return $data;

 * Implementation of hook_views_handlers() to register all of the basic handlers
 * views uses.
function mymodule_views_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'mymodule') . '/handlers',
    'handlers' => array(
      // The name of my handler
      'mymodule_views_handler_field_current_path' => array(
        // The name of the handler we are extending.
        'parent' => 'views_handler_field',

With a handler file like:

 * A handler to provide a field that is completely custom by the administrator.
 * @ingroup views_field_handlers
class mymodule_views_handler_field_current_path extends views_handler_field {
  function query() {
    // do nothing -- to override the parent query.

  function option_definition() {
    $options = parent::option_definition();

    // Override the alter text option to always alter the text.
    $options['alter']['contains']['alter_text'] = array('default' => TRUE, 'bool' => TRUE);
    $options['hide_alter_empty'] = array('default' => FALSE, 'bool' => TRUE);
    return $options;

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

  function render($values) {
    // Return the text, so the code never thinks the value is empty.
    return drupal_get_path_alias();
  • Have you considered making a module or a patch for views for this so others can more easily make use of your efforts?
    – rooby
    Commented May 5, 2013 at 6:00
  • I was just about to code this but found this...it would be great even in a sandbox on d.o. Commented Jan 9, 2014 at 18:01
  • Great idea. I've taken this code, cleaned it up for Views 3.x and added it to a Drupal 7.x sandbox project. drupal.org/sandbox/hargobind/2331349
    – hargobind
    Commented Sep 2, 2014 at 11:02
  • Correction, the sandbox is now a full project at drupal.org/project/views_current_path
    – hargobind
    Commented Oct 24, 2017 at 21:44

Use this module views_current_path

  • 1
    I created that module based on the code by @Apemantus above :)
    – hargobind
    Commented Oct 24, 2017 at 21:45

Depending on what you need to do, you can choose one.

l() : print l(t('click here'), 'node/5'); This should print a link (a tag) with correct path of node/5. Aliased, and base paths are added automatically. Strongly recommended.

url(): print url('node/5'); This will print the correct path without an a tag. Base paths will be added. See api.drupal.org for its extra parameters.

request_uri(): this will give the current request uri.

base_path(): this will give base path of drupal site.


Have you checked out Views PHP? It has some bugs when accessing the $row variable but it works great for injecting PHP into a Views field.

You could use request_path() in a Views PHP field:

Whatever text you want plus a <a href="<?php print request_path(); ?>">link</a> to the current page.
  • No need to hardcode a tags when l() can do it.
    – AKS
    Commented Nov 1, 2012 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.