PHP contextual filters
Last updated on
21 June 2017
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
When you want to do more advances contextual filtering you could use PHP contextual filter code.
Example:
There is a questions taxonomy with a number of questions.
There is a content type with 2 term reference fields, Raised questions and Answered questions
Under each page I want a list with links to pages that answer questions raised on the page.
To get this, create a new view with a block display.
- Create a contextual filter, on the Answered questions field.
- Select Provide default value.
- Select at Type; PHP Code.
- Use the following code:
-
$node = node_load(arg(1)); if($node && isset($node->field_raised_questions[LANGUAGE_NONE])) { foreach($node->field_raised_questions[LANGUAGE_NONE] as $term) { $terms[] = $term['tid']; } return implode('+',$terms); } else { return; }
- Under More, select: Filter to items that share any term.
Here is screenshot of one of example of taxonomy.
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion