2

By default, Bootstrap will render the input radio and checkbox elements as children of the label. This is done in Drupal\bootstrap\Plugin\Preprocess\FormElement, starting at line 45:

// Place single checkboxes and radios in the label field.
if (($checkbox || $radio)) {
  $label = Element::create($variables['label']);
  $children = &$label->getProperty('children', '');

In some instances, I don't want these printed as siblings - how can I undo this? form-element.html.twig isn't helpful unless I can somehow print the children first e.g. {{ label.#children }} and then twig will know not to print them again. Not sure if that solution is even possible.

Thanks!

2 Answers 2

1

At first I was going to say hook_form_alter, but when I started digging into the Bootstrap theme I found they've created their own plugin system. I would checkout this page, you might be able to get to the render array by creating a Bootstrap theme plugin: https://drupal-bootstrap.org/api/bootstrap/docs%21plugins%21Form.md/group/plugins_form/8

1
  • I posted a full answer with code samples but you are correct, the solution is to extend the appropriate \Preprocess\[element name] Commented Feb 21, 2017 at 14:28
0

As @icicleking states, bootstrap uses theme plugins that can be extended. So that we have a thorough answer for what I did I'll post it separately here.

Extend \Drupal\bootstrap\Plugin\Preprocess\FormElement. There's only one method, preprocessElement. In my case I replaced the following:

// Place single checkboxes and radios in the label field.
if (($checkbox || $radio)) {
  $label = Element::create($variables['label']);

with

// Place single checkboxes and radios in the label field.
if (($checkbox || $radio) && !$element->hasProperty('no_wrap_label')) {
  $label = Element::create($variables['label']);

Then you can use the no_wrap_label property on any element that you don't want to be wrapped inside a label, I'm using hook_preprocess_form_element:

function your_module_preprocess_form_element(&$variables) {
  if ($variables['name'] == 'field_field_name') {
    $variables['element']['#no_wrap_label'] = TRUE;
  }
}
1

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.