class ConfigStorageController extends EntityStorageControllerBase {
protected $uuidKey = 'uuid';
protected $statusKey = 'status';
protected $configFactory;
protected $configStorage;
public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage) {
parent::__construct($entity_type, $entity_info);
$this->idKey = $this->entityInfo['entity_keys']['id'];
if (isset($this->entityInfo['entity_keys']['status'])) {
$this->statusKey = $this->entityInfo['entity_keys']['status'];
}
else {
$this->statusKey = FALSE;
}
$this->configFactory = $config_factory;
$this->configStorage = $config_storage;
}
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static($entity_type, $entity_info, $container
->get('config.factory'), $container
->get('config.storage'));
}
public function load(array $ids = NULL) {
$entities = array();
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
if ($ids === NULL || $ids) {
$queried_entities = $this
->buildQuery($ids);
}
if (!empty($queried_entities)) {
$this
->attachLoad($queried_entities);
$entities += $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;
}
public function loadRevision($revision_id) {
return FALSE;
}
public function deleteRevision($revision_id) {
return NULL;
}
public function loadByProperties(array $values = array()) {
$entities = $this
->load();
foreach ($values as $key => $value) {
$entities = array_filter($entities, function ($entity) use ($key, $value) {
return $value === $entity
->get($key);
});
}
return $entities;
}
public function getConfigPrefix() {
return $this->entityInfo['config_prefix'] . '.';
}
public static function getIDFromConfigName($config_name, $config_prefix) {
return substr($config_name, strlen($config_prefix . '.'));
}
protected function buildQuery($ids, $revision_id = FALSE) {
$config_class = $this->entityInfo['class'];
$prefix = $this
->getConfigPrefix();
if ($ids === NULL) {
$names = $this->configStorage
->listAll($prefix);
$result = array();
foreach ($names as $name) {
$config = $this->configFactory
->get($name);
$result[$config
->get($this->idKey)] = new $config_class($config
->get(), $this->entityType);
}
return $result;
}
else {
$result = array();
foreach ($ids as $id) {
$config = $this->configFactory
->get($prefix . $id);
if (!$config
->isNew()) {
$result[$id] = new $config_class($config
->get(), $this->entityType);
}
}
return $result;
}
}
protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
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->entityType . '_load') as $module) {
call_user_func_array($module . '_' . $this->entityType . '_load', $args);
}
}
public function create(array $values) {
$class = $this->entityInfo['class'];
$values += array(
'langcode' => language_default()->langcode,
);
$entity = new $class($values, $this->entityType);
$entity
->enforceIsNew();
if (!isset($entity->{$this->uuidKey})) {
$uuid = new Uuid();
$entity->{$this->uuidKey} = $uuid
->generate();
}
$this
->invokeHook('create', $entity);
if (!empty($this->statusKey) && !isset($entity->{$this->statusKey})) {
$entity->{$this->statusKey} = TRUE;
}
return $entity;
}
public function delete(array $entities) {
if (!$entities) {
return;
}
$this
->preDelete($entities);
foreach ($entities as $id => $entity) {
$this
->invokeHook('predelete', $entity);
}
foreach ($entities as $id => $entity) {
$config = $this->configFactory
->get($this
->getConfigPrefix() . $entity
->id());
$config
->delete();
}
$this
->postDelete($entities);
foreach ($entities as $id => $entity) {
$this
->invokeHook('delete', $entity);
}
}
public function save(EntityInterface $entity) {
$prefix = $this
->getConfigPrefix();
$id = $entity
->id();
if ($id === NULL || $id === '') {
throw new EntityMalformedException('The entity does not have an ID.');
}
if ($entity
->getOriginalID() !== NULL) {
$id = $entity
->getOriginalID();
}
$config = $this->configFactory
->get($prefix . $id);
$is_new = $config
->isNew();
if (!$is_new && !isset($entity->original)) {
$this
->resetCache(array(
$id,
));
$result = $this
->load(array(
$id,
));
$entity->original = reset($result);
}
if ($id !== $entity
->id()) {
$this->configFactory
->rename($prefix . $id, $prefix . $entity
->id());
}
$this
->preSave($entity);
$this
->invokeHook('presave', $entity);
foreach ($entity
->getExportProperties() as $key => $value) {
$config
->set($key, $value);
}
if (!$is_new) {
$return = SAVED_UPDATED;
$config
->save();
$this
->postSave($entity, TRUE);
$this
->invokeHook('update', $entity);
$entity
->setOriginalID($entity
->id());
}
else {
$return = SAVED_NEW;
$config
->save();
$entity
->enforceIsNew(FALSE);
$this
->postSave($entity, FALSE);
$this
->invokeHook('insert', $entity);
}
unset($entity->original);
return $return;
}
protected function preSave(EntityInterface $entity) {
}
protected function postSave(EntityInterface $entity, $update) {
}
protected function preDelete($entities) {
}
protected function postDelete($entities) {
}
public function getFieldDefinitions(array $constraints) {
return array();
}
protected function invokeHook($hook, EntityInterface $entity) {
module_invoke_all($this->entityType . '_' . $hook, $entity);
module_invoke_all('entity_' . $hook, $entity, $this->entityType);
}
public function getQueryServicename() {
return 'entity.query.config';
}
public function importCreate($name, Config $new_config, Config $old_config) {
$entity = $this
->create($new_config
->get());
$entity
->save();
return TRUE;
}
public function importUpdate($name, Config $new_config, Config $old_config) {
$id = static::getIDFromConfigName($name, $this->entityInfo['config_prefix']);
$entities = $this
->load(array(
$id,
));
$entity = $entities[$id];
$entity->original = clone $entity;
foreach ($old_config
->get() as $property => $value) {
$entity->original
->set($property, $value);
}
foreach ($new_config
->get() as $property => $value) {
$entity
->set($property, $value);
}
$entity
->save();
return TRUE;
}
public function importDelete($name, Config $new_config, Config $old_config) {
$id = static::getIDFromConfigName($name, $this->entityInfo['config_prefix']);
$entities = $this
->load(array(
$id,
));
$entity = $entities[$id];
$entity
->delete();
return TRUE;
}
}