I have a view that selects a subset of book child nodes of a given type, where the parent book id is passed as argument. This works fine when I pass a given nid as parent book id in the view preview.

I have a CCK node reference that uses that view under Advanced - Nodes that can be referenced (View), selecting the created view under View use to select the nodes:.

How can I pass the nid of a specific node with an instance of the node reference field to the view under View arguments, which just says:

Provide a comma separated list of arguments to pass to the view.

I searched hi and lo for documentation on it, including CCK advanced help, and searched (including google), and all I've found so far is lots of similar questions, unanswered such as:

How do you pass argument from CCK field to a View?

I have token enabled (I thought maybe one can use tokens as arguments to the filtering view).


webel’s picture

Webel IT Australia, "Elements of the Web", Scientific IT Consultancy,
For PHP-driven Drupal CMS web sites, Enterprise Java, graphical UML, UML Parsing Analysis, SysML, XML.

webel’s picture

Webel IT Australia, "Elements of the Web", Scientific IT Consultancy,
For PHP-driven Drupal CMS web sites, Enterprise Java, graphical UML, UML Parsing Analysis, SysML, XML.

webel’s picture

I've figured out how to do what I need thanks to Advanced Help for views:

Action to take if argument is not present

Provide default

If no argument is given, a default argument can be selected. The method of choosing the default argument is selectable and pluggable.

Default arguments

Node ID from URL

This will attempt to find a Node ID from the URL, and is primarily useful for the Node: ID argument (though other arguments that use Node IDs, such as the CCK Node Reference argument, will find it useful too). For example, when visiting the path 'node/1' or 'node/1/edit' it will know that the '1' in that path is a node ID and use it.

Thanks views maintainers for this useful feature.

Webel IT Australia, "Elements of the Web", Scientific IT Consultancy,
For PHP-driven Drupal CMS web sites, Enterprise Java, graphical UML, UML Parsing Analysis, SysML, XML.

dbronst’s picture

Thanks for the post webel. I believe that I am trying to accomplish the same thing as you did, but for some reason the method you used is not working for me. Perhaps I'm missing something.

I have a content type containing a Node reference field which uses auto-complete to present a set of nodes. Like you, I'm using a view to limit the set of nodes presented in auto-complete.

The view I'm using first limits the selection on Node Type via a filter (node: Type = a_specifc_node_type). This is quite simple and works fine.

I'm trying to further limit the set of nodes presented by the auto-complete based on the nid of the node which is currently being edited (The nodes I'm trying to present are child nodes and have a field containing the parent nid.) I use an argument to do this and my view works if I set a specific nid via the "arguments" field in the view's "Live Preview" or if I set the argument via the "View arguments" field in the content type and edit an instance of the content type. However, like you I need to have the nid that is passed to the view be based on the Node that is currently being edited, and not a static value.

So I configured the argument as suggested in your post above with "Action to take if argument is not present" set to "Provide default", and the "Default argument type" set to "Node ID from URL". Unfortunately, when the view is accessed by the auto-complete, it does not seem to be able to get the node id from the path of the node that is being edited.

Am I completely misunderstanding what you are doing? Or perhaps I'm missing a configuration or module which allows the view to grab the node id? I'm reasonably sure that I've got my content type and view configured correctly as it works fine when I pass the a static value via the node reference field's "View arguments" field.

Thanks for any help.

dbronst’s picture

Looks like my problem is this: https://drupal.org/node/400294#comment-1350676

Unfortunately, I need the autocomplete field functionality. I've tried to apply the patch off the above link, but have not got it to work yet. Perhaps because I'm using the node reference create module on top of the node reference module, but more likely because the nodereference.module has changed quite a bit since the patch was created for it.

chichilatte’s picture

I got that patch working with a little tweak (I made a comment on that page you linked to). Thanks for directing me there in the first place.

dcote197’s picture

Thanks for the explanation Webel. It seems straightforward, but it didn't solve my View argument.

I have two custom content types, let's say CCT1 and CCT2. For CCT1 I have a CCK node reference field that displays a list (which I created using a View) of CCT2 nids. The user must select -- and consequently, associate -- one CCT2 from the list.

I then created a new View that uses a Relationship for the CCK node reference field. For the argument, I followed your instructions for setting the Node ID from URL, and specified the node types: CCT1 and CCT2.

