You are here

class EntityController in Render cache 7.2

EntityController - Provides render caching for entity objects.

Hierarchy

Expanded class hierarchy of EntityController

File

modules/controller/render_cache_entity/src/RenderCache/Controller/EntityController.php, line 16
Contains \Drupal\render_cache_entity\RenderCache\Controller\EntityController

Namespace

Drupal\render_cache_entity\RenderCache\Controller
View source
class EntityController extends BaseRecursionController {

  /**
   * {@inheritdoc}
   */
  protected function isCacheable(array $default_cache_info, array $context) {
    return variable_get('render_cache_' . $this
      ->getPluginId() . '_' . $context['entity_type'] . '_enabled', TRUE) && parent::isCacheable($default_cache_info, $context);
  }

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

    // Helper variables.
    $entity = $object;
    $entity_type = $context['entity_type'];
    $context = parent::getCacheContext($object, $context);

    // Setup entity context.
    list($entity_id, $entity_revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
    $context = $context + array(
      'entity_id' => $entity_id,
      'entity_revision_id' => $entity_revision_id,
      'bundle' => !empty($bundle) ? $bundle : $entity_type,
    );
    return $context;
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheKeys($object, array $context) {
    return array_merge(parent::getCacheKeys($object, $context), array(
      $context['entity_type'],
      $context['view_mode'],
    ));
  }

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

    // Helper variables.
    $entity = $object;

    // Calculate hash to expire cached items automatically.
    $hash = parent::getCacheHash($object, $context);
    $hash['entity_id'] = $context['entity_id'];
    $hash['entity_revision_id'] = $context['entity_revision_id'];
    $hash['bundle'] = $context['bundle'];
    $hash['langcode'] = $context['langcode'];

    // @todo Move to cache tags.
    $hash['modified'] = entity_modified_last($context['entity_type'], $entity);
    return $hash;
  }

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

    // Helper variables.
    $entity_type = $context['entity_type'];
    $entity_id = $context['entity_id'];
    $tags = parent::getCacheTags($object, $context);
    $tags[] = $entity_type . ':' . $entity_id;
    $tags[] = $entity_type . '_view';
    return $tags;
  }

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

    // Helper variables.
    $entities = $objects;
    $view_mode = $this->context['view_mode'];
    $langcode = $this->context['langcode'];
    $entity_type = $this->context['entity_type'];
    $entity_info = entity_get_info($entity_type);

    // If this is a entity view callback.
    if (isset($entity_info['render cache storage']['callback'])) {
      $build = $entity_info['render cache storage']['callback']($entities, $view_mode, $langcode, $entity_type);
    }
    else {
      $page = $this
        ->getPageArgument();
      $build = entity_get_controller($entity_type)
        ->view($entities, $view_mode, $langcode, $page);
    }
    $build = reset($build);
    return $build;
  }

  /**
   * Helper function to retrieve missing $page argument from backtrace.
   *
   * @return bool $page
   *   The page argument from entity_view() if found - NULL otherwise.
   */
  protected function getPageArgument() {
    $page = NULL;

    // We need the $page variable from entity_view() that it does not pass us.
    if (version_compare(PHP_VERSION, '5.4.0', '>=')) {

      // Get only the stack frames we need (PHP 5.4 only).
      $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
    }
    elseif (version_compare(PHP_VERSION, '5.3.6', '>=')) {
      $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
    }
    else {

      // @see http://php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters
      $backtrace = debug_backtrace(TRUE);
    }

    // As a safety, do not grab an unexpected arg for $page, check that this
    // was called from entity_view().
    if (isset($backtrace[2]['function']) && $backtrace[2]['function'] === 'entity_view' && isset($backtrace[2]['args'][4])) {
      $page = $backtrace[2]['args'][4];
    }
    return $page;
  }

}

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::getCacheInfo protected function Specific cache info overrides based on the $object. Overrides AbstractBaseController::getCacheInfo 1
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::getDefaultCacheInfo protected function Provides the cache info for all objects based on the context. Overrides AbstractBaseController::getDefaultCacheInfo 2
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
EntityController::getCacheContext protected function Overrides BaseController::getCacheContext
EntityController::getCacheHash protected function Overrides BaseController::getCacheHash
EntityController::getCacheKeys protected function Overrides BaseController::getCacheKeys
EntityController::getCacheTags protected function Overrides BaseController::getCacheTags
EntityController::getPageArgument protected function Helper function to retrieve missing $page argument from backtrace.
EntityController::isCacheable protected function Overrides BaseController::isCacheable
EntityController::render protected function Render uncached objects. Overrides AbstractBaseController::render