Skip to content

Commit f044c5d

Browse files
authored
[SITE-5268] Ensure index insertion only happens when publishing. (#33)
1 parent 4de5849 commit f044c5d

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

src/PantheonDocumentStorage.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
1515
use Drupal\Core\Entity\EntityTypeInterface;
1616
use Drupal\Core\Field\FieldDefinitionInterface;
17-
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
18-
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
1917
use Drupal\pantheon_content_publisher\Entity\PantheonDocument;
2018
use Symfony\Component\DependencyInjection\ContainerInterface;
2119

@@ -26,8 +24,6 @@ class PantheonDocumentStorage extends ContentEntityStorageBase implements Panthe
2624

2725
const SEPARATOR = '.';
2826

29-
protected KeyValueStoreInterface $seenStore;
30-
3127
public function __construct(
3228
EntityTypeInterface $entity_type,
3329
EntityFieldManagerInterface $entity_field_manager,
@@ -36,9 +32,7 @@ public function __construct(
3632
EntityTypeBundleInfoInterface $entity_type_bundle_info,
3733
protected EntityStorageInterface $collectionStorage,
3834
protected PantheonContentPublisherConverter $pantheonContentPublisherConverter,
39-
KeyValueFactoryInterface $keyValueFactory,
4035
) {
41-
$this->seenStore = $keyValueFactory->get('pantheon_document.seen');
4236
parent::__construct($entity_type, $entity_field_manager, $cache, $memory_cache, $entity_type_bundle_info);
4337
}
4438

@@ -50,8 +44,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
5044
$container->get('entity.memory_cache'),
5145
$container->get('entity_type.bundle.info'),
5246
$container->get('entity_type.manager')->getStorage('pantheon_document_collection'),
53-
$container->get('pantheon_content_publisher.converter'),
54-
$container->get('keyvalue')
47+
$container->get('pantheon_content_publisher.converter')
5548
);
5649
}
5750

@@ -71,9 +64,6 @@ protected function doLoadMultiple(?array $ids = NULL) {
7164
$drupal_data = $this->pantheonContentPublisherConverter
7265
->convert($pantheon_data, $collection_name, $id);
7366
$document = PantheonDocument::create($drupal_data);
74-
if ($this->seenStore->setIfNotExists($id, 1)) {
75-
$this->invokeHook('insert', $document);
76-
}
7767
$entities[$id] = $document->enforceIsNew(FALSE);
7868
}
7969
return $entities;
@@ -137,11 +127,4 @@ public static function getEntityId(string|PantheonDocumentCollectionInterface $c
137127
$pantheon_id;
138128
}
139129

140-
protected function doDelete($entities) {
141-
parent::doDelete($entities);
142-
foreach ($entities as $entity) {
143-
$this->seenStore->delete($entity->id());
144-
}
145-
}
146-
147130
}

src/Plugin/QueueWorker/EntityQueueWorker.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Drupal\pantheon_content_publisher\Plugin\QueueWorker;
66

77
use Drupal\Core\Entity\EntityTypeManagerInterface;
8+
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
9+
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
810
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
911
use Drupal\Core\Queue\QueueWorkerBase;
1012
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -20,12 +22,16 @@
2022
*/
2123
class EntityQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
2224

25+
protected KeyValueStoreInterface $seenStore;
26+
2327
public function __construct(
2428
array $configuration,
2529
$plugin_id,
2630
$plugin_definition,
2731
protected EntityTypeManagerInterface $entityTypeManager,
32+
KeyValueFactoryInterface $keyValueFactory,
2833
) {
34+
$this->seenStore = $keyValueFactory->get('pantheon_document.seen');
2935
parent::__construct($configuration, $plugin_id, $plugin_definition);
3036
}
3137

@@ -38,6 +44,7 @@ public static function create(ContainerInterface $container, array $configuratio
3844
$plugin_id,
3945
$plugin_definition,
4046
$container->get('entity_type.manager'),
47+
$container->get('keyvalue')
4148
);
4249
}
4350

@@ -50,6 +57,11 @@ public function processItem($data): void {
5057

5158
if (empty($data['delete'])) {
5259
$entity = $storage->load($data['entity_id']);
60+
if ($this->seenStore->setIfNotExists($entity->id(), 1)) {
61+
(new \ReflectionObject($storage))
62+
->getMethod('invokeHook')
63+
->invoke($storage, 'insert', $entity);
64+
}
5365
$entity->save();
5466
}
5567
else {
@@ -60,13 +72,7 @@ public function processItem($data): void {
6072
]);
6173
$entity->enforceIsNew(FALSE);
6274
$entity->delete();
63-
}
64-
$index_storage = $this->entityTypeManager->getStorage('search_api_index');
65-
$datasource_id = "entity:$entity_type_id";
66-
// Querying for datasource_id is possible, however the default config
67-
// entity query implementation loads all indexes anyways.
68-
foreach ($index_storage->loadMultiple() as $index) {
69-
$index->indexItems(-1, $datasource_id);
75+
$this->seenStore->delete($entity->id());
7076
}
7177
}
7278

0 commit comments

Comments
 (0)