You are here

function display_cache_view_entity in Display Cache 7

Alternative entity view callback.

This callback replacing all view callbacks provided by the Entity module. The original callback will be called within this callback. In this way it is possible to return the cached html before a renderable array is build.

See also

entity_view()

display_cache_entity_info_alter()

1 string reference to 'display_cache_view_entity'
display_cache_entity_info_alter in ./display_cache.module
Implements hook_entity_info_alter().

File

./display_cache.module, line 386
Module file for Display Cache.

Code

function display_cache_view_entity($entities, $view_mode, $langcode, $entity_type) {
  $info = entity_get_info($entity_type);
  $return = array();
  $ignore_cache = strpos($view_mode, DISPLAY_CACHE_VIEW_MODE_CACHE_IGNORE_SUFFIX) !== FALSE;
  if ($ignore_cache) {
    $view_mode = str_replace(':display_cache_no_cache', '', $view_mode);
  }
  foreach ($entities as $entity_id => $entity) {
    $render_array = array();

    // Disable display_cache by global setting.
    if (!$ignore_cache && variable_get('display_cache_disable', FALSE) === FALSE) {
      if (!empty($info['entity keys']['bundle'])) {
        $bundle_key = $info['entity keys']['bundle'];
      }
      elseif (!empty($entity_info['bundle keys']['bundle'])) {
        $bundle_key = $info['bundle keys']['bundle'];
      }

      // This entity may not have a bundle key, fallback to the entity type.
      if (isset($bundle_key) && !empty($entity->{$bundle_key})) {
        $bundle = $entity->{$bundle_key};
      }
      else {
        $bundle = $entity_type;
      }
      $render_array = display_cache_get_cached_display($entity_type, $bundle, $entity_id, $view_mode, 'entity');
    }

    // No cached result was found. Render entity.
    if (empty($render_array)) {

      // Get renderable array.
      if (isset($info['entity view callback'])) {
        $entity = entity_key_array_by_property(array(
          $entity,
        ), $info['entity keys']['id']);
        $render_array = $info['entity view callback']($entity, $view_mode, $langcode, $entity_type);
        $render_array = $render_array[$entity_type][$entity_id];
      }
      elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
        $render_array = entity_get_controller($entity_type)
          ->view(array(
          $entity,
        ), $view_mode, $langcode);
        $render_array = $render_array[$entity_type][$entity_id];
      }
    }
    $return[$entity_type][$entity_id] = $render_array;
  }
  return $return;
}