I'm not shure if it's bug report or not... As far as I understand Drupal behaviors the context argument passed to a behavior function is a DOM element which was inserted or updated. So I can do $(".some_selector", context) to select only childern of the inserted element.
But if a behavior function is called when hierachical select element is updated (i.e. user selects another option) then $(".some_selector", context) selects from the whole document (not only children of updated hs). According to firebug, context is some function in that case.

Comments

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)

I'm not sure what you mean (maybe it's because I'm tired, in which case I apologize). Can you please rephrase that?

TonyK’s picture

The problem:
There is a call to Drupal.attachBehaviors in Drupal.HierarchicalSelect.update function (hierarchical_select.js):

      // Attach behaviors. This is just after the HTML has been updated, so
      // it's as soon as we can.
      Drupal.attachBehaviors(Drupal.HierarchicalSelect.context);

Clearly, the first argument passed to Drupal.attachBehaviors function must be a DOM element that was inserted by JS. But Drupal.HierarchicalSelect.context is a function, not DOM element.
The question is, how this code is going to work? Maybe it's better to replace it with

      Drupal.attachBehaviors(Drupal.HierarchicalSelect.context());

?
Sorry for my English.

TonyK’s picture

Status: Postponed (maintainer needs more info) » Active
wim leers’s picture

Title: What Drupal behaviors are attached to when hierarchical select element is updated? » Drupal.attachBehaviors() used with an incorrect context?
Category: support » bug
Status: Active » Needs work

Hm … you might be right here, but it's always worked like this for me. Can you do some testing to confirm your hypothesis?

wim leers’s picture

Any news?

Isostar’s picture

subscribe

glennpratt’s picture

I'm having this issue as well, Hierachecal Select breaks other Drupal behaviors on the page if they implement context.

See: http://api.jquery.com/jQuery/

context
A DOM Element, Document, or jQuery to use as context

A simple search and replace stops breaking other code, but then HS stops working.

glennpratt’s picture

Actually, I take that back, HS works ok for one request, then goes into nojs mode with this change:

Drupal.attachBehaviors(Drupal.HierarchicalSelect.context());

glennpratt’s picture

For more evidence, my code should only update it's elements if they are in context, but when HS fires attachBehaviors, it finds anything in the document. If I actually call context as a function, it doesn't find my elements as I expect.

Test code in my behavior:

Drupal.behaviors.my_behavior = function(context) {
  console.log(jQuery('#my-non-hs-element', context));
  if(typeof(context) == 'function') console.log(jQuery('#my-non-hs-element', context()));
  //do stuff...
}

Result:

[<select name=​"foo" class=​"form-select" id=​"my-non-hs-element">​…​</select>​] // Incorrect, my elements aren't inside HS.
[]                                                                         // Correct, my elements shouldn't be found.
wim leers’s picture

Status: Needs work » Postponed (maintainer needs more info)

You're calling context(), not Drupal.HierarchicalSelect.context().

glennpratt’s picture

Status: Postponed (maintainer needs more info) » Needs work

Sorry if I was unclear.

The previous test code was in MY Drupal.behaviors function, I don't plan on calling anything to do with HS in my code! I've updated it to be more clear.

HS is calling it on line 556:

Drupal.attachBehaviors(Drupal.HierarchicalSelect.context);

The problem is, the argument is a var defined as a function and passed to my code...

Drupal.HierarchicalSelect.context = function() {
  return $("form .hierarchical-select-wrapper");
};

jQuery doesn't expect context to be a function and appears to ignore it or cause random problems (on some browsers my behavior would just stop working after an action happened in HS.

Here is my fix for my code, I tried to fix HS, but it wasn't straightforward to fix.

Drupal.behaviors.my_behavior = function(context) {
  // @todo - context is incorrectly set as a function by Hierachecal Select.
  // @see http://drupal.org/node/828418.
  if(typeof(context) == 'function') {
    context = context();
  }
  jQuery('#my-element', context).blah();
  // ...snip...
}
wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new9.29 KB

Very strange, that used to work just fine in the past. But, this should be easily fixable. Please review the attached patch.

glennpratt’s picture

Looks good in some quick testing. I tried and failed to do what you did in your patch, probably missed a line...

This will be included with code going into testing soon, so I should know pretty quick if it breaks.

Thanks so much.

wim leers’s picture

Okay, great! Don't forget to let me know if this works! :)

glennpratt’s picture

Well, I'm afraid cacheing may have gotten the better of me, now I'm seeing the 'You don't have Javascript enabled' message.

I'll try to test it on a clean environment in the next couple days.

