You are here

function panels_mini_block_list_alter in Panels 7.3

Implements hook_block_list_alter().

Remove the block if the required contexts are not available.

File

panels_mini/panels_mini.module, line 147
panels_mini.module

Code

function panels_mini_block_list_alter(&$blocks) {
  $keys_to_cache = array();
  $has_cache_hit = FALSE;
  foreach ($blocks as $key => $block) {
    if ($block->module != 'panels_mini') {

      // Ignore anything that is not a mini panel.
      continue;
    }
    $result = cache_get('panels_mini_block_list_alter:' . $key, 'cache_panels');
    if (empty($result->data)) {

      // If not a cache hit, queue it for caching.
      $keys_to_cache[] = $key;
    }
    else {

      // If a cache hit, return the cached result and indicate we had a hit.
      $blocks[$key] = $result->data;
      $has_cache_hit = TRUE;
    }
  }

  // If we don't need to cache anything, exit out of the function.
  if (empty($keys_to_cache)) {
    return;
  }
  $current_page = FALSE;
  if (module_exists('page_manager')) {
    $current_page = page_manager_get_current_page();
  }

  // If we didn't have any cache hits, load all at once to save time.
  if (!$has_cache_hit) {
    panels_mini_load_all();
  }

  // Iterate through all non-cached keys.
  foreach ($keys_to_cache as $key) {
    $block = $blocks[$key];
    if ($block->module != 'panels_mini') {

      // This block was added by a contrib module, leave it in the list.
      continue;
    }
    $panel_mini = panels_mini_load($block->delta);
    if (empty($panel_mini)) {

      // Bail out early if the specified mini panel doesn't exist.
      unset($blocks[$key]);
      continue;
    }
    if (!empty($panel_mini->requiredcontexts)) {
      if (!$current_page || empty($current_page['contexts'])) {
        unset($blocks[$key]);
        continue;
      }
      else {
        $required = array();
        foreach ($panel_mini->requiredcontexts as $context) {
          $info = ctools_get_context($context['name']);
          $required[] = new ctools_context_required($context['identifier'], $info['context name']);
        }
        if (!ctools_context_match_requirements($current_page['contexts'], $required)) {
          unset($blocks[$key]);
          continue;
        }
      }
    }

    // Cache our results.
    cache_set('panels_mini_block_list_alter:' . $key, $blocks[$key], 'cache_panels', CACHE_TEMPORARY);
  }
}