You are here

function global_filter_init in Views Global Filter 8

Same name and namespace in other branches
  1. 6 global_filter.module \global_filter_init()
  2. 7 global_filter.module \global_filter_init()

Implements hook_init().

For any of the code in this function to work, the global filter blocks in question need to be defined, but do not have to be visible and may be placed in the <none> region.

File

./global_filter.module, line 46
global_filter.module

Code

function global_filter_init() {
  if (isset($_REQUEST['clear-global-filters'])) {
    $filter_names = explode(',', $_REQUEST['clear-global-filters']);
    global_filter_clear_filters($filter_names);
    return;
  }
  foreach (global_filter_get_parameter(NULL) as $filter_key => $filter) {
    if (!empty($filter['name'])) {
      $name = $filter['name'];
      if (!empty($_POST['from_links_widget']) && $_POST['from_links_widget'] == $name) {
        global_filter_set_on_session($name, explode(',', check_plain($_POST[$name])));
      }
      elseif (isset($_REQUEST[$name]) && is_string($_REQUEST[$name]) && !isset($_POST[$name])) {

        // URL parameter, if present, overrides all of the below.
        // Example: http://mysite.com?field_country=23,7,12
        global_filter_set_on_session($name, explode(',', $_REQUEST[$name]));
      }
      else {
        $value = global_filter_get_session_value($name);
        if (!isset($value)) {

          // No value set for this session, eg at logout when a blank session
          // gets created for the now anonymous user.
          $default = global_filter_get_global_default($filter_key);
          global_filter_debug(t('Global Filter %name not set: setting default...', array(
            '%name' => $name,
          )));

          // Could still be empty.
          global_filter_set_on_session($name, $default);
        }
      }
    }
  }

  // Now that we've dealt with the special cases, make sure that filter blocks
  // and session data are available before anything else on the page needs
  // these.
  $active_blocks = array();
  $visible_filters_only = global_filter_get_module_parameter('visible_filters_only', FALSE);
  $active_filters = global_filter_active_filter_names($visible_filters_only);
  foreach ($active_filters as $key => $name) {
    $block_id = global_filter_get_parameter($key, 'block');
    $active_blocks[$block_id][] = $key;
  }

  // Once rendered each block is cached by global_filter_block_view(). So there
  // is no CPU overhead in rendering a block here and then have
  // global_filter_block_view() called once more by core at a later point.
  foreach ($active_blocks as $block_id => $keys) {
    global_filter_block_view($block_id);
  }

  // Auto-cycle filter.
  if (global_filter_get_module_parameter('view_autocycle_every_click')) {
    global_filter_get_view_next_value();
  }
}