View source
<?php
interface EntityAPIControllerInterface extends DrupalEntityControllerInterface {
public function delete($ids);
public function invoke($hook, $entity);
public function save($entity);
public function create(array $values = array());
public function export($entity, $prefix = '');
public function import($export);
public function buildContent($entity, $view_mode = 'full', $langcode = NULL);
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL);
}
interface EntityAPIControllerRevisionableInterface extends EntityAPIControllerInterface {
public function deleteRevision($revision_id);
}
class EntityAPIController extends DrupalDefaultEntityController implements EntityAPIControllerRevisionableInterface {
protected $cacheComplete = FALSE;
protected $bundleKey;
protected $defaultRevisionKey;
public function __construct($entityType) {
parent::__construct($entityType);
if (isset($this->entityInfo['bundle of'])) {
$info = entity_get_info($this->entityInfo['bundle of']);
$this->bundleKey = $info['bundle keys']['bundle'];
}
$this->defaultRevisionKey = !empty($this->entityInfo['entity keys']['default revision']) ? $this->entityInfo['entity keys']['default revision'] : 'default_revision';
}
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
if ($this->revisionKey) {
$query
->addExpression('CASE WHEN base.' . $this->revisionKey . ' = revision.' . $this->revisionKey . ' THEN 1 ELSE 0 END', $this->defaultRevisionKey);
}
return $query;
}
public function query($ids, $conditions, $revision_id = FALSE) {
$query = $this
->buildQuery($ids, $conditions, $revision_id);
$result = $query
->execute();
if (!empty($this->entityInfo['entity class'])) {
$result
->setFetchMode(PDO::FETCH_CLASS, $this->entityInfo['entity class'], array(
array(),
$this->entityType,
));
}
return $result;
}
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 ($this->cache && !$revision_id) {
$entities = $this
->cacheGet($ids, $conditions);
if ($passed_ids) {
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
}
if (!empty($this->entityInfo['entity cache']) && !$revision_id && $ids && !$conditions) {
$cached_entities = EntityCacheControllerHelper::entityCacheGet($this, $ids, $conditions);
$ids = array_diff($ids, array_keys($cached_entities));
$entities += $cached_entities;
if ($this->cache && !empty($cached_entities) && !$revision_id) {
$this
->cacheSet($cached_entities);
}
}
if (!($this->cacheComplete && $ids === FALSE && !$conditions) && ($ids === FALSE || $ids || $revision_id)) {
$queried_entities = array();
foreach ($this
->query($ids, $conditions, $revision_id) as $record) {
if (isset($entities[$record->{$this->idKey}])) {
continue;
}
if (!empty($this->entityInfo['base table'])) {
$schema = drupal_get_schema($this->entityInfo['base table']);
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['serialize']) && isset($record->{$field})) {
$record->{$field} = unserialize($record->{$field});
if (!empty($info['merge']) && is_array($record->{$field})) {
foreach ($record->{$field} as $key => $value) {
$record->{$key} = $value;
}
unset($record->{$field});
}
}
}
}
$queried_entities[$record->{$this->idKey}] = $record;
}
}
if (!empty($queried_entities)) {
$this
->attachLoad($queried_entities, $revision_id);
$entities += $queried_entities;
}
if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
}
if ($this->cache) {
if (!empty($queried_entities) && !$revision_id) {
$this
->cacheSet($queried_entities);
if (!$conditions && $ids === FALSE) {
$this->cacheComplete = TRUE;
}
}
}
if ($passed_ids && ($passed_ids = array_intersect_key($passed_ids, $entities))) {
foreach ($passed_ids as $id => $value) {
$passed_ids[$id] = $entities[$id];
}
$entities = $passed_ids;
}
return $entities;
}
public function resetCache(array $ids = NULL) {
$this->cacheComplete = FALSE;
parent::resetCache($ids);
if (!empty($this->entityInfo['entity cache'])) {
EntityCacheControllerHelper::resetEntityCache($this, $ids);
}
}
public function invoke($hook, $entity) {
$field_attach_hook = $hook == 'revision_delete' ? 'delete_revision' : $hook;
if (!empty($this->entityInfo['fieldable']) && function_exists($function = 'field_attach_' . $field_attach_hook)) {
$function($this->entityType, $entity);
}
if (!empty($this->entityInfo['bundle of']) && entity_type_is_fieldable($this->entityInfo['bundle of'])) {
$type = $this->entityInfo['bundle of'];
if ($hook == 'insert') {
field_attach_create_bundle($type, $entity->{$this->bundleKey});
}
elseif ($hook == 'delete') {
field_attach_delete_bundle($type, $entity->{$this->bundleKey});
}
elseif ($hook == 'update' && $entity->original->{$this->bundleKey} != $entity->{$this->bundleKey}) {
field_attach_rename_bundle($type, $entity->original->{$this->bundleKey}, $entity->{$this->bundleKey});
}
}
module_invoke_all($this->entityType . '_' . $hook, $entity);
if ($hook == 'presave' || $hook == 'insert' || $hook == 'update' || $hook == 'delete') {
module_invoke_all('entity_' . $hook, $entity, $this->entityType);
}
if (module_exists('rules')) {
rules_invoke_event($this->entityType . '_' . $hook, $entity);
}
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$entities = $ids ? $this
->load($ids) : FALSE;
if (!$entities) {
return;
}
$transaction = isset($transaction) ? $transaction : db_transaction();
try {
$ids = array_keys($entities);
db_delete($this->entityInfo['base table'])
->condition($this->idKey, $ids, 'IN')
->execute();
if (isset($this->revisionTable)) {
db_delete($this->revisionTable)
->condition($this->idKey, $ids, 'IN')
->execute();
}
$this
->resetCache($ids);
foreach ($entities as $id => $entity) {
$this
->invoke('delete', $entity);
}
db_ignore_slave();
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
throw $e;
}
}
public function deleteRevision($revision_id) {
if ($entity_revision = entity_revision_load($this->entityType, $revision_id)) {
if (entity_revision_is_default($this->entityType, $entity_revision)) {
return FALSE;
}
db_delete($this->revisionTable)
->condition($this->revisionKey, $revision_id)
->execute();
$this
->invoke('revision_delete', $entity_revision);
return TRUE;
}
return FALSE;
}
public function save($entity, DatabaseTransaction $transaction = NULL) {
$transaction = isset($transaction) ? $transaction : db_transaction();
try {
if (!empty($entity->{$this->idKey}) && !isset($entity->original)) {
$entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
}
$entity->is_new = !empty($entity->is_new) || empty($entity->{$this->idKey});
$this
->invoke('presave', $entity);
if ($entity->is_new) {
$return = drupal_write_record($this->entityInfo['base table'], $entity);
if ($this->revisionKey) {
$this
->saveRevision($entity);
}
$this
->invoke('insert', $entity);
}
else {
if (!$this->revisionKey || !empty($entity->{$this->defaultRevisionKey})) {
$return = drupal_write_record($this->entityInfo['base table'], $entity, $this->idKey);
}
if ($this->revisionKey) {
$return = $this
->saveRevision($entity);
}
$this
->resetCache(array(
$entity->{$this->idKey},
));
$this
->invoke('update', $entity);
if ($this->revisionKey && !$entity->{$this->defaultRevisionKey} && !empty($this->entityInfo['fieldable'])) {
field_attach_update($this->entityType, $entity->original);
}
}
db_ignore_slave();
unset($entity->is_new);
unset($entity->is_new_revision);
unset($entity->original);
return $return;
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
throw $e;
}
}
protected function saveRevision($entity) {
$record = (array) $entity;
$entity->is_new_revision = !empty($entity->is_new_revision) || !empty($entity->revision) || $entity->is_new;
$entity->revision =& $entity->is_new_revision;
$entity->{$this->defaultRevisionKey} = !empty($entity->{$this->defaultRevisionKey}) || $entity->is_new;
if ($entity->is_new_revision && isset($record[$this->revisionKey])) {
$record[$this->revisionKey] = NULL;
}
if ($entity->is_new_revision) {
drupal_write_record($this->revisionTable, $record);
$update_default_revision = $entity->{$this->defaultRevisionKey};
}
else {
drupal_write_record($this->revisionTable, $record, $this->revisionKey);
$update_default_revision = $entity->{$this->defaultRevisionKey} && $entity->{$this->revisionKey} != $entity->original->{$this->revisionKey};
}
$entity->{$this->revisionKey} = $record[$this->revisionKey];
if ($update_default_revision) {
db_update($this->entityInfo['base table'])
->fields(array(
$this->revisionKey => $record[$this->revisionKey],
))
->condition($this->idKey, $entity->{$this->idKey})
->execute();
}
return $entity->is_new_revision ? SAVED_NEW : SAVED_UPDATED;
}
public function create(array $values = array()) {
$values += array(
'is_new' => TRUE,
);
if (isset($this->entityInfo['entity class']) && ($class = $this->entityInfo['entity class'])) {
return new $class($values, $this->entityType);
}
return (object) $values;
}
public function export($entity, $prefix = '') {
$vars = get_object_vars($entity);
unset($vars['is_new']);
return entity_var_json_export($vars, $prefix);
}
public function import($export) {
$vars = drupal_json_decode($export);
if (is_array($vars)) {
return $this
->create($vars);
}
return FALSE;
}
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$entity->content = $content;
$langcode = isset($langcode) ? $langcode : $GLOBALS['language_content']->language;
$context = array(
'entity_type' => $this->entityType,
'entity' => $entity,
'langcode' => $langcode,
);
drupal_alter('entity_view_mode', $view_mode, $context);
$entity->content += array(
'#view_mode' => $view_mode,
);
if ($extra_field_controller = entity_get_extra_fields_controller($this->entityType)) {
$wrapper = entity_metadata_wrapper($this->entityType, $entity);
$extra = $extra_field_controller
->fieldExtraFields();
$type_extra =& $extra[$this->entityType][$this->entityType]['display'];
$bundle_extra =& $extra[$this->entityType][$wrapper
->getBundle()]['display'];
foreach ($wrapper as $name => $property) {
if (isset($type_extra[$name]) || isset($bundle_extra[$name])) {
$this
->renderEntityProperty($wrapper, $name, $property, $view_mode, $langcode, $entity->content);
}
}
}
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);
}
if (module_exists('rules')) {
rules_invoke_all($this->entityType . '_view', $entity, $view_mode, $langcode);
}
else {
module_invoke_all($this->entityType . '_view', $entity, $view_mode, $langcode);
}
module_invoke_all('entity_view', $entity, $this->entityType, $view_mode, $langcode);
$build = $entity->content;
unset($entity->content);
return $build;
}
protected function renderEntityProperty($wrapper, $name, $property, $view_mode, $langcode, &$content) {
$info = $property
->info();
$content[$name] = array(
'#label_hidden' => FALSE,
'#label' => $info['label'],
'#entity_wrapped' => $wrapper,
'#theme' => 'entity_property',
'#property_name' => $name,
'#access' => $property
->access('view'),
'#entity_type' => $this->entityType,
);
$content['#attached']['css']['entity.theme'] = drupal_get_path('module', 'entity') . '/theme/entity.theme.css';
}
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$entities = entity_key_array_by_property($entities, $this->idKey);
if (!empty($this->entityInfo['fieldable'])) {
field_attach_prepare_view($this->entityType, $entities, $view_mode);
}
entity_prepare_view($this->entityType, $entities);
$langcode = isset($langcode) ? $langcode : $GLOBALS['language_content']->language;
$view = array();
foreach ($entities as $entity) {
$build = entity_build_content($this->entityType, $entity, $view_mode, $langcode);
$build += array(
'#theme' => 'entity',
'#entity_type' => $this->entityType,
'#entity' => $entity,
'#view_mode' => $view_mode,
'#language' => $langcode,
'#page' => $page,
);
drupal_alter(array(
$this->entityType . '_view',
'entity_view',
), $build, $this->entityType);
$key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
$view[$this->entityType][$key] = $build;
}
return $view;
}
}
class EntityAPIControllerExportable extends EntityAPIController {
protected $entityCacheByName = array();
protected $nameKey, $statusKey, $moduleKey;
public function __construct($entityType) {
parent::__construct($entityType);
$this->nameKey = isset($this->entityInfo['entity keys']['name']) ? $this->entityInfo['entity keys']['name'] : $this->idKey;
if (!empty($this->entityInfo['exportable'])) {
$this->statusKey = isset($this->entityInfo['entity keys']['status']) ? $this->entityInfo['entity keys']['status'] : 'status';
$this->moduleKey = isset($this->entityInfo['entity keys']['module']) ? $this->entityInfo['entity keys']['module'] : 'module';
}
}
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery(array(), $conditions, $revision_id);
if ($ids) {
$key = is_numeric(reset($ids)) ? $this->idKey : $this->nameKey;
$query
->condition("base.{$key}", $ids, 'IN');
}
return $query;
}
public function load($ids = array(), $conditions = array()) {
$entities = array();
if (!$ids || $this->nameKey == $this->idKey || is_numeric(reset($ids))) {
return parent::load($ids, $conditions);
}
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 ($this->cache && !$revision_id) {
$entities = $this
->cacheGetByName($ids, $conditions);
}
if ($entities) {
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
$entities_by_id = parent::load($ids, $conditions);
$entities += entity_key_array_by_property($entities_by_id, $this->nameKey);
$return = array();
foreach ($passed_ids as $name => $value) {
if (isset($entities[$name])) {
$return[$entities[$name]->{$this->idKey}] = $entities[$name];
}
}
return $return;
}
protected function cacheGet($ids, $conditions = array()) {
if (!empty($this->entityCache) && $ids !== array()) {
$entities = $ids ? array_intersect_key($this->entityCache, array_flip($ids)) : $this->entityCache;
return $this
->applyConditions($entities, $conditions);
}
return array();
}
protected function cacheGetByName($names, $conditions = array()) {
if (!empty($this->entityCacheByName) && $names !== array() && $names) {
$entities = array_intersect_key($this->entityCacheByName, array_flip($names));
return $this
->applyConditions($entities, $conditions);
}
return array();
}
protected function applyConditions($entities, $conditions = array()) {
if ($conditions) {
foreach ($entities as $key => $entity) {
$entity_values = (array) $entity;
foreach ($conditions as $condition_key => $condition_value) {
if (is_array($condition_value)) {
if (!isset($entity_values[$condition_key]) || !in_array($entity_values[$condition_key], $condition_value)) {
unset($entities[$key]);
}
}
elseif (!isset($entity_values[$condition_key]) || $entity_values[$condition_key] != $condition_value) {
unset($entities[$key]);
}
}
}
}
return $entities;
}
protected function cacheSet($entities) {
$this->entityCache += $entities;
if ($this->nameKey != $this->idKey) {
$this->entityCacheByName += entity_key_array_by_property($entities, $this->nameKey);
}
}
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);
}
if ($this->nameKey != $this->idKey) {
$entities_by_name = entity_key_array_by_property($queried_entities, $this->nameKey);
}
else {
$entities_by_name = $queried_entities;
}
$args = array_merge(array(
$entities_by_name,
), $this->hookLoadArguments);
foreach (module_implements($this->entityInfo['load hook']) as $module) {
call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
}
}
public function resetCache(array $ids = NULL) {
$this->cacheComplete = FALSE;
if (isset($ids)) {
foreach (array_intersect_key($this->entityCache, array_flip($ids)) as $id => $entity) {
unset($this->entityCacheByName[$this->entityCache[$id]->{$this->nameKey}]);
unset($this->entityCache[$id]);
}
}
else {
$this->entityCache = array();
$this->entityCacheByName = array();
}
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$entities = $ids ? $this
->load($ids) : FALSE;
if ($entities) {
parent::delete($ids, $transaction);
foreach ($entities as $id => $entity) {
if (entity_has_status($this->entityType, $entity, ENTITY_IN_CODE)) {
entity_defaults_rebuild(array(
$this->entityType,
));
break;
}
}
}
}
public function invoke($hook, $entity) {
if ($hook == 'delete') {
if (entity_has_status($this->entityType, $entity, ENTITY_IN_CODE) && !module_exists($entity->{$this->moduleKey})) {
$entity->{$this->statusKey} = ENTITY_CUSTOM;
}
$is_revert = entity_has_status($this->entityType, $entity, ENTITY_IN_CODE);
}
if (!empty($this->entityInfo['fieldable']) && function_exists($function = 'field_attach_' . $hook)) {
$function($this->entityType, $entity);
}
if (isset($this->entityInfo['bundle of']) && ($type = $this->entityInfo['bundle of'])) {
if ($hook == 'insert') {
field_attach_create_bundle($type, $entity->{$this->bundleKey});
}
elseif ($hook == 'delete' && !$is_revert) {
field_attach_delete_bundle($type, $entity->{$this->bundleKey});
}
elseif ($hook == 'update' && ($id = $entity->{$this->nameKey})) {
if ($entity->original->{$this->bundleKey} != $entity->{$this->bundleKey}) {
field_attach_rename_bundle($type, $entity->original->{$this->bundleKey}, $entity->{$this->bundleKey});
}
}
}
module_invoke_all($this->entityType . '_' . $hook, $entity);
if ($hook == 'presave' || $hook == 'insert' || $hook == 'update' || $hook == 'delete') {
module_invoke_all('entity_' . $hook, $entity, $this->entityType);
}
}
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (!empty($entity->{$this->nameKey}) && empty($entity->{$this->idKey}) && !isset($entity->original)) {
$entity->original = entity_load_unchanged($this->entityType, $entity->{$this->nameKey});
}
if (entity_has_status($this->entityType, $entity, ENTITY_IN_CODE) && empty($entity->is_rebuild)) {
$entity->{$this->statusKey} |= ENTITY_CUSTOM;
}
return parent::save($entity, $transaction);
}
public function export($entity, $prefix = '') {
$vars = get_object_vars($entity);
unset($vars[$this->statusKey], $vars[$this->moduleKey], $vars['is_new']);
if ($this->nameKey != $this->idKey) {
unset($vars[$this->idKey]);
}
return entity_var_json_export($vars, $prefix);
}
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$view = parent::view($entities, $view_mode, $langcode, $page);
if ($this->nameKey != $this->idKey) {
$return = array();
foreach ($view[$this->entityType] as $id => $content) {
$key = isset($content['#entity']->{$this->nameKey}) ? $content['#entity']->{$this->nameKey} : NULL;
$return[$this->entityType][$key] = $content;
}
$view = $return;
}
return $view;
}
}