0

In a custom module, I have created a custom form. In this form, I need to alter the markup of the radio buttons wrapper and add a new attribute. After reading many posts and testing, I can alter all the form elements (form-item, first fieldset...) except this wrapper.

This is the markup I am outputting.

<fieldset ...>
  <legend>Mylegend</legend>
    <div class="fieldset-wrapper">
      <div id="edit-myfield"> <!-- < I need to alter the attributes of this div -->
        <div class="form-item js-form-item form-type-radio js-form-type-radio...">
    ...
        </div>
        <div class="form-item js-form-item form-type-radio js-form-type-radio...">
          ...
        </div>
      </div>
    </div>
  </fieldset>

I see that the related system template is radios.html.twig. I tried to copy and paste this template in the templates directory of my module without success. I tried to use theme => 'my custom theme' in the definition of my field. I tried to use a preprocess function in my module.

What is the simpler method to alter this wrapper?

1 Answer 1

0

You can do this directly in the form.

Add this to the radios element:

'#attributes' => [
  'attribute_name' = 'attribute_value'
],
2
  • Hello Jdrupal. The first solution add a new attribute on the first fieldset (and not on the edit-myfield div). And If I make a dpm in a form_alter I don't see a wrapper element. So I can't add a new attribute.
    – user28220
    Commented Jul 29, 2019 at 16:19
  • You should also be able to add it on the radios element, if you don't have a wrapper.
    – user72672
    Commented Jul 29, 2019 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.