Problem/Motivation
Suppose I use views where I set contextual filter to filter by NID and I use Provide default value - Content ID from URL. This is perfectly working on node view or node edit pages but in case the view is being rendered for ajax callback where the ajax callback is called from node/edit page no NID value is provided. This is due to a fact that the url of such a callback is system/ajax and Provide default value - Content ID from URL is trying to get node with menu_get_object from url (implemented in views_plugin_argument_default_node->get_argument()).
One of concrete cases where this issue is happening is when I add Entity Reference field to the node then set number of values to unlimited and use ENTITY SELECTION
mode: Views: Filter by an entity reference view. Then every time when "Add another item" is pressed the view display return 0 results because Content ID from URL does not provide NID value. The issue is also documented on entity reference module here: #1823506: Ajax Calls Generated for Entity Reference Views cause issues
I made a bit of research because I was not sure if this is the problem of views module or if it is a problem of entity reference module or even the drupal ajax. Issue number 1 is that the system/ajax callback does not provide any data about node when is called from node/edit form. Only way to get the node info is from Header data for ajax callback from referer value. With the current implementation of views and ajax I think this issue should be addressed in this module.
Proposed resolution
I am attaching patch that alters get_argument() method of views_plugin_argument_default_node class where I added functionality to retrieve node NID value from referer value from header of ajax callback.
Comments
Comment #1
barancekk CreditAttribution: barancekk commentedPatch attached. Thank you
Comment #2
Jody LynnI think you should add your new condition as the last condition in the function as it's sort of a worst case scenario.
Also you have an extra line break between the condition and its statement.
Comment #3
barancekk CreditAttribution: barancekk commented@Jody
You are right I created new patch that adds the new functionality to the end of the method. But it looks like that to code ... if (arg(0) == 'node' && is_numeric(arg(1))) { .... is doing exactly the same thing as ... $node = menu_get_object('node', $i); ... before in method but maybe I am wrong.
Thank you
Comment #4
barancekk CreditAttribution: barancekk commentedThis one patch is with fixed line break formatting.
Thanks
Comment #5
ladybug_3777 CreditAttribution: ladybug_3777 commentedThe last patch does not contain the correct directory structure. It assumes that views is installed in /sites/all/modules/contrib/views/modules/node/views_plugin_argument_default_node.inc
I have updated the patch to use the proper structure so it can be saved and applied directly from the veiws main module folder.
See attached. (NOTE: I have not yet tested the patch code you wrote yet, I wanted to fix the path problem first)
Comment #6
ladybug_3777 CreditAttribution: ladybug_3777 commentedI've applied the patch and it does resolve the error. We can really use some regression testing to see if this negatively impacts any other functionality though. I'm going to keep it installed on my dev server and will report back if I come across new issues due to the patch.
Updating status to "Needs review"
Comment #11
ladybug_3777 CreditAttribution: ladybug_3777 commentedComment #13
ladybug_3777 CreditAttribution: ladybug_3777 commentedComment #14
ladybug_3777 CreditAttribution: ladybug_3777 commentedComment #15
ladybug_3777 CreditAttribution: ladybug_3777 commentedHow can I get my .patch fle re-tested? It's been sitting with "Test Request Sent" status for a couple of weeks now..... I've been using the patch on my development server for a bit and so far I haven't seen any apparent issues.
Comment #16
ladybug_3777 CreditAttribution: ladybug_3777 commentedIf anyone has input on this patch feel free to let me know. THANKS!
Comment #17
ladybug_3777 CreditAttribution: ladybug_3777 commentedAny input on this patch yet? I keep having to apply every time a new stable release comes out.
Comment #18
Jody Lynn@ladybug_3777 Use https://github.com/davereid/drush-patchfile and you won't have to manually re-apply patches when you do updates. Then you can just use patches and never worry about them.
Comment #19
ladybug_3777 CreditAttribution: ladybug_3777 commentedOh cool! Thanks Jody! I will check that out for sure.
Comment #20
JvE CreditAttribution: JvE at One Shoe commentedYou want to use menu_get_item() here in case of path aliases.
And check the current url is system/ajax so this doesn't trigger unexpectedly.
Comment #21
JvE CreditAttribution: JvE at One Shoe commentedI've created an alternate patch.
Works for nodes, users and taxonomy terms and does not trigger unexpectedly.
Comment #22
JvE CreditAttribution: JvE at One Shoe commentedUpdated patch to include non-jQuery ajax callbacks such as ajax file uploads.
Comment #23
JvE CreditAttribution: JvE at iO commentedFixing missing underscore and adding empty check
Comment #24
ladybug_3777 CreditAttribution: ladybug_3777 commented@Jody Lynn - I have to tell you, I finally had some time to play around with that Drush Patch File projects you suggested and it is SOOO cool! It is going to save me a bunch of time in the future! My maintenance nightmare is much more manageable now! THANK YOU!
Comment #25
NWOM CreditAttribution: NWOM commentedThis saved my day. Using the Relation Select module, it opens a view in a Subform via ajax in order to assign relations. However, without this patch, I wasn't able to use contextual views filters. Awesome work and thanks!
Comment #26
clemens.tolboomComment #27
clemens.tolboomChecking why the tests are postponed https://qa.drupal.org/pifr/test/1071723 I found an answer here http://drupal.stackexchange.com/questions/36167/what-does-test-status-po... http://drupal.stackexchange.com/a/36168/641
Comment #28
clemens.tolboomReviewing #23 it works nice within the restrictions of HTTP_REFERER
I think we should use similar code as modules/file/file.module:249
which contains the node/term etc in $form_state.
This should use $base_url. Attached patch fixes this.
Comment #29
jproctor#28 works for me, where my single-value entity reference selector was getting mangled by a multi-value field collection elsewhere in the node form.
Comment #30
jesse.voogt CreditAttribution: jesse.voogt at Plethora commentedMy issue: I have an entity reference field referring to a view with contextual filter of the node id, which defaults to the url. Whenever I uploaded an image to another field on the page, I was getting "An illegal choice has been detected" for the entity reference fields that were referencing the view with the contextual filter when I then tried to save the form. I narrowed the issue down to the fact that the node id was of course not present in the URL of the ajax request, so the view was returning nothing and then invalidating the selected values in my entity reference field.
My resolution:
#28 almost worked for me, except I was using language prefixes in the url because this is a multi-language site. I figured out that current_url(), used for regular pages to set the $path variable in views_plugin_argument_default.inc, was returning the path without the language prefix, but patch #28 was not stripping this. To get the urls acting in a similar fashion I modified the patch by calling language_url_split_prefix.
Comment #31
jesse.voogt CreditAttribution: jesse.voogt at Plethora commentedHere's a modified version of #28 that solves the language issue for me.
Comment #32
gaurav_manerkar CreditAttribution: gaurav_manerkar commentedMy patch works perfectly
Comment #33
clemens.tolboomI should have added a diff between #23 and #28 which is
#28 and #31
diff views_ajax_default_argument_paths-2358049-28.patch views-ajax_default_argument_paths_strip_lang-2358049-30-7.patch
#31 and #32 seems to have removed (introduced?) whitespace and removed documentation.
@gauravmanerkar please check and add an interdiff #31 and your new patch.
I agree with critical as it is disruptive. Note there are more modules breaking on ajax. I listed a few on http://build2be.com/content/ajax-nukes-reference-based-form-fields ; I advised my users not to use the upload button but just save the node on each new file :(
Comment #34
clemens.tolboomComment #35
NWOM CreditAttribution: NWOM commentedI found a bug in the patch when adding a Views Pane to a Panel Page. The view has a default argument as a contextual filter provided by the Views Contextual Filter Query module. I don't think it's related to the module, but instead a problem when adding a view with the default value contextual filter in general, but I figured I'd provide the module name just to be safe.
The following error is displayed when attempting to add the pane:
Removing the patch fixes the issue, and allows the views pane to be added without problems.
Comment #36
mobilemelody CreditAttribution: mobilemelody commentedI thought I was having a similar issue, but the patch didn't seem to resolve it. Is this patch is specific to node IDs or should work for things like term IDs as well? I have a node reference dropdown field that is populated using a reference view with a taxonomy term ID as the contextual filter. It's able to grab the correct term ID from the URL when the page initially loads, but after an AJAX request defaults to showing all nodes (which is what I've set it to do under the validation criteria). Any insight would be greatly appreciated
Comment #37
dtamajon CreditAttribution: dtamajon commentedI have tested #32, and got the error "call to undefined function language_url_split_prefix"
If I add the require_once in get_path(), then it works properly:
require_once DRUPAL_ROOT . '/includes/language.inc';
Not sure it this should be added to the patch or if any other additional check should be done.
Comment #38
caspervoogt CreditAttribution: caspervoogt at Plethora commentedJust to say that the patch from #32 worked for me and I have not run into "call to undefined function language_url_split_prefix" yet.
Comment #39
DamienMcKennaComment #40
hoopy21 CreditAttribution: hoopy21 commentedIt would be really nice if this patch could get put into the next release.
Edit: I also wanted to mention that I did have to add the require as comment #37 mentioned at line 105 in views/plugins/views_plugin_argument_default.inc.
Comment #41
gateway69 CreditAttribution: gateway69 commentedI'm also bumping into what I think is the issue, I wasn't sure if it was a entity reference issue or views.. but would this patch resolve this issue https://www.drupal.org/project/entityreference/issues/2429173