View source
<?php
class render_cache_hijack_context_reaction_block extends context_reaction_block {
function block_get_blocks_by_region($region) {
module_load_include('module', 'block', 'block');
$build = $this
->block_list_build($region);
if ($this
->is_editable_region($region)) {
$build = $this
->editable_region($region, $build);
}
return $build;
}
function get_blocks($region = NULL, $context = NULL, $reset = FALSE) {
if (!variable_get('render_cache_cache_block_list', TRUE)) {
return parent::get_blocks($region, $context, $reset);
}
$hash = md5(serialize(array(
$region,
$context,
)));
$cid = "render_cache:context_reaction_block:block_list:{$hash}";
if (!$reset) {
$cache = cache_get($cid);
if (!empty($cache->data)) {
return $cache->data;
}
}
$blocks = parent::get_blocks($region, $context, $reset);
cache_set($cid, $blocks);
return $blocks;
}
function block_list_build($region) {
global $theme;
$cache_info_default = render_cache_cache_info_defaults();
$cache_info_default['keys'] = array(
'render_cache',
);
drupal_alter('render_cache_block_default_cache_info', $cache_info_default, $default_alter_context);
$markup =& drupal_static('context_reaction_block_list_build');
$contexts = context_active_contexts();
$cid_map = array();
$cache_info_map = array();
if (!isset($markup)) {
$info = $this
->get_blocks();
$markup = array();
$context_blocks = array();
foreach ($contexts as $context) {
$options = $this
->fetch_from_context($context);
if (!empty($options['blocks'])) {
foreach ($options['blocks'] as $context_block) {
$bid = "{$context_block['module']}-{$context_block['delta']}";
if (isset($info[$bid])) {
$block = (object) array_merge((array) $info[$bid], $context_block);
$block->bid = $bid;
$block->context = $context->name;
$block->title = isset($info[$block->bid]->title) ? $info[$block->bid]->title : NULL;
$alter_context = array(
'context' => $context,
'bid' => $bid,
'module' => $context_block['module'],
'delta' => $context_block['delta'],
'granularity' => isset($info[$block->bid]->cache) ? $info[$block->bid]->cache : DRUPAL_NO_CACHE,
);
$alter_context['region'] = $block->region;
$cache_info = $cache_info_default;
$cache_info['keys'][] = $block->region;
$cache_info['keys'][] = $bid;
$cid_map[$bid] = $this
->render_cache_get_cid($block, $cache_info, $alter_context);
$cache_info_map[$bid] = $cache_info;
$context_blocks[$block->region][$block->bid] = $block;
$block->cache = DRUPAL_NO_CACHE;
}
}
}
}
$cids = array_filter(array_values($cid_map));
$cached_blocks = $cids ? cache_get_multiple($cids, 'cache_render') : array();
$this
->is_editable_check($context_blocks);
$active_regions = $this
->system_region_list($theme);
foreach ($context_blocks as $r => $blocks) {
$markup[$r] = array();
if (array_key_exists($r, $active_regions)) {
uasort($blocks, array(
'context_reaction_block',
'block_sort',
));
foreach ($blocks as $bid => $block) {
$cid = $cid_map[$bid];
if (isset($cached_blocks[$cid])) {
$build = $cached_blocks[$cid]->data;
}
else {
$this
->build_block($block);
if ($this
->is_editable_region($r)) {
$this
->editable_block($block);
}
$build = $this
->render_cache_block($block, $bid, $cid_map[$bid], $cache_info_map[$bid]);
}
render_cache_process_attached_callbacks($build, $bid);
$markup[$r]["{$block->module}_{$block->delta}"] = $build;
}
}
}
}
return isset($markup[$region]) ? $markup[$region] : array();
}
protected function render_cache_get_cid($block, &$cache_info, $context) {
$cache_info += render_cache_cache_info_defaults();
drupal_alter('render_cache_block_cache_info', $cache_info, $block, $context);
if ($cache_info['granularity'] == DRUPAL_NO_CACHE) {
return NULL;
}
$cid_parts = array();
$hash = array();
if (!empty($cache_info['keys']) && is_array($cache_info['keys'])) {
$cid_parts = $cache_info['keys'];
}
$granularity = isset($cache_info['granularity']) ? $cache_info['granularity'] : NULL;
$cid_parts = array_merge($cid_parts, drupal_render_cid_parts($granularity));
$hash['module'] = $context['module'];
$hash['delta'] = $context['delta'];
$hash['context'] = $context['context']->name;
$hash['render_method'] = !empty($cache_info['render_cache_render_to_markup']);
if ($hash['render_method']) {
$hash['render_options'] = serialize($cache_info['render_cache_render_to_markup']);
}
drupal_alter('render_cache_block_hash', $hash, $block, $cache_info, $context);
$cid_parts[] = sha1(implode('-', $hash));
drupal_alter('render_cache_block_cid', $cid_parts, $block, $cache_info, $context);
return implode(':', $cid_parts);
}
protected function build_block($block) {
$array = module_invoke($block->module, 'block_view', $block->delta);
$delta = str_replace('-', '_', $block->delta);
drupal_alter(array(
'block_view',
"block_view_{$block->module}_{$delta}",
), $array, $block);
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->{$k} = $v;
}
}
if (isset($block->content) && $block->content) {
if (is_string($block->content)) {
$block->content = array(
'#markup' => $block->content,
);
}
if ($block->title) {
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
if (!isset($block->subject)) {
$block->subject = '';
}
}
}
protected function render_cache_block($block, $bid, $cid, $cache_info) {
if (is_string($block->content)) {
$block->content = array(
'#markup' => $block->content,
);
}
$build = $block->content;
unset($block->content);
$build += array(
'#attached' => array(),
);
if ($bid != 'system-main' && $bid != 'system-help') {
$build['#contextual_links']['block'] = array(
'admin/structure/block/manage',
array(
$block->module,
$block->delta,
),
);
}
$build['#block'] = $block;
$build['#theme_wrappers'][] = 'block';
if (!empty($cid)) {
$build['#cache'] = $cache_info;
$build['#cache']['cid'] = $cid;
return array(
'#markup' => drupal_render($build),
'#attached' => drupal_render_collect_attached($build, TRUE),
);
}
return $build;
}
}