I feel I must be missing something simple here, but I'll ask anyway.

I have a view that accepts an argument which is the nodeid of a node of a certain content type, in this case representing a person.

When someone visits the page display for the view with a valid argument in the url, I'd like the returned page's title to be the same as the title of the argument node, i.e. the person's name. When I try to do this using %1 in the "Title" option for a particular argument in the view setup, it just gives me the nodeid, rather than the name. So the title is, for example, "265" instead of "Bob so-and-so".

Any advice?

Thanks,
Macho

Comments

matkeane’s picture

Hi,

The reason you're only seeing the node ID rather than the name is that Views, by default, only knows about the data stored in the node being displayed - and the node-reference field is just a numeric ID of another node. If you want to load extra data from the referenced node to show the other node's title, you can use Relationships to tell Views to load the node referenced by the argument. However, I'm not sure - not having tried this - that you will be able to use the extra fields from the Relationship in the 'title' field part of the UI.

One workaround for this - and not necessarily the most elegant - is to modify the title variable in a page_preprocess function. I did this recently to change the title for different vocabularies on the Taxonomy page view.

It looks like I could also have achieved this using hook_views_pre_view (more info about Views hooks here: http://drupal.org/node/99567 and an example here: http://agaric.com/note/modifying-a-view-with-a-views-hook-example-custom...)

sapark’s picture

I'm not sure but I *think* you could add the title of the argument node (person's name) as a field, choose not to display it, then you could use it as %2. Hope that helps.

zsiswick’s picture

I have tried that approach and found that it works fine. It would be good if there was a mention in the field instructions that arguments will work

macho’s picture

The node that identifies a person has the title as the person's name. But if I add in "Node: Title" as a field in the view that will be the title of the node whose nid references the person.

I hope I am explaining that clearly.

macho’s picture

in case anyone happens upon this with the same question, this node seems to answer at least the part of the question about how to use php code to redefine a view title:

http://drupal.org/node/365418

i'm still not sure how to access the first argument from the php code, though.

macho’s picture

BrianLewisDesign’s picture

In your view, click Header. Put this (with input format: php):

$view = views_get_current_view();
$node = node_load($view->args[0]); // args[0] will get %1 node id
drupal_set_title($node->title); // set node title as view title