Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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

3 Answers 3

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);
    unset($form['help']);
  }

  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 at 18:01

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. –  Ayesh K Nov 1 '12 at 16:39

Your Answer

 
discard

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.