0

I have created a fieldset (which groups a field collection). Because it has a few nested levels and many fields, I want it to be shown collapsed. I am trying this code but it does not work. The id is retrieved directly from the source code of the web browser. The fieldset is the top-level, and there are more fieldsets inside of it. The form is displayed in the Admin page, the node edit form. The field "field-seo-general" is a #container.

function hook_form_alter(&$form, &$form_state, $form_id){
    if($form_id == "my_form"){
        $form["field-seo-general"]['options']['attributes']["#collapsible"] = TRUE;
        $form["field-seo-general"]['options']['attributes']["#collapsed"] = TRUE;
      ...
} // function
  • I don't have time to test this out myself, but try adding the classes "collapsible" and "collapsed" to your field_seo_general field. so something like $form['field_seo_general']['attributes']['class'][] = 'collapsible'; etc. Edit: If that doesn't work, try taking out ['options']['attributes'] in each line. – Erin McLaughlin Oct 29 '15 at 18:04
  • Thanks, I have solved at high level with module Field Collection Fieldset. – Cesar Oct 29 '15 at 18:13
  • I tested by removing options and attributes. I will try later your option and compare with what the module does. – Cesar Oct 29 '15 at 18:15
0

I have solved it installing the module Field Collection Fieldset, and instead of Embedding the Field Collection, I have put it as a Fieldset, and then one of the options is to be shown Collapsed at rendering. All with a module.

0

The field "field-seo-general" is a #container.

The The Form API entry for container does not include #collapsed or #collapsible as properties.

It seems that fieldset is the only element that can use the #collapsed and #collapsible properties.

Change the #type for field-seo-general to fieldset to make it collapsible. If field-seo-general needs to be a container, then a second option would be to create a fieldset and place the field-seo-general container within it. However, doing this will add one more dimension within the values array for your fields when the form is submitted, so you need to take that into account.

$container_seo_general = $form['field_seo_general'];
unset($form['field_seo_general']);
$fieldset = array(
  '#type' => 'fieldset',
  '#title' => 'My fieldset title',
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
);
$fieldset['field_seo_general'] = $container_seo_general;
$form['my_module_fieldset'] = $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.