Hi,

I'm not sure how to do this and can't find much info to help me, probably searching with the wrong terms as I think people have done this before :)

I have a series of nodes which have a cck field with a select widget with a couple of options (eg. bike, car, plane).
Now what I would like to do is to show a view in a block which loads a title list of nodes that have the same selected option for that cck field.

I have created a view that takes this CCK field value as an argument.
But how do I implement this view as a block so that it takes the CCK field value of the current node as the argument?

Thanx for the help!

Comments

bartezz’s picture

Status: Active » Closed (fixed)

DUH!

Was actually an easy fix... guess I need some sleep!
Funny thing, tyring something for hours without results and then after posting here I fix it within minutes.... happens to me a lot!

Anyhow, here's my solution;

As I said I added the CCK filed as an arugment.
Within the sttings of this argument I checked [Provide default argument] under [action to take if argument is not present]
Then added php code after checking [PHP code] under [Default argument type];

$node=node_load(arg(1));
if($node) {
	return $node->field_cckfieldname[0]['value'];
} else {
	return FALSE;
}

That did it :D Hope it helps others...

Cheers

brianbrarian’s picture

Thank you! Exactly what I needed. Works perfectly.

Though it took me a little while to figure out I had to substitute 'nid' for 'value' to display nodes that have the same value the current node has in a node reference field.

$node=node_load(arg(1));
if($node) {
return $node->field_mynoderef[0]['nid'];
} 
else {
return FALSE;
}

And then I added a second argument to prevent the current node from being listed in the view, as described here:
http://drupal.org/node/131482#comment-1173842

Thanks again.

jkomenda’s picture

Thank you so much for this! I'd not realized that 'field_cckfieldname' was the CCK name, and that ['nid'] could be ['value'].

nodecode’s picture

@Bartezz: Freaking brilliant! I've been struggling with this and the tutorials on Views 2 Arguments are terrible. You saved my sanity, way to go!

@brianbrarian: Also freaking brilliant! I see Bartezz's solution and *BAM* you've already connected the next piece of the puzzle that i needed to make it work.

Thank you! :)

bartezz’s picture

Always good to read it helped someone!

Cheers

techypaul’s picture

Thanks for popping back to update this, I needed it.

deng17’s picture

I followed exactly what Bartezz did. What I wanted is when viewing a specific node, it will show nodes with the same value in "field_series"

$node=node_load(arg(1));
if($node) {
return $node->field_series[0]['nid'];
} 
else {
return FALSE;
}

It worked perfectly through Panels by specifying "Node Title" in the context BUT it only works for only one node (which is a parent node). All its child nodes also have the same value in that cck field.

If I display it as a normal block (not the Panels like above), it doesn't show up at all.

bartezz’s picture

No experience with panels whatsoever... but it's logical that it'll only work with the parent node:

$node=node_load(arg(1));

Try it with path turned of, you'll see that the second argument in the path (0 based count, so nid is arg(1) ) will be the id of the parent node. For instance (node/123).

Not sure if this solution will work for you, sorry.

Cheers

deng17’s picture

Sorry for being a noob but I don't know how to turn the path off. How will I do that?

I tried creating a node with the same value in "field_series" also set it as a parent node.

I thought that the block would work with the parent node but it didn't. It only worked for the first node I created with that value in "field_series". That first node is also a parent node.

bartezz’s picture

Path module that is... disable it under admin/build/modules/list... if you have enabled it in the first place ofcourse. Again I have no experience with panels whatsoever.
What do your urls look like? Something like node/123 or something like example.com/about-us?

But again, I think this solution might not work with panels, maybe some one else could share a light in this?

Cheers

deng17’s picture

mydrupalsite/custom-url

I'm using Pathauto module. Hmm I think I can't disable my path module since I'm using it.

Nevermind the panels...the block also displays the same result.

deng17’s picture

Finally I had it working after reading how arguments work.

$node=node_load(arg(1));
if($node) {
return $node->field_series[0]['value'];
} 
else {
return FALSE;
}

I used nid before so it's reading from the node id itself. But by using 'value', it will read from the value of the cck field.

Thanks. I was just confused with the one who posted a modified code XD

naero’s picture

Thanks for sharing this! When I used this piece of code tonight, I had to make sure that on my content pane view I changed "Argument input" (under Pane Settings) to "No argument".

To pass the value of the term ID, I used:

$node=node_load(arg(1));
if($node) {
  return $node->field_profile_taxonomy[0]['value'];
}
jeroen87’s picture

you've made my day ! views 2 with the arguments is a bitch but thanks to you i finally got it right :D

thanks!!!!

PS shouldn't this kind of code be more documented because I found many users who are looking for it?

Zehrious’s picture

how can I use this same method to display nodes within a certain price range of $node->field_price[0]['value']?

gberm’s picture

How would this work if I wanted to use two different node types. I have two different node types with the same cck field and would like to display all of the nodes of the other type that have the same field.

Any help would be greatly appreciated.

Thanks

drupalfan81’s picture

Beautiful!! I have been wanting to do this for some time now, can't believe how easy it was. I have listings on my website and I wanted to show nearby listings based on a field in that node. Works perfectly! Thanks so much!

kdump’s picture

Has anyone got this working under D7.12? thanks.

1mundus’s picture

Also looking for a solution for D7. The suggestions from here don't work unfortunately.

pedrofsv’s picture

For Drupal 7, add the CCK field as Relationship, then add Content:Nid as Contextual Filter with that relationship assigned. Choose [Provide default argument] -> [PHP code] and type:

return ($node = menu_get_object()) ? $node->field_name_of_your_CCK_field['und'][0]['nid'] : FALSE;

Source: http://drupal.org/node/370227 and hours of craziness ;P

If you want to hide the current node from the view, just add another Content:Nid as Contextual Filter, check [Provide default argument], select [Content id from url] and, at the bottom of the window, under [More] link check [Exclude].