I'm trying to build a custom profile page that contains a views' block displaying the nodes posted by the user of a particular profile page. The view should be visible to anonymous as well as logged in users.

I'm thinking I need a filter checking whose profile page is being looked at (doing something like 'Author = UID of profile page that's being looked at').

Is this possible with the current version of views (I'm using Drupal 4.7.5 with views 4.7.x-1.5)?

I know there is 'Author is Current User', but that only applies to logged in users. I could build the view with the 'Node: Author Name' filter, but then I would need to build a different view per user (which is possible, but not very practical).

So, am I simply missing a step, or should I post a feature request?

Thank you.

Comments

merlinofchaos’s picture

The trick here is that you need to figure out the context of the block, which is one thing that's currently difficult in Drupal as it doesn't have a concept of keeping context information. Hopefully we'll get this changed some in the future.

Views does have a place where you can put PHP code in order to try and find some context.

So what you want to is to set up your block view with the argument for the author's UID. In the PHP Argument Code section (in the arguments fieldset), enter this code (without the <?php part).

  if (arg(0) == 'user' && is_numeric(arg(1))) {
    return array(arg(1));
  }

Set the 'default' for this argument to page not found -- in a block context, it will simply make the block not appear. That way, the block will only appear if the URL begins with user/XXX and XXX is a valid number. If so, it'll render the view normally.

brunodbo’s picture

Status: Active » Fixed

Thanks a lot, that worked. Thanks also for explaining. Marking it as fixed.

Anonymous’s picture

Status: Fixed » Closed (fixed)
jenlampton’s picture

on your view, would you then also have to have the "arguement" of "User:UID is Author" selected? I don't understand how Arguments and Argument handling code interact with each other.

jenlampton’s picture

I figured it out. :-)

guojia’s picture

About #4 - I have the same question, but has not been able to figure it out - can you please enlighten me? When I add the custom argument, the block disappears. Thanks :)

bao truong’s picture

Version: 4.7.x-1.5 » 5.x-1.x-dev
Status: Closed (fixed) » Active

Hello,

I have a similar problem in trying to get a view with content specific to a certain user. Whenever I create such a view only displays content specific to the logged in user and not content specific to a chosen user.

I've been using the following argument handling code in the views:

if ($type == 'page') {
global $user;
if ($user->uid) {
$args[0] = $user->uid;
}
}

I would like to have menu tabs on each user profile page that will link to a panel or views with content created by that user whose profile i am looking at and not by whatever user is currently logged in which would by me most most of the time.

merlinofchaos’s picture

Version: 5.x-1.x-dev » 4.7.x-1.5
Status: Active » Closed (fixed)

Please don't take over old issues with new ones.

scarer’s picture

Version: 4.7.x-1.5 » 5.x-1.6

I have been trying for a couple of days now to produce a calendar view for a user on their profile page i.e. so they have their own calendar displaying on their profile. Please help!