View source
<?php
namespace Drupal\render_cache_entity\RenderCache\Controller;
use Drupal\render_cache\RenderCache\Controller\BaseRecursionController;
class EntityController extends BaseRecursionController {
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);
}
protected function getCacheContext($object, array $context) {
$entity = $object;
$entity_type = $context['entity_type'];
$context = parent::getCacheContext($object, $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;
}
protected function getCacheKeys($object, array $context) {
return array_merge(parent::getCacheKeys($object, $context), array(
$context['entity_type'],
$context['view_mode'],
));
}
protected function getCacheHash($object, array $context) {
$entity = $object;
$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'];
$hash['modified'] = entity_modified_last($context['entity_type'], $entity);
return $hash;
}
protected function getCacheTags($object, array $context) {
$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;
}
protected function render(array $objects) {
$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 (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;
}
protected function getPageArgument() {
$page = NULL;
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
}
elseif (version_compare(PHP_VERSION, '5.3.6', '>=')) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
}
else {
$backtrace = debug_backtrace(TRUE);
}
if (isset($backtrace[2]['function']) && $backtrace[2]['function'] === 'entity_view' && isset($backtrace[2]['args'][4])) {
$page = $backtrace[2]['args'][4];
}
return $page;
}
}