_fieldset_helper_set_collapsible_fieldset_ids

contrib-jrockowitz/fieldset_helper/fieldset_helper.module, line 170

Versions
6
_fieldset_helper_set_collapsible_fieldset_ids(&$form, $form_id, $id='fieldset')

Set collapsible fieldsets 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 functions call _fieldset_helper_set_collapsible_fieldset_ids()

fieldset_helper_form_alter in contrib-jrockowitz/fieldset_helper/fieldset_helper.module
Implementation of hook_form_alter().
_fieldset_helper_set_collapsible_fieldset_ids in contrib-jrockowitz/fieldset_helper/fieldset_helper.module
Set collapsible fieldsets id based on the associated array keys.

Code

<?php
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;
      }

      // Set that this form has a collapsible fieldset.
      $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;
}
?>