KeyValueEntityStorage.php in Zircon Profile 8.0
File
core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php
View source
<?php
namespace Drupal\Core\Entity\KeyValueStore;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class KeyValueEntityStorage extends EntityStorageBase {
const MAX_ID_LENGTH = 128;
protected $keyValueStore;
protected $uuidService;
protected $languageManager;
public function __construct(EntityTypeInterface $entity_type, KeyValueStoreInterface $key_value_store, UuidInterface $uuid_service, LanguageManagerInterface $language_manager) {
parent::__construct($entity_type);
$this->keyValueStore = $key_value_store;
$this->uuidService = $uuid_service;
$this->languageManager = $language_manager;
$this->uuidKey = $this->entityType
->getKey('uuid');
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('keyvalue')
->get('entity_storage__' . $entity_type
->id()), $container
->get('uuid'), $container
->get('language_manager'));
}
public function doCreate(array $values = array()) {
$values += array(
$this
->getEntityType()
->getKey('langcode') => $this->languageManager
->getDefaultLanguage()
->getId(),
);
$entity = new $this->entityClass($values, $this->entityTypeId);
if ($entity instanceof FieldableEntityInterface) {
foreach ($entity as $name => $field) {
if (isset($values[$name])) {
$entity->{$name} = $values[$name];
}
elseif (!array_key_exists($name, $values)) {
$entity
->get($name)
->applyDefaultValue();
}
unset($values[$name]);
}
}
return $entity;
}
public function doLoadMultiple(array $ids = NULL) {
if (empty($ids)) {
$entities = $this->keyValueStore
->getAll();
}
else {
$entities = $this->keyValueStore
->getMultiple($ids);
}
return $this
->mapFromStorageRecords($entities);
}
public function loadRevision($revision_id) {
return NULL;
}
public function deleteRevision($revision_id) {
return NULL;
}
public function doDelete($entities) {
$entity_ids = array_keys($entities);
$this->keyValueStore
->deleteMultiple($entity_ids);
}
public function save(EntityInterface $entity) {
$id = $entity
->id();
if ($id === NULL || $id === '') {
throw new EntityMalformedException('The entity does not have an ID.');
}
if (strlen($entity
->id()) > static::MAX_ID_LENGTH) {
throw new ConfigEntityIdLengthException("Entity ID {$entity->id()} exceeds maximum allowed length of " . static::MAX_ID_LENGTH . ' characters.');
}
return parent::save($entity);
}
protected function doSave($id, EntityInterface $entity) {
$is_new = $entity
->isNew();
$this->keyValueStore
->set($entity
->id(), $entity
->toArray());
if ($this
->has($id, $entity) && $id !== $entity
->id()) {
$this->keyValueStore
->delete($id);
}
return $is_new ? SAVED_NEW : SAVED_UPDATED;
}
protected function has($id, EntityInterface $entity) {
return $this->keyValueStore
->has($id);
}
protected function getQueryServiceName() {
return 'entity.query.keyvalue';
}
}