kleomash's picture

Hi!

I cannot resolve simple task and need help. May be I do something wrong.

I have node type 'event' and two node other types: 'event_page'  and 'event_request'. Each of them has field 'event_ref' referrencing to event.

At 'event_page' and 'event_request' I need a block with list of event_pages which has the same value of event_ref as the current one has.

How to use value of cureent node field as contextual filter in view?

Comments

drop's picture

Here's my recipe:

  1. Create a view of content (block display)
  2. Add a contextual filter for Content: nid
    1. in "When the filter value is NOT available" select "Content ID from URL"
    2. under "More" check the box for "Exclude"
  3. Add a second contextual filter for your field event_ref
    1. in "When the filter value is NOT available" select "PHP code"
      1. Pull the field value off the current node, with something like the following:
        // If we're on a node page... 
        if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) { 
        
          // Load the node. 
          $node = node_load(arg(1));
        
          // Check the key of the field using devel module.
          // dpm($node->field_event_ref[$node->langcode][0]);
        
          // Get and return the field value. 
          $lang = $node->langcode;
          return $node->field_event_ref[$lang][0]['value']; 
        }
  4. Save the view.
  5. Add the block to the layout.

One thing to note is that if your event_ref field is not a text field (if it's a reference field or a taxonomy field, for example) that final key will be something other than 'value'.

You can enable the devel module and use the dpm() function to check!