class DefaultEntityController implements EntityControllerInterface {
protected $entityCache;
protected $entityType;
protected $entityInfo;
protected $hookLoadArguments;
protected $idKey;
protected $revisionKey;
protected $revisionTable;
protected $persistentCache;
protected $staticCache;
public function __construct($entityType) {
$this->entityType = $entityType;
$this->entityInfo = entity_get_info($entityType);
$this->entityCache = array();
$this->hookLoadArguments = array();
$this->idKey = $this->entityInfo['entity keys']['id'];
if (!empty($this->entityInfo['entity keys']['revision'])) {
$this->revisionKey = $this->entityInfo['entity keys']['revision'];
$this->revisionTable = $this->entityInfo['revision table'];
}
else {
$this->revisionKey = FALSE;
}
$this->staticCache = !empty($this->entityInfo['static cache']);
if (isset($this->entityInfo['entity cache'])
&& $this->entityInfo['entity cache']
&& db_table_exists('cache_entity_' . $this->entityType)) {
$this->persistentCache = TRUE;
}
else {
$this->persistentCache = FALSE;
}
}
public function resetCache(array $ids = NULL) {
if ($this->persistentCache) {
if (!empty($ids)) {
cache_clear_all($ids, 'cache_entity_' . $this->entityType);
}
else {
cache_flush('cache_entity_' . $this->entityType);
}
}
$this->resetStaticCache($ids);
}
public function resetStaticCache(array $ids = NULL) {
if (isset($ids)) {
foreach ($ids as $id) {
unset($this->entityCache[$id]);
}
}
else {
$this->entityCache = array();
}
}
public function load($ids = array(), $conditions = array()) {
$entities = array();
if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
$revision_id = $conditions[$this->revisionKey];
unset($conditions[$this->revisionKey]);
}
else {
$revision_id = FALSE;
}
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
if ($conditions && !$revision_id) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $this->entityType);
foreach ($conditions as $property_name => $condition) {
$query->propertyCondition($property_name, $condition);
}
if ($passed_ids) {
$query->propertyCondition($this->idKey, array_keys($passed_ids));
}
$result = $query->execute();
if (isset($result[$this->entityType])) {
$entity_ids = array_keys($result[$this->entityType]);
return $this->load($entity_ids);
}
else {
return array();
}
}
if ($this->staticCache && !$revision_id) {
$entities += $this->cacheGet($ids, $conditions);
if ($passed_ids) {
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
}
if ($this->persistentCache && !$revision_id && $ids && !$conditions) {
$cached_entities = array();
if ($ids && !$conditions) {
$cached = cache_get_multiple($ids, 'cache_entity_' . $this->entityType);
if ($cached) {
foreach ($cached as $item) {
$cached_entities[$item->cid] = $item->data;
}
}
}
$entities += $cached_entities;
$ids = array_diff($ids, array_keys($cached_entities));
if ($this->staticCache) {
if (!empty($cached_entities) && !$revision_id) {
$this->cacheSet($cached_entities);
}
}
}
if ($ids === FALSE || $ids || $revision_id || ($conditions && !$passed_ids)) {
$query_result = $this->buildQuery($ids, $conditions, $revision_id)->execute();
if (!empty($this->entityInfo['entity class'])) {
$query_result->setFetchMode(PDO::FETCH_CLASS, $this->entityInfo['entity class'], array(array(), $this->entityType));
}
$queried_entities = $query_result->fetchAllAssoc($this->idKey);
}
if (!empty($queried_entities)) {
$this->attachLoad($queried_entities, $revision_id);
$entities += $queried_entities;
}
if ($this->persistentCache && !empty($queried_entities) && !$revision_id) {
if ($passed_ids) {
$queried_entities_by_id = array_intersect_key($queried_entities, $passed_ids);
if (!empty($queried_entities_by_id)) {
foreach ($queried_entities_by_id as $id => $item) {
$bundle_name = !empty($item->bundle()) ? $item->bundle() : $this->entityType;
$bundle_cache = isset($this->entityInfo['bundles'][$bundle_name]['bundle cache']) ? $this->entityInfo['bundles'][$bundle_name]['bundle cache'] : TRUE;
if ($bundle_cache) {
cache_set($id, $item, 'cache_entity_' . $this->entityType);
}
}
}
}
}
if ($this->staticCache) {
if (!empty($queried_entities) && !$revision_id) {
$this->cacheSet($queried_entities);
}
}
if ($passed_ids) {
$passed_ids = array_intersect_key($passed_ids, $entities);
foreach ($entities as $entity) {
$passed_ids[$entity->{$this->idKey}] = $entity;
}
$entities = $passed_ids;
}
return $entities;
}
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = db_select($this->entityInfo['base table'], 'base');
$query->addTag($this->entityType . '_load_multiple');
if ($revision_id) {
$query->join($this->revisionTable, 'revision', "revision.{$this->idKey} = base.{$this->idKey} AND revision.{$this->revisionKey} = :revisionId", array(':revisionId' => $revision_id));
}
elseif ($this->revisionKey) {
$query->join($this->revisionTable, 'revision', "revision.{$this->revisionKey} = base.{$this->revisionKey}");
}
$entity_fields = $this->entityInfo['schema_fields_sql']['base table'];
if ($this->revisionKey) {
$entity_revision_fields = backdrop_map_assoc($this->entityInfo['schema_fields_sql']['revision table']);
unset($entity_revision_fields[$this->idKey]);
$entity_field_keys = array_flip($entity_fields);
foreach ($entity_revision_fields as $key => $name) {
if (isset($entity_field_keys[$name])) {
unset($entity_fields[$entity_field_keys[$name]]);
}
}
$query->fields('revision', $entity_revision_fields);
$query->addExpression('base.' . $this->revisionKey . ' = revision.' . $this->revisionKey, 'is_active_revision');
}
$query->fields('base', $entity_fields);
if ($ids) {
$query->condition("base.{$this->idKey}", $ids, 'IN');
}
if ($conditions) {
foreach ($conditions as $field => $value) {
$query->condition('base.' . $field, $value);
}
}
return $query;
}
protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
if ($this->entityInfo['fieldable']) {
if ($revision_id) {
field_attach_load_revision($this->entityType, $queried_entities);
}
else {
field_attach_load($this->entityType, $queried_entities);
}
}
foreach (module_implements('entity_load') as $module) {
$function = $module . '_entity_load';
$function($queried_entities, $this->entityType);
}
$args = array_merge(array($queried_entities), $this->hookLoadArguments);
foreach (module_implements($this->entityInfo['load hook']) as $module) {
call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
}
}
protected function cacheGet($ids, $conditions = array()) {
$entities = array();
if (!empty($this->entityCache)) {
if ($ids) {
$entities += array_intersect_key($this->entityCache, array_flip($ids));
}
elseif ($conditions) {
$entities = $this->entityCache;
}
}
if ($conditions) {
foreach ($entities as $entity) {
foreach ($conditions as $property_name => $condition) {
if (is_array($condition)) {
if (!in_array($entity->{$property_name}, $condition)) {
unset($entities[$entity->{$this->idKey}]);
continue 2;
}
}
elseif ($condition != $entity->{$property_name}) {
unset($entities[$entity->{$this->idKey}]);
continue 2;
}
}
}
}
return $entities;
}
protected function cacheSet($entities) {
$this->entityCache += $entities;
}
public function buildContent(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
global $language_content;
$langcode = $langcode ? $langcode : $language_content->langcode;
$entity->content = array();
$view_mode = key(entity_view_mode_prepare($entity->entityType(), array($entity->id() => $entity), $view_mode, $langcode));
$entity->content += array('#view_mode' => $view_mode);
if (!empty($this->entityInfo['fieldable'])) {
$key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
field_attach_prepare_view($this->entityType, array($key => $entity), $view_mode);
$entity->content += field_attach_view($this->entityType, $entity, $view_mode, $langcode);
}
module_invoke_all($this->entityType . '_view', $entity, $view_mode, $langcode);
module_invoke_all('entity_view', $entity, $this->entityType, $view_mode, $langcode);
return $entity->content;
}
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
global $language_content;
$langcode = $langcode ? $langcode : $language_content->langcode;
if (!empty($this->entityInfo['fieldable'])) {
field_attach_prepare_view($this->entityType, $entities, $view_mode);
}
entity_prepare_view($this->entityType, $entities);
$view = array();
foreach ($entities as $entity) {
$this->buildContent($entity, $view_mode, $langcode);
$build = $entity->content;
unset($entity->content);
$build += array(
'#theme' => 'entity',
'#entity_type' => $this->entityType,
'#entity' => $entity,
'#view_mode' => $view_mode,
'#language' => $langcode,
'#page' => $page,
);
backdrop_alter(array($this->entityType . '_view', 'entity_view'), $build, $this->entityType);
$view[$this->entityType][$entity->id()] = $build;
}
return $view;
}
}