1

The node edit form contains the following form element.

  $form['revision_information'] = array(
    '#type' => 'details',
    '#group' => 'advanced',
    '#title' => t('Revision information'),
    // Open by default when "Create new revision" is checked.
    '#open' => $node->isNewRevision(),
    '#attributes' => array(
      'class' => array('node-form-revision-information'),
    ),
    '#attached' => array(
      'library' => array('node/drupal.node'),
    ),
    '#weight' => 20,
    '#optional' => TRUE,
  );

What does the #optional property mean for it?

I looked at both the Details and the VerticalTabs classes, but neither of them seems to handle that property.

What does setting #optional to TRUE do?

4

This is handled in the render element Details:

Details.php:

  public static function preRenderDetails($element) {
    ...
    // Do not render optional details elements if there are no children.
    if (isset($element['#parents'])) {
      $group = implode('][', $element['#parents']);
      if (!empty($element['#optional']) && !Element::getVisibleChildren($element['#groups'][$group])) {
        $element['#printed'] = TRUE;
      }
    }
    return $element;
  }

If #optional is TRUE and there are no visible children, then the element is not rendered.

  • Do you have any idea, why these properties are not documented properly in each Render Element (e.g. Details element)? How are users supposed to use them, they have to search in code and "resolve" written conditions? – Juraj Nemec Nov 15 '16 at 9:37

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.