You are here

function _fieldset_helper_set_collapsible_fieldset_ids in Fieldset helper 6

Set a collapsible fieldset's id based on the associated array keys.

All fieldset id's will begin with 'fieldset-' to insure their uniqueness.

Parameters

&$form: Nested array of form elements that comprise the form.

$form_id: String representing the id of the form.

$id: Based id for collapsible fieldsets.

Return value

TRUE if a form contains a collapsible fieldset.

2 calls to _fieldset_helper_set_collapsible_fieldset_ids()
FieldsetHelperTestCase::testFieldsetHelperSetCollapsibleFieldsetIds in ./fieldset_helper.test
Test adding fieldset id's to $form array.
fieldset_helper_form_alter in ./fieldset_helper.module
Implementation of hook_form_alter().

File

./fieldset_helper.module, line 165
Saves the collapsed state of a Drupal collapsible fieldset.

Code

function _fieldset_helper_set_collapsible_fieldset_ids(&$form, $form_id, $id = 'fieldset') {
  static $has_collapsible_fieldset;
  foreach ($form as $key => $value) {

    // If $key is a property (begins with a hash (#) then continue.
    if (strpos($key, '#') === 0) {
      continue;
    }

    // If this element has no type or it is not a fieldset then continue.
    if (!isset($form[$key]['#type']) || $form[$key]['#type'] != 'fieldset') {
      continue;
    }

    // Add key, as valid DOM id, to fieldset id.
    $fieldset_id = _fieldset_helper_format_id($id . '-' . $key);

    // Handle collapsible fieldset.
    if (isset($form[$key]['#collapsible']) && $form[$key]['#collapsible']) {

      // Add id to the collapsible fieldset if an id is not defined.
      if (!isset($form[$key]['#attributes']['id'])) {
        $form[$key]['#attributes']['id'] = $fieldset_id;
      }

      // Store that the form has a collapsible fieldset in a lookup table.
      $has_collapsible_fieldset[$form_id] = TRUE;
    }

    // Recurse downward
    _fieldset_helper_set_collapsible_fieldset_ids($form[$key], $form_id, $fieldset_id);
  }

  // Return if the form has a collapsible fieldset.
  return isset($has_collapsible_fieldset[$form_id]) && $has_collapsible_fieldset[$form_id] ? TRUE : FALSE;
}