0

I have 2 content types. One called "tributes" and another called "offers". Both content types have a field reference to pages of a third content type called "county".

What i want is to display a view block with content from "offers" in the pages of content type "tribute" based on the value of the field county that appears in the node each time.

Is there a way to create a contextual filter on field county and use as default value the value of the field county appearing in the "tribute" node?? (through PHP perhaps?!)

Thanks in advance!

0

Getting a block view to get a parameter from the node view can be tricky. Here's what you can do:

For "tribute nodes":

  1. create a new view to display "offer" nodes
  2. add a filter to that view to limit the content type to "offer"
  3. add an argument to that view to your node reference
  4. Under "Action to take if argument is not present" select "Provide default argument"
  5. For default argument type, select PHP Code
  6. We now need to write some php code to grab the node reference id from the node being viewed. your case might be a bit different, but here's a starting point: (you dont need php tags)

    <?php
    
    if(arg(0) == "node" && is_numeric( arg(1) )) {
      $node = node_load(arg(1));
      if($node->type == 'tribute') {
        return $node->field_county[0]['nid'];
      }
    }
    

replacing field_county with whatever your cck field is named.

Then you should be able to add that block to any tribute node page and it will pull in the associated offers, and just do the same thing again for the offers->tribute direction.

  • First of all thank you so much for your quick reply. <br /> I just want to ask you two things: <br /> 1) when I put <pre>($node->type == 'tribute')<pre> 'tribute' is the machine_name of the content type? 2) Because my site is multilingual after the domain there is a /en. Does that count as an argument? So I have to put if(arg(1) == "node" && is_numeric(arg(2))) Does every "/" in the URL count as an arg?? Sorry if I'm being tiring but I don't really understand how URL args work and I'm also using aliases. Does that change things in args? – Argiris Feb 2 '12 at 22:17
  • 1. Yes, that's the machine name of the content type. 2. Not sure, I've never set up multilingual. I know that arg() returns the 0-indexed argument from the un-aliased path (so if you have a url alias on a node, it will still be node/nid). I'm not sure if drupal handles i18n paths by changing the actual path, the aliased path, or some other magic. I'd try it out like above, and if that doesn't work try incremental the values. You can also do a dpm(arg(0)) from a node page and see what you get. – adharris Feb 2 '12 at 22:21
  • thanks again! I will try some combinations with the args and see what i' ll get!! – Argiris Feb 2 '12 at 22:27
  • I get this error when I use arg(0) and arg(1) Notice: Undefined offset: 0 in eval() (line 4 /sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(50) : eval()'d code). Is it because the field_reference can take more than one values?? – Argiris Feb 2 '12 at 22:42
  • more than likely. You'll want to use devel and dpm($node) to see what the structure is for your node/fields – adharris Feb 3 '12 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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