Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stBorchert’s picture

Hm, 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?

markhalliwell’s picture

Title: js module integration » JS 7.x-2.x Integration
Status: Active » Needs review
FileSize
5.22 KB

Hm, 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 and js_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).

markhalliwell’s picture

I'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:

$element.jsCallback({
  callback: 'shs_json_term_get_children',
  arguments: {
    vid: settings.vid,
    parent: parent_value,
    settings: settings.settings,
    field: settings.fieldName
  }
});

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 in js.js just to see what it could do for you! I am most certainly open to suggestions :D

stBorchert’s picture

Thanks for the patch (looks good!) and the documentation. I will have a look into it later.

Owen Barton’s picture

This 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.

m42’s picture

Priority: Normal » Major

JS 1.0 is no longer supported, this issue needs to be solved.

m42’s picture

Here is a "mashup"-patch (with all the good ideas of the previous ones) that applies on the latest dev. version.

markhalliwell’s picture

Status: Needs review » Needs work

I 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).

jadsay’s picture

This is a mashup-patch of the previous ones that applies on the latest 7.x-1.6 version of shs

markhalliwell’s picture

@jadsay, js_7.x-1.x is no longer supported. It's a security risk. Do no use it.

y_h’s picture

This doesn't seem to work with a multilingual setup if you're using a language prefix.

$.ajax({
   url: Drupal.settings.basePath + '?q=' + Drupal.settings.pathPrefix + 'js/shs/json',

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.

hanoii’s picture

I 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.

daniel_j’s picture

The patch in #9 no longer cleanly applies to 7.x-1.x-dev. I have manually updated it to do so here.

guillaumev’s picture

Patch 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...

guillaumev’s picture

Status: Needs work » Needs review
markhalliwell’s picture

Status: Needs review » Needs work

As 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.

moveronweb’s picture

Patch #14 works for me, only "warning: 1 line adds whitespace errors." shows up.

gouky10’s picture

Patch #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

stBorchert’s picture

Status: Needs work » Needs review
FileSize
53.9 KB

Updated the patch and removed all references to JS 7.x-1.x.

vdsh’s picture

I 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

cosolom’s picture

FileSize
21 KB

Rerolled for latest dev

vdsh’s picture

The 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?

vdsh’s picture

Status: Needs review » Needs work
vdsh’s picture

I 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)

markhalliwell’s picture

Ok. 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.

markhalliwell’s picture

FileSize
38.09 KB
524 bytes

Whoops, forgot to change the element path for the add token after some last minute rearranging of some code (it's late for me).

plach’s picture

Path looks good to me, although I did not test it, RTBC +1 if anyone can confirm it is working properly.

+++ b/js/shs.js
@@ -118,85 +150,71 @@
+        // Add empty option (if field is not required or this is not the
+        // first level.

80 chars

gonssal’s picture

-

gonssal’s picture

-

markhalliwell’s picture

This 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.

gonssal’s picture

Well, 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.