You are here

function esi__block_handler in ESI: Edge Side Includes 6.2

Menu handler for ESIs

Render a particular block.

1 string reference to 'esi__block_handler'
esi_menu in ./esi.module
Implementation of hook_menu(). Define a menu-handler.

File

./esi.module, line 379
Adds support for ESI (Edge-Side-Include) integration, allowing blocks to be\ delivered by ESI, with support for per-block cache times.

Code

function esi__block_handler($bid, $page = NULL) {

  // Expect the bid format to be theme:region:module:delta
  if (substr_count($bid, ':') !== 3) {
    return FALSE;
  }
  list($passed_theme, $region, $module, $delta) = explode(':', $bid);
  require_once drupal_get_path('module', 'esi') . '/esi.inc';
  global $custom_theme, $theme_key, $user;

  // Save this page's internal URL.
  $last_arg = explode('/', $_GET['q']);
  $last_arg = array_pop($last_arg);

  // Block content may change per-page.
  // If this is true for the current block, the origin page url should be
  // provided as an argument.
  if (isset($page) && strpos($page, 'CACHE=') !== 0) {
    $q = base64_decode($page);
    if ($q == "") {
      $q = variable_get('site_frontpage', 'node');
    }
    $_GET['q'] = $q;
    if (variable_get('esi_override_request_uri', ESI_OVERRIDE_REQUEST_URI)) {
      $_SERVER['REQUEST_URI'] = $GLOBALS['base_path'] . $q;
    }
  }

  // Load up the ESI configuration for this block.
  $config = esi_get_settings($module . '_' . $delta);
  $current_user = $user;

  // Use user 0 if the scope is global or page.
  if ($config['scope'] == 2 || $config['scope'] == 3) {
    $user = user_load(0);
  }

  // Theme set-up.
  // We need to do this manually, because output is echo'd instead of returned.
  $custom_theme = $passed_theme;
  $theme_key = $passed_theme;
  init_theme();

  // Get the block and theme it.
  $block = _esi__get_block($passed_theme, $region, $module, $delta);
  if (!empty($block)) {
    $output = theme('block', $block);
  }
  else {
    $output = esi_fast404('block');
  }

  // Pass PER-USER or PER-ROLE cache info to varnish.
  // No-cache is header max age (TTL) controlled.
  // Per-page is passed as a url argument.
  esi_add_cache_headers($config['scope'], $config['max_age']);
  echo $output;
  $user = $current_user;
  esi_set_page_cache($output, $config['scope'], $last_arg);
  exit;
}