You are here

class render_cache_hijack_context_reaction_block in Render cache 7.2

Same name and namespace in other branches
  1. 7 modules/render_cache_context/context/plugins/render_cache_hijack_context_reaction_block.inc \render_cache_hijack_context_reaction_block

Hierarchy

Expanded class hierarchy of render_cache_hijack_context_reaction_block

2 string references to 'render_cache_hijack_context_reaction_block'
render_cache_context_context_plugins in modules/utility/render_cache_context/render_cache_context.module
Implementation of hook_context_plugins().
render_cache_context_context_registry_alter in modules/utility/render_cache_context/render_cache_context.module
Implements hook_context_registry_alter().

File

modules/utility/render_cache_context/context/plugins/render_cache_hijack_context_reaction_block.inc, line 3

View source
class render_cache_hijack_context_reaction_block extends context_reaction_block {

  /**
   * {@inheritdoc}
   */
  function block_get_blocks_by_region($region) {
    module_load_include('module', 'block', 'block');

    // renderBlockList returns a render array() already,
    // so no need to call _block_get_renderable_array().
    $build = $this
      ->renderBlockList($region);
    if ($this
      ->is_editable_region($region)) {
      $build = $this
        ->editable_region($region, $build);
    }
    return $build;
  }

  /**
   * Support the optional aggressive block list caching.
   *
   * Otherwise system_cron() clears context cache and parent::get_blocks()
   * runs _block_rehash(), which is slow, every 30 minutes.
   *
   * @param string|null $region
   * @param object|null $context
   * @param bool $reset
   *
   * @return array|bool
   */
  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;
  }

  /**
   * A caching version of block_list() plus _block_get_renderable_array().
   *
   * This function is identical to context_reaction_block::block_list()
   * except that $this->renderBlocks() is called instead of
   * _block_render_blocks() and that the sorting is done earlier.
   *
   * @see context_reaction_block::block_list().
   *
   * @param string $region
   *   The region to get blocks from.
   *
   * @return array
   *   A render array for this region. This render array will only contain
   *   #markup elements.
   */
  protected function renderBlockList($region) {
    module_load_include('module', 'block', 'block');
    $context_blocks =& drupal_static('context_reaction_block_list');
    $contexts = context_active_contexts();
    if (!isset($context_blocks)) {
      $info = $this
        ->get_blocks();
      $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->context = $context->name;
              $block->title = isset($info[$block->bid]->title) ? $info[$block->bid]->title : NULL;
              $block->cache = isset($info[$block->bid]->cache) ? $info[$block->bid]->cache : DRUPAL_NO_CACHE;
              $context_blocks[$block->region][$block->bid] = $block;
            }
          }
        }
      }
      $this
        ->is_editable_check($context_blocks);
      global $theme;
      $active_regions = $this
        ->system_region_list($theme);
      foreach ($context_blocks as $r => $blocks) {

        //only render blocks in an active region
        if (array_key_exists($r, $active_regions)) {

          // Sort blocks.
          uasort($blocks, array(
            'context_reaction_block',
            'block_sort',
          ));
          $context_blocks[$r] = $this
            ->renderBlocks($blocks, $r);

          // Make blocks editable if allowed.
          if ($this
            ->is_editable_region($r)) {
            foreach ($context_blocks[$r] as $key => $block) {
              $context_blocks[$r][$key] = $this
                ->editable_block($block);
            }
          }
        }
      }
    }
    return isset($context_blocks[$region]) ? $context_blocks[$region] : array();
  }
  protected function renderBlocks($context_blocks, $region) {

    // Save the region to the $context.
    $context = array(
      'region' => $region,
    );

    // Convert the blocks array into the right format.
    $blocks = array();
    foreach ($context_blocks as $key => $block) {
      $blocks["{$block->module}_{$block->delta}"] = $block;
    }

    // Delegate to render cache controller.
    $rcc = render_cache_get_controller('block');
    $rcc
      ->setContext($context);
    $build = $rcc
      ->view($blocks);
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
context_reaction::$description property
context_reaction::$plugin property
context_reaction::$title property
context_reaction::fetch_from_context function Retrieve options from the context provided. 1
context_reaction::get_contexts function Retrieve active contexts that have values for this reaction.
context_reaction::__clone function Clone our references when we're being cloned.
context_reaction::__construct function Constructor. Do not override.
context_reaction_block::block_list function An alternative version of block_list() that provides any context enabled blocks.
context_reaction_block::block_sort static function Sort callback.
context_reaction_block::context_block_ajax_rendering_allowed private function Allow modules to selectively allow ajax rendering of a specific block
context_reaction_block::editable_block protected function Add markup for making a block editable.
context_reaction_block::editable_region protected function Add markup for making a region editable.
context_reaction_block::editor_form function Context editor form for blocks. 1
context_reaction_block::editor_form_submit function Submit handler context editor form. 1
context_reaction_block::execute function Execute.
context_reaction_block::is_editable_check function Determine if there is an active context editor block, and set a flag. We will set a flag so that we can make sure that blocks with empty content have some default content. This is needed so the save of the context inline editor does not remove the…
context_reaction_block::is_editable_region protected function Determine whether inline editing requirements are met and that the current user may edit.
context_reaction_block::is_enabled_region protected function Return a list of enabled regions for which blocks should be built. Split out into a separate method for easy overrides in extending classes. 1
context_reaction_block::json_decode protected function Compatibility wrapper around json_decode().
context_reaction_block::max_block_weight protected function Generate the safe weight range for a block being added to a region such that there are enough potential unique weights to support all blocks.
context_reaction_block::options_form function Options form. Overrides context_reaction::options_form 1
context_reaction_block::options_form_submit function Options form submit handler. Overrides context_reaction::options_form_submit 1
context_reaction_block::rebuild_needed function Check or set whether a rebuild of the block info cache is needed.
context_reaction_block::render_ajax function Block renderer for AJAX requests. Triggered when $_GET['context_block'] is set. See ->execute() for how this is called.
context_reaction_block::settings_form function Settings form for variables. Overrides context_reaction::settings_form
context_reaction_block::system_region_list protected function Provide caching for system_region_list since it can get called frequently. Evaluate for removal once https://drupal.org/node/1873450 lands or system_region_list is otherwise cached in core
render_cache_hijack_context_reaction_block::block_get_blocks_by_region function Get a renderable array of a region containing all enabled blocks. Overrides context_reaction_block::block_get_blocks_by_region
render_cache_hijack_context_reaction_block::get_blocks function Support the optional aggressive block list caching. Overrides context_reaction_block::get_blocks
render_cache_hijack_context_reaction_block::renderBlockList protected function A caching version of block_list() plus _block_get_renderable_array().
render_cache_hijack_context_reaction_block::renderBlocks protected function