4
votes

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

3
I am not certain what you mean. Do you want to use this view to control which nodes appear as selections in a node reference field?jergason
Yes, i just want to use a view filtered by a argument as the entry of a content type's field. This field is a node reference and the argument is other field of the content type.JTB
If you want to embed the view based on the value of the other field, then I know how, and see my answer. If you want to dynamically change what nodes are possible to select in a node reference field during content creation based on the value of another field, then I don't think there is an easy solution. Drupal doesn't know anything about what you have selected until you hit the "Submit" button during content creation, so you would need to develop a custom module that used AJAX to update the select list for your node reference field once the other field had been set.jergason

3 Answers

2
votes

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

0
votes

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.

0
votes

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.