How do I: filter a view based on the CCK field value of the current node

henno.gous's picture

So basically what I want to achieve is the following:

I have two content types, lets say a "story" and an "advertisement". I have appended two CCK fields (currently just text, but eventually locations) to my stories, while another CCK field (also currently just text, but eventually a location) is appended to my advertisement.

Now I want to display two blocks below my story nodes. The first block should display advertisements where the advertisment's CCK field value matches that of the first CCK value appended to the story, and another block showing ads where the advertisement's CCK field value matches that of the second CCK value appended to the story.

From my background reading I understand that including an argument with PHP code that will load the current node's CCK values into parameters and then filtering from there would be the way to go, but I'm not quite sure how to approach this.

Eventually I would like to improve this system to the point where the ads are filtered according to proximity (e.g. the location associated with the advertisement is within a 10 mile radius of the story's first associated location, and is therefore displayed), but for the time being simple matching of the CCK text fields wil suffice.

Any ideas?

South Africa


i dont know the answer...

IrishGringo's picture

but I wanted to add something to the question. Is there a way to use the context by assuming that there is a common way of addressing a global $var. I was looking for a way to share an argument across block views, perhaps using PANNELS, perhaps I did not put enough time into it, but I get a message from someone else saying that they could never get it to work, so I gave up as I was short on time. But this seems like something that should be very doable. Perhaps I my understanding of context is wrong some how. Or perhaps there is a smarter way of doing it.

One way I did something like

nvahalik's picture

One way I did something like this was to use panels templates to "override" the default node template for the "story" node type. From there, they can all share context such as the parent node... Then, at the bottom of the page you can include a block or a separate view which gets the correct parameter passed to it.

I'm not sure about passing that field value, but one way might be to add an argument for the node ID and a relationship on that particular CCK field to the view and force the use of the relationship for the results of the view.

Another way might be to add the CCK field as an argument and then use tokens to pass it directly as a parameter to the view.

passing a CCK field via an argument

henno.gous's picture

Thanks for your response... could you maybe give me a clue as to how to pass a CCK value with an argument?

Something like this article

nvahalik's picture

Something like this article here seems to give a better idea of setting up "related" content with views. Give that a shot.

This works

HongPong's picture

This saved the day for me! In the views argument - by PHP style:
Similar Node Title:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  return $node->title;
} else {
  return FALSE;

To use CCK Field Argument:

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

awesome sauce! I've been

vaccinemedia's picture

awesome sauce! I've been looking all afternoon for this solution. I have a series of Stores in a shopping mall which have job vacancies available and the jobs are referenced to the store - so creating a view for the store which lists all available jobs is easy. My problem was creating a "other jobs available at this store" when looking at the detail for the job - i.e. viewing the job as a node with the block on the left column so I had to pass the CCK field from the node to the block :)

change drupal page layout / block based on a node field

Alauddin's picture

i know this is old post, but wanted to reference this module that will let you do the same with context module and the context_entity_field


carlodimartino's picture

This is exactly what I was looking for over 2 years after your post!