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
- Install the Chosen module
- 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.