2

I want to change fieldset settings on some form, I want to make it collapsed by default. The problem is this fieldset isn't made via usually form API. Its made via #theme_wrappers param. The question is how to alter #theme_wrappers output so it affect only certain form. Thanks!.

[#theme_wrappers] => Array(
  [0] => fieldset
)
1

I had to do this today on a Backdrop CMS site, but the problem and solution are exactly the same:

The problem is that most theme wrappers only add markup around the outside of something else. But a fieldset also has a process function, and process functions are not automatically added for #theme_wrappers.

The process function needs to run on the fieldset in order to take the #collapsible and #collapsed settings you add in the Form API to turn into the addition of the necessary CSS classes and JS library.

That means that not only do you need to set the values for #collapsible and #collapsed, but you also need to add the #process function. See below for an example.

/**
 * Implementation of hook_form_BASE_FORM_alter().
 * - Limits access to legacy fields.
 * - Collapses legacy fieldset.
 */
function mymodule_form_node_form_alter(&$form, &$form_state) {
  if (isset($form['field_images_inline'])) {
    $form['field_images_inline']['#access'] = user_access('administer nodes');

    // Add the collapsible and collapsed attributes.
    $lang = $form['langcode']['#value'];
    $form['field_images_inline'][$lang]['#collapsible'] = TRUE;
    $form['field_images_inline'][$lang]['#collapsed'] = TRUE;

    // Add the fieldset process function.
    $form['field_images_inline'][$lang]['#process'] = array('form_process_fieldset');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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