3

It's similar and connected to How to modify attributes of <label> in form definition function? - This time I want to ask how to modify attributes of a <div class="fieldset-wrapper">. HTML structure of a fieldset is:

<fieldset id="some-id" class="well-small form-wrapper vertical-tabs-pane">
  <legend>
    <span class="fieldset-legend">
      Lorem Ipsum
    </span>
  </legend>
  <div class="fieldset-wrapper">
    <span class="some-field">
      <!-- Irrelevant -->  
    </span>
  </div>
</fieldset>

I need class well-small directly on a fieldset-wrapper div. But if I apply it to a fieldset itself, it appears when you see it in sample above. When I move it one level down, well, there are only fields there, so it's one level too far instead of one level too close to the document root. Is there a way to conditionally add classes and other attributes directly from form building functions?

CSS and JS workarounds are out of the scope for me. I know how to work around it. Now I'm asking to know how to do it directly in PHP.

1 Answer 1

5

Looking at theme_fieldset the fieldset-wrapper class is hardcoded and that div doesn't take any additional classes from the $variables array.

So looks like the only option is to override theme_fieldset in your template.php of the active theme. If your conditional variables are present in $variables then you could do the logic straight in the theme_fieldset override function (replace mythemename with the name of your theme)

function mythemename_fieldset($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array('id'));
  _form_set_class($element, array('form-wrapper'));

  $output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
  if (!empty($element['#title'])) {
    // Always wrap fieldset legends in a SPAN for CSS positioning.
    $output .= '<legend><span class="fieldset-legend">' . $element['#title'] . '</span></legend>';
  }


  // Condition/logic here
  $class = '';
  if($variables['....'] == '....') {
    $class = ' well-small';
  }


  $output .= '<div class="fieldset-wrapper'.$class.'">';
  if (!empty($element['#description'])) {
    $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
  }
  $output .= $element['#children'];
  if (isset($element['#value'])) {
    $output .= $element['#value'];
  }
  $output .= '</div>';
  $output .= "</fieldset>\n";
  return $output;
}

Otherwise, a more elegant solution (by @Mołot) would be to supply use drupal_attributes like so:

mythemename_fieldset(&$variables) {
  $element = $variables['element'];
  element_set_attributes($element, array('id'));
  _form_set_class($element, array('form-wrapper'));

  $output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
  if (!empty($element['#title'])) {
    // Always wrap fieldset legends in a SPAN for CSS positioning.
    $output .= '<legend><span class="fieldset-legend">' . $element['#title'] . '</span></legend>';
  }

  $element['#wrapper-attributes']['class'][] = 'fieldset-wrapper';

  $output .= '<div' . drupal_attributes($element['#wrapper-attributes']) . '">';
  if (!empty($element['#description'])) {
    $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
  }
  $output .= $element['#children'];
  if (isset($element['#value'])) {
    $output .= $element['#value'];
  }
  $output .= '</div>';
  $output .= "</fieldset>\n";
  return $output;
}
11
  • Good idea. Now, how do I override default theme functions? I can't quite get hook_theme_registry_alter to work in my template :/
    – Mołot
    Commented Nov 18, 2013 at 14:23
  • It works just like a hook no? theme_fieldset will be overridden by mytheme_fieldset in template.php of mytheme.
    – Beebee
    Commented Nov 18, 2013 at 14:25
  • Sadly, simply failed to work. die() implanted in it to test didn't happened. Will see in module.
    – Mołot
    Commented Nov 18, 2013 at 14:26
  • Don't think it'll work from a module sadly either. It should work in template.php ... strange.
    – Beebee
    Commented Nov 18, 2013 at 14:30
  • 1
    @Mołot I smell another question about broken theme registry :P
    – Beebee
    Commented Nov 18, 2013 at 14:51

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.