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
Comment | File | Size | Author |
---|---|---|---|
#4 | advanced_url_view_args_2.patch | 12.72 KB | huangja |
advanced_url_view_args.patch | 7.13 KB | huangja |
Comments
Comment #1
mimpse CreditAttribution: mimpse commentedGreat 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
Comment #2
huangja CreditAttribution: huangja commentedNo problem, let me know if there are any bugs/issues. I'll look into applying it to userreference as well.
Comment #3
huangja CreditAttribution: huangja commentedComment #4
huangja CreditAttribution: huangja commentedUpdated userreference.module to include this functionality. I haven't had time to test it, so it was more or less a copy/paste job.
Comment #5
huangja CreditAttribution: huangja commentedComment #6
huangja CreditAttribution: huangja commentedComment #7
huangja CreditAttribution: huangja commentedComment #8
defconjuan CreditAttribution: defconjuan commentedHave other tested this? Seems promising.
Comment #9
superstar CreditAttribution: superstar commentedI tested the original patch and it worked well. Haven't tested the user reference one.
Comment #10
huangja CreditAttribution: huangja commentedgood 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
Comment #11
brainski CreditAttribution: brainski commentedAfter 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.
Comment #12
tazus CreditAttribution: tazus commentedsubscribed
Comment #13
mitchell CreditAttribution: mitchell commentedThis 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.
Comment #14
markus_petrux CreditAttribution: markus_petrux commentedIMHO, 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
Comment #15
paganwinter CreditAttribution: paganwinter commentedSubscribing...
Comment #16
betz CreditAttribution: betz commentedsubscribing
Comment #17
Parkes Design CreditAttribution: Parkes Design commentedsubscribe
Comment #18
csc4 CreditAttribution: csc4 commentedSubscribing
Comment #19
Yarkos1 CreditAttribution: Yarkos1 commentedHow to save this argument in CCK field?
Thanks.
Comment #20
vegeneric CreditAttribution: vegeneric commentedyou 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:
... 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.
Comment #21
Xaber CreditAttribution: Xaber commentedfine! thx! =)
Comment #22
SanDiego CreditAttribution: SanDiego commentedThank you very much. This is such as simple yet elegant solution. You saved me a great deal.
Comment #23
KarenS CreditAttribution: KarenS commentedSo 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.
Comment #24
ccshannon CreditAttribution: ccshannon commentedDoes 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 argnode/add/event/14
return arg(3);
Yet it doesn't work. Any ideas? Thanks.
Comment #25
finlaycm CreditAttribution: finlaycm commentedSame 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
Comment #26
ccshannon CreditAttribution: ccshannon commentedEven 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.
Comment #27
clashar CreditAttribution: clashar commentedsubscribe
Comment #28
ronchica CreditAttribution: ronchica commentedI 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.
Comment #29
mariusz.alef.bak CreditAttribution: mariusz.alef.bak commentedThe 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.
Comment #30
K.MacKenzie CreditAttribution: K.MacKenzie commentedMaybe 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
Anyone have any suggestions?
Comment #31
K.MacKenzie CreditAttribution: K.MacKenzie commentedRight 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
Comment #32
zatarain21 CreditAttribution: zatarain21 commentedThis soluction works in Drupal 7
Thanks vegeneric!! : )
Comment #33
TravisJohnston CreditAttribution: TravisJohnston commented@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.
Comment #34
TravisJohnston CreditAttribution: TravisJohnston commentedManaged 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.