Problem/Motivation

When adding option attributes to a select element, it seems to change its class.

Without form_options_attributes:
<select data-drupal-selector="edit-field-categorie" id="edit-field-categorie" name="field_categorie" class="form-select form-element form-element--type-select">

With form_options_attributes:
<select data-drupal-selector="edit-field-categorie" id="edit-field-categorie" name="field_categorie" class="form-select">

Steps to reproduce

  1. Install form_options_attributes
  2. Use hook_form_FORM_ID_alter to add option attributes on a select element on a node add form.

(We are using Claro as the back-office theme.)

Proposed resolution

The class seems to be overriden in form_options_attributes_preprocess_form_options_attributes__select__options_attributes. It should use the original class instead.

Data model changes

CommentFileSizeAuthor
#2 form_options_attributes-3176067-2.patch730 bytesprudloff
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

prudloff created an issue. See original summary.

prudloff’s picture

Status: Active » Needs review
StatusFileSize
new730 bytes

The attached patch makes it use the original class.

mykola dolynskyi’s picture

Facing same problem.
Solved like this:

function form_options_attributes_preprocess_form_options_attributes__select__options_attributes(&$variables) {
  $element = $variables['element'];

  Element::setAttributes($element, ['id', 'name', 'size']);
  RenderElement::setAttributes($element, ['form-select']);

  /* new line */
  $element['#attributes']['class'] = array_unique(array_merge(
    empty($variables['attributes']['class'])? [] : $variables['attributes']['class'],
    $element['#attributes']['class']), SORT_REGULAR);
  /* new line end */

  $variables['attributes'] = $element['#attributes'];
...

Renrhaf made their first commit to this issue’s fork.

renrhaf’s picture

Faced a problem also with classes being overriden (in checkboxes options)
Patch attached https://git.drupalcode.org/project/form_options_attributes/-/merge_reque...

  • zengenuity committed 5eb77bf on 8.x-1.x
    Issue #3176067 by Renrhaf, prudloff: Wrong class on select element
    

  • zengenuity committed 29fe0ca on 2.0.x
    Issue #3311706: Port select element fix from #3176067 to 2.0.x
    
zengenuity’s picture

Status: Needs review » Fixed

Committed the fix for select elements. The fix for checkboxes doesn't seem to be needed any more due to other code updates.

Rolled new 8.x-1.4 and 2.0.2 releases.

Thanks!

Status: Fixed » Closed (fixed)

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