views_block

Versions
5
views_block($op = 'list', $delta = 0)
6
views_block($op = 'list', $delta = 0, $edit = array())

Implementation of hook_block

Code

contributions/views/views.module, line 304

<?php
function views_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      // Try to avoid instantiating all the views just to get the blocks info.
      views_include('cache');
      $cache = views_cache_get('views_block_items', TRUE);
      if ($cache && is_array($cache->data)) {
        return $cache->data;
      }

      $items = array();
      $views = views_get_all_views();
      foreach ($views as $view) {
        // disabled views get nothing.
        if (!empty($view->disabled)) {
          continue;
        }

        $view->init_display();
        foreach ($view->display as $display_id => $display) {

          if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) {
            $result = $display->handler->execute_hook_block();
            if (is_array($result)) {
              $items = array_merge($items, $result);
            }
          }

          if (isset($display->handler) && $display->handler->get_option('exposed_block')) {
            $result = $display->handler->get_special_blocks();
            if (is_array($result)) {
              $items = array_merge($items, $result);
            }
          }
        }
      }

      // 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($items);
      foreach ($keys as $delta) {
        if (strlen($delta) >= 32) {
          $hash = md5($delta);
          $hashes[$hash] = $delta;
          $items[$hash] = $items[$delta];
          unset($items[$delta]);
        }
      }

      // Only save hashes if they have changed.
      $old_hashes = variable_get('views_block_hashes', array());
      if ($hashes != $old_hashes) {
        variable_set('views_block_hashes', $hashes);
      }
      // Save memory: Destroy those views.
      foreach ($views as $view) {
        $view->destroy();
      }

      views_cache_set('views_block_items', $items, TRUE);

      return $items;
    case 'view':
      $start = views_microtime();
      // if this is 32, this should be an md5 hash.
      if (strlen($delta) == 32) {
        $hashes = variable_get('views_block_hashes', array());
        if (!empty($hashes[$delta])) {
          $delta = $hashes[$delta];
        }
      }

      // This indicates it's a special one.
      if (substr($delta, 0, 1) == '-') {
        list($nothing, $type, $name, $display_id) = explode('-', $delta);
        // Put the - back on.
        $type = '-' . $type;
        if ($view = views_get_view($name)) {
          if ($view->access($display_id)) {
            $view->set_display($display_id);
            if (isset($view->display_handler)) {
              $output = $view->display_handler->view_special_blocks($type);
              $view->destroy();
              return $output;
            }
          }
          $view->destroy();
        }
      }

      list($name, $display_id) = explode('-', $delta);
      // Load the view
      if ($view = views_get_view($name)) {
        if ($view->access($display_id)) {
          $output = $view->execute_display($display_id);
          vpr("Block $view->name execute time: " . (views_microtime() - $start) * 1000 . "ms");
          $view->destroy();
          return $output;
        }
        $view->destroy();
      }
      break;
  }
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.