Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Status: Active » Needs review
FileSize
3.05 KB
johnennew’s picture

Can date fields be included in this as well? They also get rendered as 3 select drop downs on webform.

johnennew’s picture

Please find updated patch with chosen toggle for date fields applied as well.

joelpittet’s picture

Status: Needs review » Needs work

This works fairly good but if you leave no preference, it seems to pickup it's value from previous select fields. It should be picking up from the global setting right? That is how the field UI stuff works with no preference.

May have something to do with that note in the comments about Webforms turning off prerender?

ericshell’s picture

May have something to do with that note in the comments about Webforms turning off prerender?

From everything else I have found, this seems to be the issue. @Dave Reid mentioned he created an issue for the Webform module but I have not been able to find it to aid in resolving this issue.

I have also created this post which has not gotten much activity:. http://drupal.stackexchange.com/questions/129489/webform-capability-issu...

After applying the patch above, the select fields in the webform were still not wrapped by the chosen module. Even after configuring the module to wrap all select options and clearing the cache again, the chosen module was still not being applied.

Dave Reid’s picture

+++ b/chosen.module
@@ -282,3 +291,86 @@ function chosen_get_chosen_path() {
+      return;

Yeah, we should just probably remove this. And confirm which webform element types would need to have #pre_render added all the time (we know select does, not sure about the rest).

scottrigby’s picture

FileSize
6.71 KB
3.44 KB

Hey guys,

So this new patch doesn't address #4 or #6 yet, but does support webform conditionals (otherwise Chosen keeps it's pseudo element disabled).

The JS code is mostly based on webform.js (only the conditionals section), which is namespaced for Chosen, and needed to trigger a Chosen library redraw/update method (documented here https://harvesthq.github.io/chosen/#change-update-events).

Also attaching an interdiff with #3.

scottrigby’s picture

Status: Needs work » Needs review
FileSize
7.17 KB
572 bytes

OK this patch is ready for review:

  • The concern in #4 is addressed (the field now picks up global setting if 'No preference' is selected, but properly uses or doesn't use if the other two options are selected)
  • Dave's comment in #6 is addressed (if I got what you meant – my last patch in #7 already removed `'#disabled' => !chosen_get_chosen_path(),`)

Including interdiff against #7.

joelpittet’s picture

@scottrigby gave this a try with date popup select and it didn't seem to update chosen.

joelpittet’s picture

To resolve date select fields not updating I just dropped this in a behaviour.

$('.webform-datepicker', context).find('select').once('chosen-webform-datepicker', function() {
  $(this).on('change', function() {
    $(this).trigger('chosen:updated');
  });
});
Martijn de Wit’s picture

Using patch (#8) because chosen was not applied to my webforms. It now works great!
I only had to manually apply the code to my files. The patch file didn't work in source-tree and gave some error's about matching files...

Comments in Issue https://www.drupal.org/node/2122061 weren't helping a lot, but this patch did the trick :)

ahillio’s picture

Patch in #8 applied successfully to 2.0-beta4, got chosen on my webform select options now. Thanks @scottrigby!

drclaw’s picture

+++ b/chosen.js
@@ -59,6 +59,92 @@
+              $($target.selector + ' select').trigger('chosen:updated');

I believe this is causing an unrecognized expression error because the $form selector was generated using the jquery .closest() function. That produces an invalid jquery selector like this: .closest(.webform-component).closest(form) .webform-component--my-component select. Additionally, I believe the $element.selector property has been removed in jQuery > 1.8.x. Might I suggest passing the $target element in as the selector context instead? This has the added bonus of likely being more performant since jQuery won't have to search the whole DOM for the select element.

Re-roll of Patch & interdiff attached!

Status: Needs review » Needs work

The last submitted patch, 13: 2214247-chosen-webform-toggle-13.patch, failed testing.

nubeli’s picture

I tried the patch in #13. It applied well enough to chosen 7.x-2.0-beta5. When I go to the components page of a webform with an existing select element I get an error:

Recoverable fatal error: Argument 1 passed to chosen_webform_component_defaults_alter() must be of the type array, null given, called in /path/includes/module.inc on line 1157 and defined in chosen_webform_component_defaults_alter() (line 312 of /path/sites/all/modules/contrib/chosen/chosen.module)

If I delete the select element and re-add it then the patch works okay. So seems that it just needs to account for existing select elements in webform not yet having any preference?

deranga’s picture

Just wondering, for the date element piece, can we not just add ".chosen-disable select" as follows in chosen.js:

$(selector, context)
        // Disabled on:
        // - Field UI
        // - WYSIWYG elements
        // - Tabledrag weights
        // - Elements that have opted-out of Chosen
        // - Elements already processed by Chosen
        .not('#field-ui-field-overview-form select, #field-ui-display-overview-form select, .wysiwyg, .draggable select[name$="[weight]"], .draggable select[name$="[position]"], .chosen-disable, .chosen-processed, .chosen-disable select')
        .filter(function() {

And then under the webform components we don't want chosen applied to add the chosen-disable class to the wrapper?

delacosta456’s picture

Hi
in this logic , if not already exist and if it make sense is it possible to have chosen contextualy activated :

- by path
-by node type
-by entity

thanks

joelpittet’s picture

Heads up my suggestion in #10 still works.

Full code for the copy pasters out there.

(function($, Drupal) {
  Drupal.behaviors.ChosenWebformDatePicker = {
    attach: function (context) {
      $('.webform-datepicker', context).find('select').once('chosen-webform-datepicker', function() {
        $(this).on('change', function() {
          $(this).trigger('chosen:updated');
        });
      });
    }
  };
} (jQuery, Drupal));
tzt20’s picture

I ran into this issue just as you posted your solution @joelpittet. #10/#18 works like a charm. Thanks.

Elvin - Albania Drupal Developer’s picture

Thank you @joelpittet #18 solved it like a charm!!!