JTB
JTB

Reputation: 41

Link between CCK field and view

I want to use a view to select nodes in a content type field. This view must receive an argument that is another field of the content type. Can someone explain me how to pass the argument from the field to the view?

Excuse my poor english

Upvotes: 4

Views: 555

Answers (3)

Sid Kshatriya
Sid Kshatriya

Reputation: 5015

You might be able to use the Views Arguments Extras module. It will allow the argument of the view to come from a cck field. Some more details about this module (from its project page):

This module contains a group of view handlers and plugins that add the following options:

  • Argument Default Current Node CCK

    allows for cck field values of the current node to be loaded as default arguments

  • Argument Default Request Params

    allows for get and post params as default values

  • Argument Order Sort

    a sort handler, that allows for the order of items to be based on their order in a multi-value argument

Upvotes: 2

Jamison Dance
Jamison Dance

Reputation: 20174

If you just want to change what the view displays based on the value of a CCK field, the easiest way I have found is to embed a view into the template using views_embed_view(). Something like this in your template file would work I think:

//Use the dsm function to print out your $node object
//to get the name of the field you want to pass as an arg
//like this: dsm($node); 
//Assuming that the value of that field is in $node->cck_field['0']:
print views_embed_view('name_of_view', 'name_of_display', $node->cck_field['0'];

views_embed_view() only needs the first argument, the name of the view, to work. It will return the HTML for the default display of the named view. We pass it a specific display as a second argument. Anything after the second argument gets passed into the view as an argument, so we pass in the value of the field as an argument to the view. See this link for some documentation on how the function works.

Upvotes: 0

Kevin
Kevin

Reputation: 13226

I believe you can use the argument validation to validate the argument, and at that point you are free to change the $handler->argument value before it is passed in to Views.

Upvotes: 0

Related Questions