Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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();
share|improve this answer
Have you considered making a module or a patch for views for this so others can more easily make use of your efforts? – rooby May 5 '13 at 6:00
I was just about to code this but found this...it would be great even in a sandbox on d.o. – jpstrikesback Jan 9 '14 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 Sep 2 '14 at 11:02

Use this module views_current_path

share|improve this answer

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.

share|improve this answer

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.
share|improve this answer
No need to hardcode a tags when l() can do it. – AyeshK Nov 1 '12 at 16:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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