This is a follow up to updateFieldValue doesn't dispatch a change event.
I will preface by saying I'm not a JavaScript expert, and maybe there is a better way to achieve what I'm suggesting.
We have a client that needs to display messages if certain SHS dropdown options are selected. The problem is SHS doesn't provide an change event to access.
After doing some research, it seemed like the right approach was adding a change trigger, as patched in #2264795. However as far as I can tell, this won't work because it's a "change of a change"... SHS is changing the original select programmatically, and then .trigger is triggering a change from that change. JavaScript only recognizes change events when performed by the user.
So investigating further led to an approach using dispatchEvent and fireEvent, as detailed here: http://stackoverflow.com/a/2490876. This allows for an event listener to be set on the original field, and the selected value to be passed into JavaScript.
I'll post a patch in just a minute for review. As I said, there could be a much easier way to do this than I'm suggesting -- just haven't had any luck finding it. Thanks.
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2016-03-04 07-39-16.png | 35.93 KB | stborchert |
| #11 | shs-accessible_change_event-2499279-11.patch | 2.31 KB | sgdev |
| #2 | shs-accessible_change_event-2499279-2.patch | 1.9 KB | sgdev |
| #1 | shs-accessible_change_event-2499279-1.patch | 1.81 KB | sgdev |
Comments
Comment #1
sgdev commentedSee attached. Also added some text to the README to describe how to use.
The function needs to have a custom event name (in this case, "shs-change") since dispatch options do not allow for reusing existing events.
I'd appreciate any feedback, thanks.
Comment #2
sgdev commentedSorry, I realized the patch should also include the select's ID in cases where there are multiple SHS-enabled fields on the same page. See attached.
Comment #3
Mirkozzo commentedPatch don't have effect for me :/
For use i must something specific action in addition to patch shs.js?
Comment #4
sgdev commented@Mirkozzo, did you read the update made to README.txt? It is necessary to use an event listener in your site's Javascript file to use the update.
Comment #5
Mirkozzo commentedsorry, I did not understand how to use the information in the file readme.txt :/
I am creating a custom module and I have to use #states with shs
Comment #6
sgdev commentedWhat exactly are you trying to do with
#statesand shs?Comment #7
Mirkozzo commentedWhen term is select -> show field
If i use default taxonomy drupal widget -> work
If i use shs widget -> not work (field_my_field_text is always shown)
Comment #8
sgdev commentedI don't think you have a full understanding of how this module works. SHS does not store the value in the input field until after the page is saved. Use Firebug and you can see this in action -- each time a value is selected, there is no change in the default input (which, by the way, is hidden from display). The only changes are to the SHS widget that is javascript-driven.
All steps are done via json. This is the reason why the
hook_shs_json_callbacks_alterandhook_shs_json_get_children_alterfunctions exist. You need to intercept the value from JavaScript if you want to use it to control a change via#states.Comment #9
Mirkozzo commentedPatch and code in readme.txt is good for my problem?
I need to: user select term -> action
Comment #10
sgdev commented@Mirkozzo, yes that is the purpose of the patch. It provides a way to perform a secondary action based on a term event being selected.
Is there anyone else who has an opportunity to review this patch, or any feedback?
Comment #11
sgdev commentedGiven the recent changes to the 7.x-1.x-dev version of SHS, I've created a new patch for review. See attached.
Comment #12
stborchertSorry, but this is not correct.
Use Firebug (or Chrome dev tools, ...) to display the element and you will see, that the element value is updated as soon as you select an option within one of the widgets of SHS.
I've committed a change so other modules can listen to the change-event on the original element and get some additional information about what has happend.
Comment #14
sgdev commented@stBorchert, I think we might be referring to two different things. Yes, the element value is updated, but I'm referring to the default input value. This is not changed, and we verified this in both Firebug and Chrome dev tools.
The issue is SHS is changing the value programmatically, and
.triggeris triggering a change from that change. My understanding is that JavaScript only recognizeschangeevents when performed by the user. Therefore an accessible change event is necessary to hook into the process.Thanks for making an update... we'll review and post our findings.
Comment #15
stborchertCan you give a screenshot explaining, what you mean?
Comment #16
sgdev commentedYes, I was just testing the patch right now. I'll post a few images and additional information in a few minutes. Thanks.
Comment #17
sgdev commentedOk, before I add some screen shots, let me describe a test I've run. I want to capture the change event, so I have this simple code in my javascript file:
(and to clarify, this is wrapped with a Drupal.behaviors, etc.)
There are two selects on the page I'm testing -- an Ajax-enabled select that shows/hides fields based on the option chosen, and the SHS-enabled select. When I change the Ajax-enabled select, the console message is displayed. When I change the SHS-enabled select, there is no message.
As a second test, I added an event listener breakpoint in Chrome Sources for the change event. When I change the SHS-enabled select, I do see the event displayed, and the field value matches what I would expect.
Am I missing how I'm supposed to access the event, or should I send screen shots with more details?
Comment #18
stborchertStrange.
I have this javascript file https://gist.github.com/stborchert/ffbba33d6d3c8bc5e1ca and attach it to all forms:
If I now visit node/add/article and make a change to my shs-enabled select I get the following output in the console (2 times):

Seems to me its working ... ;)
Comment #19
sgdev commentedAh! Such an unfortunate mistake. Now it makes sense why we weren't seeing any values and had to write our own event listener!
Javascript was copied from another part of the site for use on the registration form, and the code was wrapped with a jQuery
.once:So it makes complete sense that a
.changeis rendered in the console for the Ajax-enabled select (the first step in the process), but not the SHS-enabled select. As soon as the.oncewas removed, it worked fine.Thank you for helping us realize what was happening. The patch works well.
Comment #20
stborchertGreat to hear.
Setting to "fixed" then since it has been committed already.
Comment #22
willabby commentedThis is working on latest version dev version.