You are here

function mefibs_block_info in MEFIBS - More exposed forms in blocks 7

Implements hook_block_info().

File

./mefibs.module, line 175
Primarily Drupal hooks and global API functions to manipulate views and to provide an additional block with an exposed filter form.

Code

function mefibs_block_info() {

  // Try to avoid instantiating all the views just to get the blocks info.
  views_include('cache');
  $cache = views_cache_get('mefibs_block_items', TRUE);
  if ($cache && is_array($cache->data) && count($cache->data)) {
    return $cache->data;
  }
  $blocks = array();
  $views = views_get_enabled_views();
  foreach ($views as $view) {
    $view
      ->init_display();
    foreach ($view->display as $display) {
      if (isset($display->handler)) {
        $display_handler = $view->display[$display->id]->handler;
        if (!mefibs_display_is_mefibs_enabled($display_handler)) {
          continue;
        }
        $mefibs_blocks = $display_handler->extender['mefibs_blocks']
          ->get_enabled_blocks();
        if (count($mefibs_blocks)) {
          foreach ($mefibs_blocks as $machine_name => $name) {
            $blocks['mefibs_' . $view->name . '--' . $display->id . '--' . $machine_name] = array(
              'info' => t('Exposed form: !view_name: !block', array(
                '!view_name' => $view->name . '-' . $display->id,
                '!block' => check_plain($name),
              )),
              'cache' => DRUPAL_NO_CACHE,
            );
          }
        }
      }
    }

    // Save memory: Destroy the view.
    $view
      ->destroy();
  }

  // The block.module has a delta length limit of 32, but our deltas can
  // unfortunately be longer because view names can be 32 and display IDs
  // can also be 32. So for very long deltas, change to md5 hashes.
  $hashes = array();

  // Get the keys because we're modifying the array and we don't want to
  // confuse PHP too much.
  $keys = array_keys($blocks);
  foreach ($keys as $delta) {
    if (strlen($delta) >= 32) {
      $hash = md5($delta);
      $hashes[$hash] = $delta;
      $blocks[$hash] = $blocks[$delta];
      unset($blocks[$delta]);
    }
  }

  // Only save hashes if they have changed.
  $old_hashes = variable_get('mefibs_block_hashes', array());
  if ($hashes != $old_hashes) {
    variable_set('mefibs_block_hashes', $hashes);
  }
  views_cache_set('mefibs_block_items', $blocks, TRUE);

  // Delete stale block data
  if (count($blocks)) {
    db_delete('block')
      ->condition('module', 'mefibs')
      ->condition('delta', array_keys($blocks), 'NOT IN')
      ->execute();
  }
  return $blocks;
}