Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The JS module has a new 7.x-2.x branch (API) and currently has a 7.x-2.0-beta1 release. Soon a new stable 7.x-2.0 will be released. Just trying to give a heads-up and also asking for feedback/testing if possible.
Comment | File | Size | Author |
---|---|---|---|
#26 | interdiff-2417417-25-26.txt | 524 bytes | markhalliwell |
#26 | 2417417-26.patch | 38.09 KB | markhalliwell |
Comments
Comment #1
stBorchertHm, I've looked into the new API and am not sure on how to create an integration that works with either JS module disabled, JS v1.x or JS v2.x ...
As far as I can see, the new API requires you to use the function
jsCallback()
or am I wrong?Comment #2
markhalliwellHm, yes.. I can see how this may be confusing at first, my apologies. Unfortunately there's not very good documentation on the 7.x-2.x branch yet (still a work in progress). But I'm glad you took a stab at it cause it's shown me some areas of documentation that need to be worked on (especially around the 7.x-1.x upgrade process).
While working on this, I did realize that the easiest way to detect whether or not the request is via core or JSv1 vs. JSv2 is by detecting if
global $_js
has been set (which is only available in JSv2).After that it's more just a matter of plugging in the necessary
#js_callback
on the select element (so it generates the necessary XSS token) and then modifying the data sent with your AJAX request for JSv2.I didn't change the menu path (for JSv1 compatibility), however in JSv2 the AJAX request doesn't need to send the module and callback in the path anymore. This is done through
js_module
andjs_callback
parameters in the POST data.Ultimately, yes... I would recommend using
$.jsCallback()
as it will cut out a lot of redundant clutter on the JS sides of things, but in reality it's not needed as long as you know what JSv2 is expecting on the backend. I simply created these jQuery methods as helpers for anyone that wanted to use them (best probably from scratch though).Comment #3
markhalliwellI'd like to further explain the purpose behind
#js_callback
and$.jsCallback()
.#js_callback
will add data attributes to whatever element you attach it to via JSv2's new#pre_render
callback:<select class="..." data-js-module="shs" data-js-callback="json" data-js-token="..." ....></select>
When used with
$.jsCallback()
, you don't have to implement your own verbose$.ajax
and then specify all the necessary parameters (like the above patch is doing). Instead, you can focus on just the data you're sending back. For example, say$element
is the<select>
element above:This would automatically pull in the data attributes from select and merge them in with the data supplied here. One of the reasons for this is that it decouples the backend requirements and allows the front-end to know have to worry about what the module, callback or token is (as these, in theory, could change).
However, given that you're also dynamically creating
<select>
elements, I suspect it would take a greater amount of effort to completely redo the code here. Regardless, I definitely recommend you take a look at the code injs.js
just to see what it could do for you! I am most certainly open to suggestions :DComment #4
stBorchertThanks for the patch (looks good!) and the documentation. I will have a look into it later.
Comment #5
Owen Barton CreditAttribution: Owen Barton at CivicActions commentedThis is working for me - thanks for working on these!
I did have one issue which is that the option values were showing up HTML escaped (i.e. & present). The HTML5 docs for "new Option" (which is what is being used by shs) are at http://www.w3.org/TR/2012/WD-html5-20121025/the-option-element.html#dom-..., but they are not exactly readable, so I put together a simple jsFiddle to confirm this is the case https://jsfiddle.net/b8qe0628/. The attached patch adds xss => FALSE to the info array to disable automatic HTML escaping by JS module.
Comment #6
m42 CreditAttribution: m42 commentedJS 1.0 is no longer supported, this issue needs to be solved.
Comment #7
m42 CreditAttribution: m42 commentedHere is a "mashup"-patch (with all the good ideas of the previous ones) that applies on the latest dev. version.
Comment #8
markhalliwellI think all the legacy cruft should be removed now that js_7.x-1.x is officially marked as unsupported and a security risk. It doesn't make sense to keep support for it here too (which just let's people stay on old, insecure code).
Comment #9
jadsay CreditAttribution: jadsay commentedThis is a mashup-patch of the previous ones that applies on the latest 7.x-1.6 version of shs
Comment #10
markhalliwell@jadsay, js_7.x-1.x is no longer supported. It's a security risk. Do no use it.
Comment #11
y_h CreditAttribution: y_h commentedThis doesn't seem to work with a multilingual setup if you're using a language prefix.
if i replace the Drupal.settings.pathPrefix with a / for testing purposes, getting rid of the language prefix (nl/ in my case), the call doesn't give a "404 not found" anymore but comes through.
Despite the 200 OK status, i don't seem to get a response but that's another can of worms.
Comment #12
hanoiiI must be doing something wrong.
I am using JS 2.4, latest shs dev and the patch on #7 and it doesn't seem to be working.
I get:
The token provided was either missing or invalid. Please refresh this page or try logging out and back in again.
Which I did, but this doesn't work on either auth or anon.
Am I missing anything?
I did add the rewrite rules and I confirm that js.php on the root folder is kicking in.
Comment #13
daniel_j CreditAttribution: daniel_j commentedThe patch in #9 no longer cleanly applies to 7.x-1.x-dev. I have manually updated it to do so here.
Comment #14
guillaumev CreditAttribution: guillaumev commentedPatch in #13 almost works for me (it's just missing the default arguments in the shs_js_callback_json function). I added these arguments in this new patch...
Comment #15
guillaumev CreditAttribution: guillaumev commentedComment #16
markhalliwellAs I stated above, js_7.x-1.x is no longer supported. It's a security risk. Support for it should be removed by this patch.
Comment #17
moveronweb CreditAttribution: moveronweb as a volunteer commentedPatch #14 works for me, only "warning: 1 line adds whitespace errors." shows up.
Comment #18
gouky10 CreditAttribution: gouky10 as a volunteer commentedPatch #14 need add
'#js_callback' => array('shs' => 'json'),
in:
function value_form(&$form, &$form_state) {
line 153 of
shs_handler_filter_term_node_tid.inc
in order to work with exposed filter. Al least, It work for me
Comment #19
stBorchertUpdated the patch and removed all references to JS 7.x-1.x.
Comment #20
vdsh CreditAttribution: vdsh commentedI have exactly the same issue as #11 (I have applied #19 which has no impact).
After investigation, it was a lot of issues combined.
First, the access callback 'user_access' was giving issues. I tried to add the user dependency (as the function was not found) - but it's still not solving the issue as the function is not working (probably some other dependencies missing, but I haven't investigated more and removed the user_access check - this should be improved) (todo)
Then the language was not properly retrieved from the request (fixed)
Lastly, as the translation modules were not loaded, the translation was never done. Here again, I manually added the missing dependencies (i18n in my case) - but there is probably also a way to improve this (todo)
Anyway, here is a patch working for my specific usecase
Comment #21
cosolom CreditAttribution: cosolom commentedRerolled for latest dev
Comment #22
vdsh CreditAttribution: vdsh commentedThe patch #21 doesn't apply cleanly to latest dev. There are also changes to the function shs_js_callback_json in #19 which I can't find in that patch. And #19 is 54KB whereas this patch is only 21KB. Is there anything to include? Do you mind rerolling the patch for the latest dev or 7.x-1.8?
Comment #23
vdsh CreditAttribution: vdsh commentedComment #24
vdsh CreditAttribution: vdsh commentedI updated patch #19 against 7.x-1.8. It now works as expected with js
If anyone is interested, I also rerolled the multilingual patch (which still needs work to have a proper one)
Comment #25
markhalliwellOk. I'm not sure what is/was going on above. So many patches that differ in size and seem to implement other issues + short array changes.
I decided to start from scratch based on the current dev version and completely refactor how the callbacks are implemented.
This supports both with and without JS, naturally, but I've made the assumption that in 7.x-2.0 (if that will ever come to be), the JS module should be a dependency (for simplicity sake).
I've marked quite a few functions as deprecated, mostly due to lack of necessity/replacement with JS module equivalents.
Due to the JS changes, I recommend that this be merged ASAP to avoid the need for another re-roll and the mess that was created above.
I have tested this thoroughly, but if others here are willing to as well and RTBC it; that certainly will help.
---
I've also gone ahead and created a new release for the JS module: js_7.x-2.5.
While this new JS module version shouldn't be necessary for this patch to work, I recommend that you update regardless.
Comment #26
markhalliwellWhoops, forgot to change the element path for the add token after some last minute rearranging of some code (it's late for me).
Comment #27
plachPath looks good to me, although I did not test it, RTBC +1 if anyone can confirm it is working properly.
80 chars
Comment #28
gonssal-
Comment #29
gonssal-
Comment #30
markhalliwellThis issue is only indirectly involved with translations (because it has to move some code around), it's not primarily about them.
This issue is specifically about the integration of JS module 7.x-2.x.
Comments like #28 & #29 are similar to what has previously derailed this issue (see above). Saying that an issue/patch "isn't working" just because it didn't fix your specific issue (which is an entirely different issue BTW) only does an issue and the people who have worked on it a disservice.
Issues specifically regarding multilingual/translations should be filed as a separate issue. Please stop hi-jacking this one.
Comment #31
gonssalWell, there are multiple comments and patches related to language support so I thought the requested testing would include making sure that worked. Hence why I said that (specifically) translations are not working and explained the reason.
I just removed my comments so you your issue is not disturbed anymore.