When using the nodereference CCK field, I can use views to determine which nodes can be referenced, but I can only pass arguments to the views from the fields settings under administration. I wanted functionality where it's possible to pass the arguments in the url to the nodereference field (with the same format that the Views module uses).

I modified nodereference.module and content.node_form.inc to include this functionality. Essentially, the views arguments are pass along using the arg() functions to populate the arguments for the views. If javascript (autocomplete textfield) is involved, it also passes the arguments through the callback. I also include a checkbox that enables this; otherwise, nodereference goes on functioning as it is now.

content.node_form.inc is changed solely to allow it to work with the 'Add More' button when it's set for unlimited autocomplete textfields.

For example, I can enable this functionality and set my advanced view argument to '%1' for a particular nodereference field. This means my node form can be 'node/add/content_type/%1' or 'node/node_id/edit/%1'. Whatever is put into %1 will be the first argument of the view used to determine nodes that can be referenced. Additionally, I can place multiple arguments as well (node/add/content_type/%1/%2/%3....).

So if I have a nodereference field 'city' that uses a view which takes [state_title] as an argument. If I type 'node/add/content_type/michigan' in the url, then the city field will only populate with options of cities in Michigan, assuming I created my view correctly.

I'd be great if this patch can be reviewed and hopefully commited. Suggestions are welcome.

James

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mimpse’s picture

Great timing, James - you submitted this just as I needed it! Might look into applying this to userreference too, unless you're already doing that...

Thanks

Simon

huangja’s picture

No problem, let me know if there are any bugs/issues. I'll look into applying it to userreference as well.

huangja’s picture

Issue tags: +CCK
huangja’s picture

Title: Passing advanced view arguments to nodereference field through URL » Passing advanced view arguments to nodereference/userreference field through URL
Assigned: Unassigned » huangja
FileSize
12.72 KB

Updated userreference.module to include this functionality. I haven't had time to test it, so it was more or less a copy/paste job.

huangja’s picture

Issue tags: +userreference
huangja’s picture

Component: nodereference.module » Views Integration
huangja’s picture

Assigned: huangja » Unassigned
defconjuan’s picture

Have other tested this? Seems promising.

superstar’s picture

I tested the original patch and it worked well. Haven't tested the user reference one.

huangja’s picture

good to hear that it's working well.

Couple of things:

Since it's trying to pass values through the url, it uses the arg() functions regularly, this means that if there is ever some sort of javascript or AHAH/AJAX invoked, it will probably break it. It's designed only to work on the primary node form pages (node/add/content_type and node/node_nid/edit).

That being said, I did include a workaround for the autocomplete textfield widget, which does use javascript. This is the reason why there's a change in content.node_form.inc, so that the arguments can be passed through the menu callback.

I appreciate the feedback.

James

brainski’s picture

After appling this patch, passing arguments works well BUT:

The FUpload Module ist broken and I'm not able to use this module anymore because it uses a menu hook like this:

function image_fupload_menu() {
..
node/add/%image_node_type/list_imagefields
..
}

Maybe we should review the patch and think about a solution that both modules can coexist.

tazus’s picture

subscribed

mitchell’s picture

This is exactly what I'm looking for! The patch looks like it's a little bit dated, but I'll try to figure out how I can help asap.

markus_petrux’s picture

IMHO, views arguments for noderef fields based on URL arguments is not a good approach. It will break as soon as the node is edited from a different URL, or created programmatically. Hence, I would say "won't fix" here.

There's another issue that's a bit more flexible, based on tokens, but it needs more work because it is not easy to provide the current node to all places where the noderef view is going to be executed.

#196518: Token support for advanced view argument

paganwinter’s picture

Subscribing...

betz’s picture

subscribing

Parkes Design’s picture

subscribe

csc4’s picture

Subscribing

Yarkos1’s picture

How to save this argument in CCK field?
Thanks.

vegeneric’s picture

you can do this with views. add an argument to your view, and under "Action to take if argument is not present" select "Provide default argument". then select "Use PHP code" and you can put in a single line of code to reference the needed value from the url. for example, put something like this in the textarea:

return arg(3);