wim leers’s picture

Okay, thanks! Keep us posted :)

Georgique’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Status: Needs review » Needs work

Please take a look on following issue, seems that our bug needs some more work: http://drupal.org/node/1507954
Guys changed code in states.js from

Drupal.behaviors.states = {
   attach: function (context, settings) {
     for (var selector in settings.states) {
       for (var state in settings.states[selector]) {
         new states.Dependent({
           element: $(selector),

to

Drupal.behaviors.states = {
   attach: function (context, settings) {
   var $context = $(context);
     for (var selector in settings.states) {
       for (var state in settings.states[selector]) {
         new states.Dependent({
           element: $context.find(selector),

So now it won't work correct with HS since HS context returns function instead of DOM element. I'm not sure what is the nice solution of this problem, but I have a feeling that HS context should not be function anymore.

Georgique’s picture

Status: Needs work » Needs review
StatusFileSize
new9.38 KB

I think I fixed it.
Let me explain.
When we had been passing function as context, system didn't understand it so it used document DOM object instead. Also every selector in hierarchical_select.js file uses hsid, so context doesn't needed there at all. And that's why it worked.
Only the place where we need context is the following line:
Drupal.attachBehaviors(Drupal.HierarchicalSelect.context);
As I explained it worked before, but behaviors were attached for the whole document object. This is not what we want, so @glenpratt had tried to change it to
Drupal.attachBehaviors(Drupal.HierarchicalSelect.context());
but this was also wrong because:
1) Drupal.HierarchicalSelect.context() returns array, not single element;
2) It returns .hierarchical-select-wrapper element, but look at the following code:

Drupal.behaviors.HierarchicalSelect = {
  attach: function (context) {
    $('.hierarchical-select-wrapper:not(.hierarchical-select-wrapper-processed)', context)
    .addClass('hierarchical-select-wrapper-processed').each(function() {
      var hsid = $(this).attr('id').replace(/^hierarchical-select-(\d+)-wrapper$/, "$1");
      Drupal.HierarchicalSelect.initialize(hsid);
    });
  }
};

$('.hierarchical-select-wrapper:not(.hierarchical-select-wrapper-processed)', context) won't work since context is .hierarchical-select-wrapper.

So I changed it to the following code:

$("form .hierarchical-select-wrapper").each(function() {
  Drupal.attachBehaviors($(this).parent());
});

Seems now it works fine, it shouldn't break other fields, it works ok with several hs fields on one page.
Review needed and I guess it would be nice to commit this and introduce new version of hs asap.

kaizerking’s picture

I have applied this patch to get two HS fields on the same page.
it did not work.
When i create initially it is available and when i try to edit and save it wont save

Georgique’s picture

@kaizerking I can't reproduce, on my machine it works fine. Can you debug?

sreynen’s picture

#18 is on the right track, but has two problems. First, the patch includes a lot of changes that don't appear to have anything to do with this issue. Second, the code has changed somewhat since the patch was posted.

The code now passes context()[0], a single element, which is close, but still wrong. As #18 explains, this is passing the HS element, but jQuery doesn't find elements within themselves.

Attached patch applies the solution outlined in #18, passing in the parent instead of the element itself.

thomas73’s picture

Thanks, this patch seems to resolve my problems.

ryanoreilly’s picture

Also confirming #21 fixes Alpha-6 bug.

mesh’s picture

#21 makes alpha6 at least usable. However, node forms still get auto-submitted on creating new terms.

sreynen’s picture

I didn't see any mention of creating new terms in this issue. Is there a separate issue for that? If not, let's make one and fix that separately. With 187 open bugs in this project, we can't solve them all in one patch.

kars-t’s picture

Status: Needs review » Reviewed & tested by the community

I apllied this to a simple D7 installation and the JS problems are fixed with this patch.

ssoulless’s picture

Please let us know if you open a new issue for fix the bug while creating new terms

wim leers’s picture

Status: Reviewed & tested by the community » Fixed
cravecode’s picture

Status: Fixed » Needs review
StatusFileSize
new1.06 KB

The patch from #21 is still not ideal and only works if one Hierarchical Select is on the page. It's using the Context (Drupal.HierarchicalSelect.context) and selecting the first element to re-attach the Drupal behaviors thus skipping over any additional Hierarchical Selects in the form. I believe we should be grabbing the wrapper of the initiating element.

Patch Summary:
Replace:
Drupal.attachBehaviors(Drupal.HierarchicalSelect.context().parent()[0]);
With:
Drupal.attachBehaviors($('#hierarchical-select-' + hsid + '-wrapper').parents('div.form-type-hierarchical-select')[0]);

My submitted patch is for the 7.x-3.0-alpha7 tag.

This patch has been tested with:

  • 4 Hierarchical Select elements on a form.
  • On a inline_entity_form form (Drupal Commerce).
  • Using jQuery 1.7.
  • Taxonomy with 4 tiers
puppyman’s picture

https://drupal.org/node/828418#comment-7939351 Works for me! Had to manually patch, however.

ssoulless’s picture

Status: Needs review » Reviewed & tested by the community

Works pretty well, i did not wanted to post another comment here due to Im using simple hierarchical select, but the error that you say is happening, and the current "comitted" patch is not enough, i tried your patch "had to manually patch" and it works pretty great!

cravecode’s picture

The reason a manual patch is required is because it was created for the latest tag (7.x-3.0-alpha7) in the Git repository. If you aren't cloning from Git and instead downloading via Drush or from drupal.org/project/hierarchical_select you're not getting the latest release, you're getting the 7.x-3.0-alpha6. Alpha7 looks like it was created specifically to include the #21 patch from this issue.

Hope that helps!

wim leers’s picture

Status: Reviewed & tested by the community » Fixed

I am very puzzled why alpha 7 does not show up. I *did* create it, immediately after pushing the commit in #28. WTF!? I even viewed the project release node after creating it IIRC. This is extremely bizarre. Looks like something went wrong on drupal.org… :(

But apparently it's a good thing that the alpha 7 release didn't succeed on d.o, because just like in alpha 6, a fully validated patch (i.e. RTBC'd in #26) turns out to be problematic.

However, the patch that's been RTBC'd this time is also fundamentally flawed IMHO; it no longer calls Drupal.HierarchicalSelect.context(), which is a fundamental change that should not have to happen. However, sincet there are 3 independent confirmations that it works, I'll commit it anyway. The D7 codebase is a mess already anyway. Better to have a working mess.

http://drupalcode.org/project/hierarchical_select.git/commit/b3e34d9c2a0...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

swarad07’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

I applied the patch in #21 on alpha 6, it works only for the first HS field.

I have a CC with two HS fields, on the node/add or node/edit page which ever field is the first field the JS works for it. For the second field the JS breaks and shows the update button.

Any idea on how to fix this ?

swarad07’s picture

Status: Active » Closed (fixed)

Sorry guys my bad! I was looking at the wrong code.

sylus’s picture

Attaching a patch that will work with Alpha 6 for drush make since Alpha 7 wont work.

ssoulless’s picture

Status: Closed (fixed) » Patch (to be ported)

Awesome finally a patch that really works!! thanks Sylus

itangalo’s picture

+1 for people working on this issue. Makes me happy.

darktek’s picture

#18 just worked really great thx so much !!

kevin.dutra’s picture

Version: 7.x-3.x-dev » 6.x-3.x-dev
Status: Patch (to be ported) » Needs review
StatusFileSize
new679 bytes

Here's a patch ported back to D6.

heddn’s picture

Added a follow to this issue #2301699: Timing to release new hs version for D7.

ssoulless’s picture

Status: Needs review » Reviewed & tested by the community

The patch works for Drupal 6 correctly. Nice...

stefan.r’s picture

Status: Reviewed & tested by the community » Closed (fixed)

D6 patch committed, thanks!

stefan.r’s picture

Status: Closed (fixed) » Fixed
calculus’s picture

I am not sure that i have to open this issue again, but the "update button" problem remains after updating to 7.x-3.0-alpha8

Should i comment there? #2000762: Problem after the last update 7.x-3.0-alpha6, it shows me a button 'Update' while selecting

stefan.r’s picture

Yes, that's fine, these fixes are not in alpha8 yet.

Alpha8 is just alpha6 with a security update. We'll do an alpha9 soon.

calculus’s picture

Ok thank you. Any quick and dirty patch? I tried #41 and worked locally but not on live server...

fredfab’s picture

Thanks you all for your works on this issue ! We clearly need the same fix for Alpha8 since it's a security release.

nicolas bouteille’s picture

Shouldn't this issue version be 7.x for now?

stefan.r’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Status: Fixed » Needs work

Reopening this issue in case anyone has a better solution than the latest patch, which still has the flaws pointed out in #33

pianomansam’s picture

Version: 7.x-3.x-dev » 7.x-3.0-alpha9
Status: Needs work » Closed (fixed)

Apparently this was fixed in 7.x-3.0-alpha9 but not updated to reflect that.