Community Documentation

PHP contextual filters

Last updated December 20, 2012. Created by peteruithoven on December 20, 2012.
Log in to edit this page.

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.

  1. To get this, create a new view with a block display.
  2. Create a contextual filter, on the Answered questions field.
  3. Select Provide default value.
  4. Select at Type; PHP-code.
  5. Use the following code:
  6. <?php
      $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;
      }

    ?>
  7. Under More, select: Filter to items that share any term.

Comments

Thank you, peteruithoven.

Thank you, peteruithoven. This was exactly what I was looking for. I worked like a charm.

Page status

About this page

Drupal version
Drupal 7.x
Audience
Site builders
Level
Advanced
Keywords
contextualfilters, filters, Views

Structure Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here