You are here

function fieldset_helper_preprocess_page in Fieldset helper 6.2

Same name and namespace in other branches
  1. 7.2 fieldset_helper.theme.inc \fieldset_helper_preprocess_page()

Implementation of hook_preprocess_page().

File

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

Code

function fieldset_helper_preprocess_page(&$variables) {

  // Make sure the user can save a fieldset's state
  if (!user_access('save fieldset state')) {
    return;
  }
  global $theme;

  // Count the number of collapsible fieldsets.
  $number_of_collapsible_fieldsets = 0;
  $region_list = system_region_list($theme);
  $region_list['content'] = TRUE;

  // Make sure 'content' is added to the region list.
  $regions = array_keys($region_list);
  foreach ($regions as $region) {

    // Using stripos() since it is much faster then executing preg_match() on every region.
    // Still use preg_match to confirm and count the number of collapsible fieldsets.
    if (stripos($variables[$region], '<fieldset') !== FALSE && ($count = preg_match_all('/<fieldset[^>]+class=["\'][^"\']*collapsible/', $variables[$region], $matches))) {
      $number_of_collapsible_fieldsets += $count;
    }
  }

  // Add toggle all. Must execute before $variable['scripts' are regenerated.
  $minimum = variable_get('fieldset_helper_toggle_all_minimum', 2);
  if ($number_of_collapsible_fieldsets >= $minimum) {
    $pages = variable_get('fieldset_helper_toggle_all_pages', 'admin/build/modules
admin/build/modules/list');
    if (!empty($pages) && (trim($pages) == '*' || drupal_match_path($_GET['q'], $pages))) {
      $variables['content'] = fieldset_helper_toggle_all_output() . $variables['content'];
    }
  }

  // Add fieldset_helper js and css.
  if ($number_of_collapsible_fieldsets > 0) {

    // Tao theme theme override theme_fieldset and it uses tao.js to handle collapsible fieldsets.
    if (stripos($variables['scripts'], 'tao.js') === FALSE) {
      drupal_add_js('misc/collapse.js', 'core');
    }
    drupal_add_js(drupal_get_path('module', 'fieldset_helper') . '/fieldset_helper.js');

    // Add ids and cookie duration.
    $settings['fieldset_helper_state_manager'] = array(
      'ids' => fieldset_helper_state_manager_get_lookup_id(),
      'cookie_duration' => variable_get('fieldset_helper_cookie_duration', 0),
    );
    drupal_add_js($settings, 'setting');

    // Reset scripts
    $variables['scripts'] = drupal_get_js();

    // Add and reset css
    drupal_add_css(drupal_get_path('module', 'fieldset_helper') . '/fieldset_helper.css');
    $variables['styles'] = drupal_get_css();
  }
}