0

I have a view which only displays a single result and I am using NID as the contextual filter. I cannot change that, it needs to be NID.

I want my page <title> to be the "Title field" of the node that was filtered rather than its NID.

As it is, in my contextual filter I can "Override title" by typing in "%1" which results in the terrible <title>1234 | My Site</title>. Assuming node/1234 is titled "John Doe", I want my title to be <title>John Doe | My Site</title>.

How can I accomplish this?

1 Answer 1

2

You need to "Specify validation criteria" in your contextual filter.

In your Contextual filter scroll down and check the box "Override title" but leave the text box empty.

Next, check the box "Specify validation criteria" and select "PHP Code" from the validator menu.

In the code box enter the following:

if($argument) { // Check that argument is present
    $myNode = node_load($argument); // Load node based on NID
    $myTitle = $myNode ->title; // Parse node data to get title
    $handler->options['title'] = $myTitle; // Change the view's page title to node's title
}
return true;
1
  • It might be worth noting that the Node caching behind node_load() should really only load "the node" used in the View once even though you're calling node_load here explicitly. I see code like this and sometimes shutter at the performance implications -- until I remind myself of this little detail.
    – tenken
    Aug 8, 2013 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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