Hi there,
I hope that someone can help me with the following problem I'm really becoming exasperated with already:
I want to react on selecting an entity reference via entity browser and update other (unrelated) fields on the same entity form. First, I've found that it is generally easy to detect the selection because the custom 'entity_browser_value_updated' ajax event is triggered.
However I have no clue, how I can modify other form element in the end, due to the way Ajax callbacks in Drupal Form API work:
- Whatever I'd change in my form code, wouldn't get updated because it's not within the Ajax wrapper element that Entity Browser is updating
- You can't add a second #ajax callback to an element, so I can't add a custom one to the entity browser's hidden input field in addition to the internal one
- I can't see any other way to hook in
What would you do in this situation? Any help would be very appreciated.. You can also find me on IRC and Slack (in both under the name agoradesign)
Comments
Comment #2
apmsooner commentedI havn't used that module but you might find the #ajax property 'trigger_as' useful in triggering your own callback. Here's an example: https://drupal.stackexchange.com/questions/34834/how-to-use-ajax-trigger-as. I'd be curious to see if you are able to make it work.
Comment #3
agoradesign commentedThank you very much for this advice, Andy! :)
Interesting idea... I didn't know - or maybe forgot and never used - the "trigger_as" features. From reading the docs I'd say there's at least a 50:50 chance that this can help me... not in the way I originally intended, but this brought me to an idea of an acceptable workaround - I'm thinking of adding a copy/transfer button (need to think about the best wording), which triggers a custom submit callback to fill in the values. With the help of "trigger_as" I maybe could trigger that automatically.
I won't have much time this weekend, so most probably I can try it on Monday. Of course I'll let you know, if it helped me :)
Thank your very much for your help! I'm still open to other ideas/solutions ;)
Comment #4
agoradesign commented@apmsooner: thanks again for your advice, unfortunately it worked only partially, so I had to dimiss it again -->
what I did: first inserted a button in the form, that was responsible for copying filling the form fields based on the selected entity. When this worked as expected for manual clicks, I tried to add "trigger_as" to the entity reference field in order to automate this. That part actually worked great, but unfortunately the entity reference field itself didn't update properly any more - for sure because it was now triggering under a foreign element, so not every part of the logic was executed as it should.
I finally ended up in implementing a workaround: after selecting the entity via entity browser, I trigger a click on the button to update the other fields... not beautiful, but at least I found any way to get it working...
Comment #5
agoradesign commented@apmsooner: thanks again for your advice, unfortunately it worked only partially, so I had to dimiss it again -->
what I did: first inserted a button in the form, that was responsible for copying filling the form fields based on the selected entity. When this worked as expected for manual clicks, I tried to add "trigger_as" to the entity reference field in order to automate this. That part actually worked great, but unfortunately the entity reference field itself didn't update properly any more - for sure because it was now triggering under a foreign element, so not every part of the logic was executed as it should.
I finally ended up in implementing a workaround: after selecting the entity via entity browser, I trigger a click on the button to update the other fields... not beautiful, but at least I found any way to get it working...
Comment #7
rzanetti commentedThe entity browser have a 'entities-selected' ajax fallback, which reacts on entities being selected via entity form.
You could add a drupal behavior and do something like this:
So, you could add inside this function the code from your button
see: entity_browser_entity_form.js file
Comment #8
vistree commentedI also need to update a paragraph select field after an image has been selected using entity-browser field widget (modal dialog).
For autocomplete fields I can do something like this:
This I can't do on entity_browser fields, for the reasons described above. Can you @agoradesign or you @rzanetti some more details/example on how to implement this in "your" ways?
I don't know - should I reopen this ticket?
Comment #9
agoradesign commentedsorry, I can't remember, for which project I did this workaround