Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 33 |
|
0.00% |
0 / 28 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
| InstanceStorage | |
0.00% |
0 / 45 |
|
0.00% |
0 / 33 |
|
0.00% |
0 / 28 |
|
0.00% |
0 / 16 |
552 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createInstance | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| loadUnchanged | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| countFieldData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| doDelete | |
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| doLoadMultiple | |
0.00% |
0 / 7 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| doSave | |
0.00% |
0 / 6 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getQueryServiceName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| has | |
0.00% |
0 / 3 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| purgeFieldItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| readFieldItemsToPurge | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doLoadMultipleRevisionsFieldItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doDeleteFieldItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doDeleteRevisionFieldItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doSaveFieldItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doPreSave | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder; |
| 6 | |
| 7 | use Drupal\Core\Cache\CacheBackendInterface; |
| 8 | use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; |
| 9 | use Drupal\Core\Entity\ContentEntityInterface; |
| 10 | use Drupal\Core\Entity\ContentEntityStorageBase; |
| 11 | use Drupal\Core\Entity\EntityFieldManagerInterface; |
| 12 | use Drupal\Core\Entity\EntityInterface; |
| 13 | use Drupal\Core\Entity\EntityTypeBundleInfoInterface; |
| 14 | use Drupal\Core\Entity\EntityTypeInterface; |
| 15 | use Drupal\Core\Field\FieldDefinitionInterface; |
| 16 | use Drupal\Core\Session\AccountInterface; |
| 17 | use Drupal\Core\State\StateInterface; |
| 18 | use Drupal\display_builder\Entity\Instance; |
| 19 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 20 | |
| 21 | /** |
| 22 | * Base class for content entity storage handlers. |
| 23 | */ |
| 24 | class InstanceStorage extends ContentEntityStorageBase { |
| 25 | |
| 26 | private const STORAGE_INDEX = 'display_builder_index'; |
| 27 | |
| 28 | private const STORAGE_PREFIX = 'display_builder_'; |
| 29 | |
| 30 | /** |
| 31 | * Current user. |
| 32 | */ |
| 33 | protected AccountInterface $currentUser; |
| 34 | |
| 35 | /** |
| 36 | * State API. |
| 37 | */ |
| 38 | protected StateInterface $state; |
| 39 | |
| 40 | /** |
| 41 | * {@inheritdoc} |
| 42 | */ |
| 43 | public function __construct( |
| 44 | EntityTypeInterface $entity_type, |
| 45 | EntityFieldManagerInterface $entity_field_manager, |
| 46 | CacheBackendInterface $cache, |
| 47 | MemoryCacheInterface $memory_cache, |
| 48 | EntityTypeBundleInfoInterface $entity_type_bundle_info, |
| 49 | StateInterface $state, |
| 50 | AccountInterface $current_user, |
| 51 | ) { |
| 52 | $this->state = $state; |
| 53 | $this->currentUser = $current_user; |
| 54 | parent::__construct($entity_type, $entity_field_manager, $cache, $memory_cache, $entity_type_bundle_info); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * {@inheritdoc} |
| 59 | */ |
| 60 | public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): self { |
| 61 | return new static( |
| 62 | $entity_type, |
| 63 | $container->get('entity_field.manager'), |
| 64 | $container->get('cache.entity'), |
| 65 | $container->get('entity.memory_cache'), |
| 66 | $container->get('entity_type.bundle.info'), |
| 67 | $container->get('state'), |
| 68 | $container->get('current_user') |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritdoc} |
| 74 | */ |
| 75 | public function loadUnchanged($id): ?EntityInterface { |
| 76 | $this->state->resetCache(); |
| 77 | |
| 78 | return parent::loadUnchanged($id); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * {@inheritdoc} |
| 83 | */ |
| 84 | public function countFieldData($storage_definition, $as_bool = FALSE) { |
| 85 | return $as_bool ? FALSE : 0; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritdoc} |
| 90 | */ |
| 91 | protected function doDelete($entities): void { |
| 92 | foreach ($entities as $entity) { |
| 93 | $id = (string) $entity->id(); |
| 94 | $display_builder_list = $this->state->get(self::STORAGE_INDEX, []); |
| 95 | unset($display_builder_list[$id]); |
| 96 | |
| 97 | $this->state->set(self::STORAGE_INDEX, $display_builder_list); |
| 98 | $this->state->delete(self::STORAGE_PREFIX . $id); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * {@inheritdoc} |
| 104 | */ |
| 105 | protected function doLoadMultiple(?array $ids = NULL): array { |
| 106 | $entities = []; |
| 107 | |
| 108 | if ($ids === NULL) { |
| 109 | $ids = \array_keys($this->state->get(self::STORAGE_INDEX, [])); |
| 110 | } |
| 111 | |
| 112 | foreach ($ids as $id) { |
| 113 | if ($data = $this->state->get(self::STORAGE_PREFIX . $id, NULL)) { |
| 114 | // @todo update once we manage proper revisions and translations. |
| 115 | $entities[$id] = new Instance($data, 'display_builder_instance'); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return $entities; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | protected function doSave($id, EntityInterface $entity): bool|int { |
| 126 | /** @var \Drupal\display_builder\InstanceInterface $entity */ |
| 127 | |
| 128 | $display_builder_list = $this->state->get(self::STORAGE_INDEX, []); |
| 129 | |
| 130 | if (!isset($display_builder_list[$entity->id()])) { |
| 131 | $display_builder_list[$entity->id()] = ''; |
| 132 | } |
| 133 | |
| 134 | $this->state->set(self::STORAGE_INDEX, $display_builder_list); |
| 135 | $this->state->set(self::STORAGE_PREFIX . $entity->id(), $entity->toArray()); |
| 136 | |
| 137 | return TRUE; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * {@inheritdoc} |
| 142 | */ |
| 143 | protected function getQueryServiceName(): string { |
| 144 | return 'entity.query.null'; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * {@inheritdoc} |
| 149 | */ |
| 150 | protected function has($id, EntityInterface $entity): bool { |
| 151 | if ($entity->isNew()) { |
| 152 | return FALSE; |
| 153 | } |
| 154 | |
| 155 | return (bool) $this->state->get(self::STORAGE_PREFIX . $id, NULL); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * {@inheritdoc} |
| 160 | */ |
| 161 | protected function purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition): void {} |
| 162 | |
| 163 | /** |
| 164 | * {@inheritdoc} |
| 165 | */ |
| 166 | protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definition, mixed $batch_size): array { |
| 167 | return []; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * {@inheritdoc} |
| 172 | */ |
| 173 | protected function doLoadMultipleRevisionsFieldItems(mixed $revision_ids): array { |
| 174 | return []; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * {@inheritdoc} |
| 179 | */ |
| 180 | protected function doDeleteFieldItems(mixed $entities): void {} |
| 181 | |
| 182 | /** |
| 183 | * {@inheritdoc} |
| 184 | */ |
| 185 | protected function doDeleteRevisionFieldItems(ContentEntityInterface $revision): void {} |
| 186 | |
| 187 | /** |
| 188 | * {@inheritdoc} |
| 189 | */ |
| 190 | protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []): void {} |
| 191 | |
| 192 | /** |
| 193 | * {@inheritdoc} |
| 194 | */ |
| 195 | protected function doPreSave(EntityInterface $entity): int|string|null { |
| 196 | return $entity->id(); |
| 197 | } |
| 198 | |
| 199 | } |