... if you want to reference the fourth argument (as in my case: node/add/type/**UID**). save the view, and then any user or node reference fields you use with it will automatically be get that value as an argument.

Xaber’s picture

fine! thx! =)

SanDiego’s picture

Thank you very much. This is such as simple yet elegant solution. You saved me a great deal.

KarenS’s picture

Status: Needs review » Closed (won't fix)

So I think the answer is we won't add a new feature, there are tools you can use for some things and others just won't get in.

ccshannon’s picture

Does not work for me. I've done exactly these steps and the View does not respond to the argument.

I've put in return 14; and it works, so I pass '14' in as the fourth arg

node/add/event/14

return arg(3);

Yet it doesn't work. Any ideas? Thanks.

finlaycm’s picture

Same here

if I add
return 584; it works

and if I add
return arg(3);

It does not work when I try to pass an argument from mysite.com/node/add/report/584

Why?

Thank you,

More if the PHP code is

if (arg(0) =='node' ){
return 584;
}
This does not work either from the add node page. (mysite.com/node/add/report/584)
which means the view is not getting the arguments from the page URL.
If I am in the view UI page and I add

if (arg(0) == 'admin'){
return 584;
}

Then it works,

What is happening here?

Thank you

ccshannon’s picture

Even though this issue is closed, thought I'd chime in why it didn't work for me. I was putting the arg in the wrong place. I thought the argument needed to be passed in the INPUT box in the nodereference field config. I was missing the obvious, that vegeneric was referring to the argument handling in Views module. That went right over my head the first time.

By using the PHP field in Views argument under 'Provide default argument' you can indeed obtain arg() values. I've done this for all kinds of Full-page and embedded Views. I just wasn't thinking of this "Widget View" as being able to do the same thing, but of course it can!

In other words, don't use the Views argument in the noderference config. Use the Views argument default value in Views.

clashar’s picture

subscribe

ronchica’s picture

I ran into the exact situation you are describing when trying to use arg() to pass an argument into a nodereference field view. Didn't work. Spent a few hours trying to figure out why something that everyone says should work, isn't...

In my case, the problem appears to be that my nodereference field was autocomplete. I changed it to a list and the correct argument is being used as the view's default argument from the url of page. I think this has something to do with javascript upsetting the url arguments when managing the autocomplete.

mariusz.alef.bak’s picture

The problem with the method proposed by vegeneric when using autocomplete field is that the path for which view is being executed is the AJAX callback path, not the path to the page containing the node form. So the path is not node/123/add/pupil, but rather something like nodereference/autocomplete/field_pupil_class/abc, where abc is the text that should be autocompleted.

I do not know if the patch proposed in this issue has the same flaw, but it seems that it takes this problem into account by passing path to autocomplete callback. Replacing autocomplete widget with simple select list also solves the problem, because in this case the view assigned to the nodereference field is being executed for node/123/add/pupil path.

K.MacKenzie’s picture

Maybe I am missing something fundamental here, but is there some reason why a $_GET parameter couldn't be passed? Seeing as you can reference the value by a specific key...

for instance when on a node/add or edit form, if you pass the url something like &city=287422 it could offer you in the drop down only the nodes that reference the city with that node id?

I have done everything to get this working, and in fact it does work until I click "add more values" button to get another field, then it breaks. I am appending the $_GET variable to the ahah path in node_form.inc but its not working.

I am using cck 3.0 dev, and I hacked this code into node_form.inc just trying to come up with a "foot in the door" solution.

node_form.inc

--Line 239--
	if(isset($_GET['league'])) {
		$path_append = "?league=".$_GET['league'];
	}
...
      '#ahah' => array(
        'path' => 'content/js_add_more/'. $content_type['url_str'] .'/'. $field_name . $path_append,

Anyone have any suggestions?

K.MacKenzie’s picture

Right after I posted this I realized that content_multigroup had a different callback (should have been obvious I know). After applying my little hack to the content_multigroup.node_form.inc I discovered the answer to my question, aparently adding a get parameter to a callback URL completely breaks the callback. However, I did get my hack working by simply switching to the arg() method.

Seeing as the arg can appear in two different places, I just did a little check in the view arguments php code to find the right one.

node/add/theater/VARTOPASS
Here its arg(3)

content_multigroup/js_add_more/theater/group_theater/VARTOPASS
here its arg(4)

Solution is simple... check to see if (arg(3) == 'group_theather') and act accordingly.

Hope that helps anyone that is either using content_multigroup module or someone who is having trouble with their args running around to different positions in their url.

Good night drupal.

PS. here is my code/hack to content_multigroup.node_form.inc

 /*LINE 702*/

	if(is_numeric(arg(3))) { //  HERE!
		$path_append = arg(3);
	}
  $form_element = array();
  $form_element[$group_name .'_add_more'] = array(
    '#type' => 'submit',
    '#name' => $group_name .'_add_more',
    '#value' => theme('content_multigroup_add_more_label', $group_name),
    '#weight' => $group_multiple + 1,
    '#submit' => array('content_multigroup_add_more_submit'),
    '#ahah' => array(
      'path' => 'content_multigroup/js_add_more/'. $content_type['url_str'] .'/'. $group_name .'/'. $path_append, // AND HERE!

zatarain21’s picture

This soluction works in Drupal 7

Posted by vegeneric on February 9, 2010 at 9:45pm
you can do this with views. add an argument to your view, and under "Action to take if argument is not present" select "Provide default argument". then select "Use PHP code" and you can put in a single line of code to reference the needed value from the url. for example, put something like this in the textarea:

return arg(3);

... if you want to reference the fourth argument (as in my case: node/add/type/**UID**). save the view, and then any user or node reference fields you use with it will automatically be get that value as an argument.

Thanks vegeneric!! : )

TravisJohnston’s picture

@vegeneric,

What Contextual Filters are you using for the php argument? This sounds promising but not sure which one you are using.

I am trying to accomplish this with a couple of reference fields in Drupal 7. I am making a Project Management feature with Projects, Tasks, Milestones, Project Members, and Assignees.

My goal is that when you click Add Task or Add Milestone on the Project node's page, it will auto populate the Project Node Reference field that is in Task and Milestone, this way people don't get confused and that field is already filled for them.

I am also trying to accomplish this with Assignees. Projects have a Project Members field where you include the users that are a part of the project. My goal with this is when you click Add Task, the Assignee field only shows the users related to the referenced Project.

TravisJohnston’s picture

Managed to get the Tasks and the Milestones to automatically fill in the Group Audience and Project Node Reference fields by creating two buttons on the Project Node Page, Add Task and Add Milestones with the following links.

Additional module that was required, Node Reference URL.

/node/add/tasks/[nid_2]?destination=[path]&og_group_ref=[gid]
/node/add/milestones/[nid_2]?destination=[path]&og_group_ref=[gid]

I did this in views, placed the two links in the view header. The [nid_2] is a NID field in my view. The field has a relationship to the field_project reference field. The same goes with [path].

Hope this helps someone.