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

&lt;fieldset id="some-id" class="well-small form-wrapper vertical-tabs-pane"&gt;
  &lt;legend&gt;
    &lt;span class="fieldset-legend"&gt;
      Lorem Ipsum
    &lt;/span&gt;
  &lt;/legend&gt;
  &lt;div class="fieldset-wrapper"&gt;
    &lt;span class="some-field"&gt;
      &lt;!-- Irrelevant --&gt;  
    &lt;/span&gt;
  &lt;/div&gt;
&lt;/fieldset&gt;


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


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


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


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

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

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

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

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