The View is a custom block that appears at the bottom of a CCT2 node/page. My site has many CCT2 node/pages. Currently, the View block is displaying all CCT1 nodes, when I only want it to display CCT1's that were specifically created for a particular CCT2 node.

The only way I've been able to display particular CCT1 nodes in my View block on a CCT2 page is by adding a filter for nid, but that option only gives me an "is equal to" (or similar inappropriate options) and a Value. I thought I could enter a token or something in the Value field, but I can't get it to work.

Any help is appreciated.

calefilm’s picture

I appreciate you putting forth an effort to explain this. I solved my problem following your simple procedure. THANK YOU

I actually set my Default argument: User ID from URL

Because of my argument type: "User relationships: Requester user", I needed to focus on the user as I had created a filter for User Reference--to filter out only a user's friends.

In fact my view looks very similar to the view that comes with the Relationships module: "user_relationships_browser" view, but needed your adjustment to get things to work right on my form.


calefilm’s picture

lol, I had the same problem - but in a completely unrelated way. Solved it again!

This time, I was trying to grab the Nid from the URL: www.mysite.com/node/1138/edit

So I have a node reference field, where I configure it for Select List. I'm trying to grab that NID and place it into the field so I can delineate this field according to the current author AND Nid.

Anyway, point is, I resolved this by going into views, Node view.

In arguments, select Node: Nid

click: Provide default argument

under: Default argument type, select: Node ID from URL

2dareis2do’s picture

I would like to use an entity reference view to display a list of node items of entity type 'article' . This works well.

I would also like to use a field value of the edit/add entity type that I am creating reference from and use a field value from that same 'issue' entity type that is used as an argument to filter the referenced nodes, in this case by issue number, which is currently populated using a numeric list type.

Is there any way to do this? How could I do this using the php code option to reference the 'issue number' field that I would like to use as an argument or otherwise?

emawk7’s picture

I have a jobApp content type and this has a node reference field that references a job content type. If someone creates a new application, the job field should provide a drop-down of all jobs but if someone visits a job ad directly and clicks "apply", they should be taken to the create page for a job app but with that job alone in the job menu. For the node reference field in job app, I used the "Advanced - Nodes that can be referenced" too and chose a view that displays all job titles if no argument is present and if the node id of a specific job is passed to the view, only that one job is displayed. So, this view correctly shows the names of all jobs when I don't put any arguments in the "View arguments" field of the content type field configuration but once I put an argument there, nothing displays in the job menu. The "apply" link takes me to node/add/application/15 so I have the node id of the job in question. How do I pass this to the view to select the appropriate node?

Any assistance would be greatly appreciated.

dgitonga’s picture

The module will allow you to reference the job-id in the url on the application page to give the node reference field an auto-populated value of the id title.

Can be found here -> http://drupal.org/project/nodereference_url

ontoligent’s picture

I am still trying to find an answer to the original question, which I understood as:

What is the format for the arguments that go into the field in the "VIEWS - USERS THAT CAN BE REFERENCED" section of the form that defines the User Reference field? Do you use field names in token format (e.g. [group_audience])? WIthout brackets? Some other format? Nothing seems to work for me.

The same question applies to all reference fields that want to use a view.

mikepvd’s picture

This is from a quick perusal of the code, so if I am way off base please forgive me.

From what I can tell, the arguments you specify are stored in the field settings. This is the general field information that is stored for ALL places the field is used. References accesses this information to supply the options you see in the form. However, it does this using hook_options_list which adds the options direct to the field definition on load. In addition, it calls the view directly, passing it the stored arguments from field settings. So, in essence it acts similarly to how the view would react if you typed the arguments into the form field for 'preview'.

So, short answer is that you can put anything that a conditional filter will understand as direct input in the view arguments but that is it. Nothing dynamic or based on other form input.

happysnowmantech’s picture

FYI I submitted a patch for this in D7:

Dynamic views arguments in node reference

shveta08’s picture

I have tried the patch as mentioned but I can't get this working. I want to populate one select box based on the selection of the other select made while creating a custom content type. I have made the necessary advanced view for field2 which requires the ID of the previous field say field1 to be passed(Content ID from URL). Now, I am unsure of exactly which token should be passed as the View arguments as the id of field1 is not present on the same page. Please let me know if this is an achievable task for the said setup. Thanks !