I'm having an issue with a custom module I'm developing and was wondering if someone could help. I've attached a stripped down version of what I'm trying to do in my module to keep things as clear as possible.
The problem is with a ctools modal button on a page - created with the ctools_modal_text_button function:
$button = ctools_modal_text_button('Test', 'eludom/nojs/test', '', 'button thebutton');
The "test" at the end of the url on that link is a parameter to pass to the modal, which will then set the default value for a textfield in the modal to "test". This bit works and sets the value of the textfield in the modal popup when you click the button:
My issue is that I then take the value of the textfield (update it from "test" to say "foo") and when the submit button is clicked it updates the link's href attribute via an ajax_command_invoke in the callback:
<?php
$output[] = ajax_command_invoke('.thebutton', 'attr', array(
array('href' => 'eludom/nojs/' . $form_state['input']['changeit'])
));
?>
and this works as you can see from browser inspect:
The problem is that even though the href on the link has changed if you click the test button again the modal opens with the original value of "test" in the textfield, and the dpm() I have on there confirms that it is getting "test" as the callback parameter rather than "foo".
I've tried removing the processed classes on submit, wrote my own process function (see the Javascript file in the module), tried various binds/unbinds to try and overwrite what ctools is doing on click and even changed the ajax command to replace the button completely, but this breaks the button and it links to the nojs version as the behavior doesn't apply on replace.
Anybody know how I could get this working?
Comment | File | Size | Author |
---|---|---|---|
eludom.zip | 1.91 KB | Chris Graham |
Comments
Comment #2
Chris Graham CreditAttribution: Chris Graham commentedOK, so I don't know the implications of this as it seems a bit hacky from my perspective but I did manage to unbind the ctools click handler and bind my own. Just in case anyone else stumbles onto this, this is how I got it working.
I did the unbinds/bind in my behavior:
Then I wrote my bind function (outside the behavior):
The reason that I find it a bit hacky is that Drupal.CTools.Modal.modal_display() call in the success function of the ajax call. I noticed from doing a console.log on the response data that 2 objects were returned structured like so (stripped down that output to make it readable):
I basically ignored the settings command as I didn't know what to merge it with and used the second object as the response parameter on modal_display(). I get the feeling that there should be a way of invoking both commands automagically but I couldn't what to use in the CTools modal.js so... it works, but take it with a pinch of salt if you are using it.
Comment #3
kwfinken CreditAttribution: kwfinken at Michigan State University for Michigan State University commentedI am experiencing a similar thing. It seems like this should be changed from a support request to a bug report:
My Drupal form has a dynamically generated (select) field via ajax (works fine). When a user presses the browse index button, it opens a modal dialog for the person to search the index (works fine). I want to pass information from the field to the modal. If I change the value of the field and then click browse, it gives me the following error:
If I dont' change the field's value, the modal works fine. If I change it and then change it back to the default, I get the error. If I change it and then add or remove a copy of the field, then the modal works fine.
Sorry to add so much code, but I am at a loss.
Comment #4
kwfinken CreditAttribution: kwfinken at Michigan State University for Michigan State University commentedComment #5
bleen CreditAttribution: bleen at NBCUniversal commentedFWIW it seems that this same issue made its way into the core modal behavior in D8 ...
Comment #6
open.source.developer CreditAttribution: open.source.developer at Capgemini commentedI have found solution for updating the Ctools link initial URL with javascript. Below is the code and explanation
I am calling above JS function using ajax_command_invoke
As seen in the above JS function in first 5 lines I read the existing link href and append it with new value as required and update the href attribute on the anchor tag.
Now to modify the actual ajax url which ctools modal window calls while initializing is modified using beforeSend event
The beforeSend event callback is registered on Drupal.ajax[link] where link is the inital modal url, if any doubts you can console.log this to check that you are on right track.
Finally inside the beforeSend callback update options.url with the new link
This worked for me
Comment #7
guschilds CreditAttribution: guschilds commentedAs mentioned in #5, this behavior is in Drupal 8 core.
I experienced it when altering the URL for the "Browse media" button in the new Media Library widget (I was using JS to change the Media Library's exposed filter options in the URL based on the value of another field).
The reason it happens is because core's
Drupal.ajax.bindAjaxLinks()
adds an event listener to such a button that doesn't react to any future changes to the button's URL. This can be overcome by removing what that function adds (jquery.once('ajax')
and the click handler) and then calling it again. An example:Hope that helps the next person!
Comment #8
imclean CreditAttribution: imclean commentedThanks @guschilds, that works a treat. In my case the DOM was being updated with VueJS but the URLs weren't changing. Drupal just lost track of which links referred to which page so the links looked correct but the wrong page opened in the modal. To just refresh the binding you can use this:
Comment #9
imclean CreditAttribution: imclean commentedTo add some information to #8, if you're populating a page with Vuejs and the content includes ajax modal links, the standard ajax link binding won't work for initial page load either. In that case, add the following to your attach behaviour: