17

In Drupal 7 there was option to have fieldset collapsible or not, and to restrict default state of this form element (collapsed, or uncollapsed). Example:

$form['contact_data'] = array(
        '#title' => t("Contact data"),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
);

Now, in Drupal 8 I cannot find attributtes #collapsible and #collapsed in Drupal\Core\Render\Element\Fieldset. In official documentation of Fieldset element is not mentioned any new element, which should substitute the Fieldset element.

Also there is not Form API elements overview for Drupal 8, such as for Drupal 7. You have to manually search for all Render Elements definition in code, which is more time consuming (especially when you would like to see elements overview and compare them).

3 Answers 3

36

All collapsible fieldsets have been replaced with HTML5 details elements.

Fieldset and legand are very hard to style and its not recommended to use with Drupal 8 any more. Instead its replaced with more cross browser way, which is the HTML5 Details & Summary tags.

'#type' => 'details'

https://www.drupal.org/node/1852020

Drupal 7

$form['advanced'] = array(
  '#type' => 'fieldset',
  '#title' => t('Advanced settings'),
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,
  '#description' => t('Lorem ipsum.'),
);

Drupal 8

$form['advanced'] = array(
  '#type' => 'details',
  '#title' => t('Advanced settings'),
  '#description' => t('Lorem ipsum.'),
  '#open' => TRUE, // Controls the HTML5 'open' attribute. Defaults to FALSE.
);
5
  • 2
    Thanks you for answer. But again, I am very frustrated from the fact, that this (not small) change is only described on some "change record" page and is not updated to official documentation pages... See: Online documentation: Not done... Commented Nov 15, 2016 at 10:03
  • 1
    Change records are the perfect place to look for how thing X changed in Druapl 8. They're easily searchable and always contain before/after examples. Both code and documentation is mostly written in people's free time. Don't complain, help improve it.
    – Berdir
    Commented Nov 15, 2016 at 20:56
  • @Berdir Yes, I know that documentation in written in free time and I appreciate that :) But personally I don't think, that change records are easily searchable (except from google). Primary, as developer, you search in documentation (or API documentation), so just simple link reference to specific change record would be sufficient (and it is not as time consuming as writing those examples in change records). Apart from that, I must admit, that the code examples in change records are very good. Commented Nov 16, 2016 at 8:46
  • Where do you ad this code in drupal 8 files?
    – DavSev
    Commented Oct 9, 2018 at 10:47
  • How to set it to not be collapsible?
    – liquidcms
    Commented May 20, 2021 at 3:13
0

Example using HereDoc notation to store the HTML Description:

$desc_html = <<<HTML
<p>
  <strong>Put the HTML to be displayed when open in here.</strong>
<p>
HTML;

$form['advanced'] = array(
  '#type' => 'details',
  '#title' => t('Advanced settings'),
  '#description' => t($desc_html),
  '#open' => TRUE, // Controls the HTML5 'open' attribute. Defaults to FALSE.
);
3
  • 1
    Variables should never be passed to t(), see drupal.stackexchange.com/a/9366/8881 for an explanation.
    – berliner
    Commented Jul 23, 2021 at 9:36
  • Good catch, thanks! Do you know if you can use a HEREDOC string in a t()? The docs didn't specify. (at least I couldn't find it)
    – diamondsea
    Commented Jul 28, 2021 at 17:30
  • No I don't. But I personally wouldn't do it even if it was possible as it might confuse users who are translating strings in the UI. In the example of this answer I would only put the pure string into t() and then you can put that into the HereDoc string if you need to. Better even not to use markup at all, but to do the styling via CSS.
    – berliner
    Commented Jul 29, 2021 at 10:57
0

Drupal 8, change the form group default closed value to open programatically -

function hook_field_group_form_process_build_alter(array &$element, FormStateInterface $form_state, &$complete_form) {
      $node = $form_state->getFormObject()->getEntity();
      if ($node->hasField('field_pub_date') && !$node->get('field_pub_date')->isEmpty()) {
        $element['field_pub_date']['#attributes']['open'] = TRUE;
      }
}

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.