KeyValueEntityStorage.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Drupal\Core\Entity\KeyValueStore;
  3. use Drupal\Component\Uuid\UuidInterface;
  4. use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
  5. use Drupal\Core\Entity\FieldableEntityInterface;
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Entity\EntityMalformedException;
  8. use Drupal\Core\Entity\EntityStorageBase;
  9. use Drupal\Core\Entity\EntityTypeInterface;
  10. use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
  11. use Drupal\Core\Language\LanguageManagerInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Provides a key value backend for entities.
  15. *
  16. * @todo Entities that depend on auto-incrementing serial IDs need to explicitly
  17. * provide an ID until a generic wrapper around the functionality provided by
  18. * \Drupal\Core\Database\Connection::nextId() is added and used.
  19. * @todo Revisions are currently not supported.
  20. */
  21. class KeyValueEntityStorage extends EntityStorageBase {
  22. /**
  23. * Length limit of the entity ID.
  24. */
  25. const MAX_ID_LENGTH = 128;
  26. /**
  27. * The key value store.
  28. *
  29. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  30. */
  31. protected $keyValueStore;
  32. /**
  33. * The UUID service.
  34. *
  35. * @var \Drupal\Component\Uuid\UuidInterface
  36. */
  37. protected $uuidService;
  38. /**
  39. * The language manager.
  40. *
  41. * @var \Drupal\Core\Language\LanguageManagerInterface
  42. */
  43. protected $languageManager;
  44. /**
  45. * Constructs a new KeyValueEntityStorage.
  46. *
  47. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  48. * The entity type.
  49. * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value_store
  50. * The key value store.
  51. * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
  52. * The UUID service.
  53. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  54. * The language manager.
  55. */
  56. public function __construct(EntityTypeInterface $entity_type, KeyValueStoreInterface $key_value_store, UuidInterface $uuid_service, LanguageManagerInterface $language_manager) {
  57. parent::__construct($entity_type);
  58. $this->keyValueStore = $key_value_store;
  59. $this->uuidService = $uuid_service;
  60. $this->languageManager = $language_manager;
  61. // Check if the entity type supports UUIDs.
  62. $this->uuidKey = $this->entityType->getKey('uuid');
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  68. return new static(
  69. $entity_type,
  70. $container->get('keyvalue')->get('entity_storage__' . $entity_type->id()),
  71. $container->get('uuid'),
  72. $container->get('language_manager')
  73. );
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function doCreate(array $values = []) {
  79. // Set default language to site default if not provided.
  80. $values += [$this->getEntityType()->getKey('langcode') => $this->languageManager->getDefaultLanguage()->getId()];
  81. $entity = new $this->entityClass($values, $this->entityTypeId);
  82. // @todo This is handled by ContentEntityStorageBase, which assumes
  83. // FieldableEntityInterface. The current approach in
  84. // https://www.drupal.org/node/1867228 improves this but does not solve it
  85. // completely.
  86. if ($entity instanceof FieldableEntityInterface) {
  87. foreach ($entity as $name => $field) {
  88. if (isset($values[$name])) {
  89. $entity->$name = $values[$name];
  90. }
  91. elseif (!array_key_exists($name, $values)) {
  92. $entity->get($name)->applyDefaultValue();
  93. }
  94. unset($values[$name]);
  95. }
  96. }
  97. return $entity;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function doLoadMultiple(array $ids = NULL) {
  103. if (empty($ids)) {
  104. $entities = $this->keyValueStore->getAll();
  105. }
  106. else {
  107. $entities = $this->keyValueStore->getMultiple($ids);
  108. }
  109. return $this->mapFromStorageRecords($entities);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function loadRevision($revision_id) {
  115. return NULL;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function deleteRevision($revision_id) {
  121. return NULL;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function doDelete($entities) {
  127. $entity_ids = array_keys($entities);
  128. $this->keyValueStore->deleteMultiple($entity_ids);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function save(EntityInterface $entity) {
  134. $id = $entity->id();
  135. if ($id === NULL || $id === '') {
  136. throw new EntityMalformedException('The entity does not have an ID.');
  137. }
  138. // Check the entity ID length.
  139. // @todo This is not config-specific, but serial IDs will likely never hit
  140. // this limit. Consider renaming the exception class.
  141. if (strlen($entity->id()) > static::MAX_ID_LENGTH) {
  142. throw new ConfigEntityIdLengthException("Entity ID {$entity->id()} exceeds maximum allowed length of " . static::MAX_ID_LENGTH . ' characters.');
  143. }
  144. return parent::save($entity);
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. protected function doSave($id, EntityInterface $entity) {
  150. $is_new = $entity->isNew();
  151. // Save the entity data in the key value store.
  152. $this->keyValueStore->set($entity->id(), $entity->toArray());
  153. // If this is a rename, delete the original entity.
  154. if ($this->has($id, $entity) && $id !== $entity->id()) {
  155. $this->keyValueStore->delete($id);
  156. }
  157. return $is_new ? SAVED_NEW : SAVED_UPDATED;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. protected function has($id, EntityInterface $entity) {
  163. return $this->keyValueStore->has($id);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function hasData() {
  169. return (bool) $this->keyValueStore->getAll();
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. protected function getQueryServiceName() {
  175. return 'entity.query.keyvalue';
  176. }
  177. }