You are here

function _fieldset_helper_alter_fieldset in Fieldset helper 7.2

Alter fieldset element and set default collapsible and collapsed attributes.

1 call to _fieldset_helper_alter_fieldset()
fieldset_helper_preprocess_fieldset in ./fieldset_helper.theme.inc
Implements hook_preprocess_fieldset().

File

./fieldset_helper.theme.inc, line 138
Theme functions for fieldset helper module

Code

function _fieldset_helper_alter_fieldset(&$form_element) {

  // Make sure each fieldset element is only altered once.
  if (isset($form_element['#fieldset_helper_processed'])) {
    return;
  }

  // Don't set collapsible or collapsed classes for fieldsets that are missing
  // a <legend> (aka #title).
  if (empty($form_element['#title'])) {
    return;
  }

  // Set id for fieldsets without them.
  if (empty($form_element['#id'])) {
    $id = drupal_html_id(preg_replace('/[^a-z0-9]+/', '-', drupal_strtolower('fieldset-' . $form_element['#title'])));
    $form_element['#id'] = $id;
  }

  // Set attribute id.
  $form_element['#attributes']['id'] = $form_element['#id'];

  // Set children. Prevents 'Undefined index: #children in theme_fieldset()'.
  if (!isset($form_element['#children'])) {
    $form_element['#children'] = '';
  }

  // Set all fieldsets to be collapsible.
  if ($default_collapsible = fieldset_helper_default_collapsible()) {
    $form_element['#collapsible'] = $default_collapsible;
  }

  // Collapse all collapsible fieldsets by default.
  if (isset($form_element['#collapsible']) && $form_element['#collapsible'] === TRUE) {
    if ($default_collapsed = fieldset_helper_default_collapsed()) {
      $form_element['#collapsed'] = $default_collapsed;
    }
  }

  // Set fieldset state
  if (user_access('save fieldset state')) {
    $collapsed = isset($form_element['#collapsed']) ? $form_element['#collapsed'] : FALSE;
    $form_element['#collapsed'] = fieldset_helper_state_manager_get_state($form_element['#id'], $collapsed);
  }

  // Alter fieldset element.
  drupal_alter('fieldset', $form_element);

  // Unset collapsible and collapsed classes and collapse.js
  if (isset($form_element['#attributes']['class'])) {
    $form_element['#attributes']['class'] = array_diff($form_element['#attributes']['class'], array(
      'collapsible',
      'collapsed',
    ));
  }

  // Reset collapsible fieldsets. From: form_process_fieldset(&$form_element, &$form_state).
  if (!empty($form_element['#collapsible'])) {
    $form_element['#attributes']['class'][] = 'collapsible';
    if (!empty($form_element['#collapsed'])) {
      $form_element['#attributes']['class'][] = 'collapsed';
    }

    // Set has collapsible fieldset.
    $is_vertical_tab = isset($form_element['#group']) && $form_element['#group'] == TRUE;
    if (!$is_vertical_tab) {
      fieldset_helper_number_of_collapsible_fieldset(1);
    }
  }
  $form_element['#fieldset_helper_processed'] = TRUE;
}