3

How do I get the URL alias of a node in views.

Here is a previous question, Get a url alias as a field value? However, the Content: Link field outputs something along the lines of

<a href="url">view</a>

I want to wrap the link around another field using rewrite results and tokens. Something like

<a href="/[url-alias]">[product:image]</a>

I was able to do this with

<a href="node/[nid]">[product:image]</a>

However, I would rather have it show the node alias than the node ID.

2
  • Have you tried changing the output using a Views template? In that template you could use drupal_lookup_path function to get the node's alias then output it to your View. Jun 13, 2013 at 4:39
  • @VanD just use node/[nid] format along with Global Redirect. It'll work fine. Don't need to do all these.
    – AgA
    Jan 24, 2014 at 5:10

4 Answers 4

8

If you click on Content: Path it will give you the a field that repesents the path of the node.

5
  • That will not give you the alias ......
    – AlxVallejo
    Jun 23, 2015 at 13:36
  • Hmmm weird I just double checked because the 5 years i've been developing in drupal it has, so maybe things have changed? Nope no they haven't. If you use pathauto like about 98% of site developers use. Adding Content: path to your view will return the path alias. Jun 23, 2015 at 13:59
  • Sorry, my bad, the nodes I were testing didn't have aliases assigned.
    – AlxVallejo
    Jun 23, 2015 at 14:19
  • 2
    Just reviewing this recently and realized my reply was not polite. I apologize for that. Oct 9, 2020 at 20:51
  • Also check Content: Link to Content. Output the URL as text Jul 1, 2023 at 22:00
6

Just an update for those than land here for Drupal 8.5+

The Path field no longer exists in views, you now need the "Link To Content" field instead. If you need to output the path only, then make sure you tick the "Out put URL as Text" option.

Reference: Issue raised on DO after Path field disappeared in Drupal 8.5.0

-1

Firstly, it is not a good idea to write the url in a href manually, because it won't work if you move your site to a subdirectory.

You can rewrite a field in view and click make this field a link giving the URL to the provided text field. You can use tokens as you wrote and this is the best practice to do it, so that you can always change the aliases and the link won't break.

Then you can install the Global Redirect module and set it to always use URL aliases. This makes your site also more optimized for search engines, preventing duplicate content penalty.

2
  • That's not entirely true it will still work if you have your rewrite base set up properly in your vhost or .htaccess setup
    – VanD
    May 20, 2014 at 15:02
  • 1
    @VanD However this is a better approach: It works regardless of web-server configuration.
    – hkoosha
    Jul 20, 2014 at 8:49
-4

Have you ever tried the Views PHP module? You could use it to do something like the following:

<a href="<?php print url('node/'.$data->nid)?>">Print Image Here</a>

Mind you, you will need to have the image field added before the PHP field. You will also need to exclude it so it doesn't show up twice.

1
  • 3
    That module has always been a terrible idea. It uses eval() to run PHP code that gets stored in the database. Its about the worst security issue you can have.
    – tobynew
    Aug 13, 2019 at 14:26

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.