Chosen module breaks JS enhancements

Created on 24 June 2024, 6 days ago
Updated 25 June 2024, 5 days ago

Problem/Motivation

This was already mentioned in πŸ› Unable to add, remove or copy timeslot Needs review but could benefit from having a dedicated issue.

When the Chosen β†’ module is installed, it replaces the Hours dropdowns with fancy widgets which breaks the JS enhancements to copy / delete time slots.

This can be worked around by altering the widget, but maybe it is a good idea to fix it in the module.

Workaround:


/**
 * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
 *
 * Registers an after build callback to exclude office hours dropdowns from
 * chosen. Chosen alters the HTML structure of the dropdowns, which breaks the
 * JS enhancements of the office hours widget.
 */
function mymodule_field_widget_single_element_office_hours_default_form_alter(&$element, FormStateInterface $form_state, $context) {
  $element['office_hours'][0]['value']['#after_build'][] = 'mymodule_field_widget_single_element_office_hours_exclude_from_chosen';
}

/**
 * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
 */
function mymodule_field_widget_single_element_office_hours_exceptions_form_alter(&$element, FormStateInterface $form_state, $context) {
  mymodule_field_widget_single_element_office_hours_default_form_alter($element, $form_state, $context);
}

/**
 * Form after build callback to exclude office hours dropdowns from chosen.
 */
function mymodule_field_widget_single_element_office_hours_exclude_from_chosen($element) {
  foreach (Element::children($element) as $child_id) {
    foreach (['starthours', 'endhours'] as $hours_id) {
      $element[$child_id][$hours_id]['hour']['#chosen'] = FALSE;
    }
  }
  return $element;
}

Steps to reproduce

  1. Install the Chosen module
  2. Edit a form that uses the default or complex widget

Result: The Hours dropdown is replaced by a Chosen widget. JS functionality to delete a time slot or copy its values to the next slot no longer works as expected.

Proposed resolution

Add '#chosen' => FALSE to the hours dropdown.

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code - widget

Created by

πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pfrenssen
  • Status changed to Needs review 6 days ago
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia
  • Pipeline finished with Failed
    6 days ago
    Total: 149s
    #206630
  • πŸ‡³πŸ‡±Netherlands johnv

    I am sorry, I cannot replicate.
    - enable office_hours, chosen modules
    - add office_hours field with select list dropdown, for 24 hours notation and 60*1 minute notation.
    - add 'List (integer)' field with some values
    - customize /admin/config/user-interface/chosen to fall outside boundaries.
    - edit node.

    Only when explicitly adding the 'Chosen' widget (from Chosen Field module), I get the chosen widget.

Production build 0.69.0 2024