You are here

class BlockController in Render cache 7.2

BlockController - Provides render caching for block objects.

Hierarchy

Expanded class hierarchy of BlockController

File

modules/controller/render_cache_block/src/RenderCache/Controller/BlockController.php, line 16
Contains \Drupal\render_cache_block\RenderCache\Controller\BlockController

Namespace

Drupal\render_cache_block\RenderCache\Controller
View source
class BlockController extends BaseRecursionController {

  /**
   * {@inheritdoc}
   */
  protected function isCacheable(array $default_cache_info, array $context) {

    // Disabled caching for now.
    return variable_get('render_cache_' . $this
      ->getPluginId() . '_' . $context['region'] . '_enabled', TRUE) && parent::isCacheable($default_cache_info, $context);
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheContext($object, array $context) {

    // Helper variables.
    $block = $object;
    $context = parent::getCacheContext($object, $context);
    $context = $context + array(
      'bid' => $block->bid,
      'delta' => $block->delta,
      'module' => $block->module,
    );
    return $context;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultCacheInfo($context) {
    $default_cache_info = parent::getDefaultCacheInfo($context);

    // The block cache renders to markup by default.
    $default_cache_info['render_cache_render_to_markup'] = TRUE;
    return $default_cache_info;
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheInfo($object, array $context) {

    // Helper variables.
    $block = $object;
    $cache_info = parent::getCacheInfo($object, $context);

    // Overwrite the default granularity, this is a pretty sane default.
    $cache_info['granularity'] = isset($block->cache) ? $block->cache : DRUPAL_NO_CACHE;
    return $cache_info;
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheKeys($object, array $context) {

    // Helper variables.
    // @todo Unused variable $block
    $block = $object;
    return array_merge(parent::getCacheKeys($object, $context), array(
      $context['region'],
      $context['module'],
      $context['delta'],
    ));
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheHash($object, array $context) {
    $hash['module'] = $context['module'];
    $hash['delta'] = $context['delta'];

    // Context module support.
    if (!empty($context['context']->name)) {
      $hash['context'] = $context['context']->name;
    }
    return $hash;
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheTags($object, array $context) {
    $tags = parent::getCacheTags($object, $context);
    $tags[] = 'block:' . $context['id'];
    return $tags;
  }

  /**
   * {@inheritdoc}
   */
  protected function render(array $objects) {

    // Helper variables.
    $blocks = $objects;

    // Ensure block module is loaded.
    module_load_include('module', 'block', 'block');

    // Turn off core block caching.
    foreach ($blocks as $block) {
      $block->cache = DRUPAL_NO_CACHE;
    }

    // This works because _block_render_blocks uses the same format for the
    // $id of the block in the array.
    $list = _block_render_blocks($blocks);
    $build = array();
    if ($list) {
      $build = _block_get_renderable_array($list);
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseController::$cache protected property The injected cache backend adapter.
BaseController::$context protected property An optional context provided by this controller.
BaseController::$renderStack protected property The injected render stack.
BaseController::alter protected function Overrides AbstractBaseController::alter
BaseController::determineCachingStrategy protected function Determines the caching strategy for a given cache info structure.
BaseController::getCacheIdInfo protected function Provides the fully pouplated cache information for a specific object. Overrides AbstractBaseController::getCacheIdInfo
BaseController::getCacheInfoMap protected function Returns the cache information map for the given objects.
BaseController::getCacheValidate protected function Overrides AbstractBaseController::getCacheValidate
BaseController::getContext public function Overrides ControllerInterface::getContext
BaseController::getPlaceholders protected function Get the placeholders from the cache information map.
BaseController::renderPlaceholders public static function Overrides ControllerInterface::renderPlaceholders
BaseController::setContext public function Overrides ControllerInterface::setContext
BaseController::view public function Overrides ControllerInterface::view 1
BaseController::viewPlaceholders public function Overrides ControllerInterface::viewPlaceholders
BaseController::__construct public function Constructs a controller plugin object.
BaseRecursionController::recursionStep public function Ensures that recursion storage is added to the right cached object. Overrides RecursionControllerInterface::recursionStep
BaseRecursionController::renderRecursive protected function Renders uncached objects in a recursion compatible way. Overrides BaseController::renderRecursive
BlockController::getCacheContext protected function Overrides BaseController::getCacheContext
BlockController::getCacheHash protected function Overrides BaseController::getCacheHash
BlockController::getCacheInfo protected function Specific cache info overrides based on the $object. Overrides BaseController::getCacheInfo
BlockController::getCacheKeys protected function Overrides BaseController::getCacheKeys
BlockController::getCacheTags protected function Overrides BaseController::getCacheTags
BlockController::getDefaultCacheInfo protected function Provides the cache info for all objects based on the context. Overrides BaseController::getDefaultCacheInfo
BlockController::isCacheable protected function Overrides BaseController::isCacheable
BlockController::render protected function Render uncached objects. Overrides AbstractBaseController::render