I've created a clickable block with Views and rewrote the fields with HTML like this:

<a href="user"> some divs... </a>

Clicking the block should refer the logged-in user to his/her profile. As the code mentions, I'm using user as relative link, but a problem occurs when the user clicks the link when he/she is already visiting his/her profile: the link is then turned into mysite/user/user instead of mysite/user.

Using /user doesn't work either, because it redirects the user to localhost/user instead of localhost/mysite/user.

What's the correct way to construct a path that takes into account either being relative or the $base_path?


There is no, and can not be always correct relative path.

Relative path is relative, and there is no infinite amount of paths to user profile, to be in the same relation with each and every path in your system. There cannot be. What you need is to properly generate an absolute path instead.

So instead of the code you are using, use Drupal's built-in link generator, l():

<?php print l('some divs', 'user'); ?>

or, if above is not feasible, url():

<a href="<?php print url('user'); ?>"> some divs... </a>
| improve this answer | |
  • Your answer looked promising, but apparently Views can't evaluate the PHP in your code. I'm currently using the code in a "Global: Custom text" field. – Jeroen May 9 '14 at 16:49
  • @Jeroen as a matter of fact views can evaluate PHP code. In tpl files. Just move your wrapper to tpl, where it probably belongs, and you are good to go. Anyway, my answer answers your question from the moment of posting. If you will change question's meaning and make this answer irrelevant, I'll go away and have a beer, and delete this answer when I'm back ;) Of course you can always ask a more specific question about URLs in views fields. – Mołot May 9 '14 at 16:52
  • I've tested your answer and the link to the user's profile worked correctly this time, although the view wasn't displayed as it should be (a result of my lack of experience with these kind of things). Would you mind taking a look at my question about this at drupal.stackexchange.com/questions/112711/…? – Jeroen May 9 '14 at 17:46
  • 1
    Yes! I've got it fixed now that I know how to write a template for the view. Let me give you another upvote for sparing me a lot of lost hours. – Jeroen May 9 '14 at 19:12
<a href="/user/!1"> ... </a>

will do the trick.

| improve this answer | |

if you create it with 'user/[uid]' it will be added to the current url.

Create it with a / in front to make it start at your domain, use '/user/[uid]'.

@Gregory: he is using a [token] so it's no code. More like a block in views or something.

To clarify my comment:

<a href="/user">profile</a> or /user in views as a link is always user specific. It brings anonymous users to the register/login page and logged in users to their own page. No need to put [uid] or !1 or something behind it.

| improve this answer | |
  • /user brings you to your own profile too you know. – Marcel May 9 '14 at 15:09
  • I guess the uid here comes from an input, we're not talking about the user's own profile... are we ? – Gregory Kapustin May 9 '14 at 15:12
  • Then just make a link to /user as @Marcel suggests :) – Gregory Kapustin May 9 '14 at 15:27
  • @GregoryKapustin: I did that, but the problem is still there. When a user clicks the link when he/she is already visiting his/her profile, he/she is redirected to mysite/user/user. – Jeroen May 9 '14 at 15:29
  • Event with <a href="/user">profile</a> ? – Gregory Kapustin May 9 '14 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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