Skip to content

Commit d0042c6

Browse files
authored
Fix hash entity update fails (#4152)
1 parent 71f2789 commit d0042c6

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

modules/harvest/src/Entity/HarvestHash.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* },
3333
* base_table = "harvest_hashes",
3434
* entity_keys = {
35-
* "id" = "data_uuid",
35+
* "id" = "id",
3636
* },
3737
* internal = TRUE,
3838
* )
@@ -45,10 +45,14 @@ class HarvestHash extends ContentEntityBase implements HarvestHashInterface {
4545
* {@inheritdoc}
4646
*/
4747
public static function baseFieldDefinitions($entity_type) {
48+
// Parent will give us an 'id' field because we annotated it as the id key.
49+
$fields = parent::baseFieldDefinitions($entity_type);
50+
4851
// Data node UUID. This is a UUID to a Data node, probably of type
4952
// 'dataset'.
53+
// Note that this is a UUID by convention, and could be any string.
5054
// @todo Add uuid constraint.
51-
$fields['data_uuid'] = BaseFieldDefinition::create('uuid')
55+
$fields['data_uuid'] = BaseFieldDefinition::create('string')
5256
->setLabel(t('Data node UUID'))
5357
->setDescription(t('The Data node UUID.'))
5458
->setReadOnly(FALSE)

modules/harvest/src/Storage/HarvestHashesEntityDatabaseTable.php

+24-14
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function store($data, string $id = NULL) : string {
113113
]);
114114
}
115115
$entity->save();
116-
return $entity->get($this->primaryKey())->getString();
116+
return $entity->get('data_uuid')->getString();
117117
}
118118

119119
/**
@@ -150,7 +150,9 @@ public function remove(string $id) {
150150
->execute()
151151
) {
152152
$entity_id = reset($ids);
153-
$this->entityStorage->delete([$this->loadEntity($entity_id)]);
153+
$this->entityStorage->delete([
154+
$this->entityStorage->load($entity_id),
155+
]);
154156
}
155157
return $id;
156158
}
@@ -159,16 +161,23 @@ public function remove(string $id) {
159161
* {@inheritDoc}
160162
*/
161163
public function retrieveAll(): array {
162-
// Some calling code is very particular about the output being an array,
163-
// both as a return value here and after json_encode(). Since the entity
164-
// query returns a keyed array, json_encode() will think it's an object. We
165-
// don't want that, so we use array_values() to yield an indexed array.
166-
return array_values(
164+
$data_uuids = [];
165+
$entities = $this->entityStorage->loadMultiple(
167166
$this->entityStorage->getQuery()
168167
->condition('harvest_plan_id', $this->planId)
169168
->accessCheck(FALSE)
170169
->execute()
171170
);
171+
/** @var \Drupal\harvest\HarvestHashInterface $entity*/
172+
foreach ($entities as $entity) {
173+
$uuid = $entity->get('data_uuid')->getString();
174+
$data_uuids[$uuid] = $uuid;
175+
}
176+
// Some calling code is very particular about the output being an array,
177+
// both as a return value here and after json_encode(). Since we're using a
178+
// keyed array, json_encode() will think it's an object. We don't want that,
179+
// so we use array_values() to yield an indexed array.
180+
return array_values($data_uuids);
172181
}
173182

174183
/**
@@ -205,9 +214,10 @@ public function count(): int {
205214
* {@inheritDoc}
206215
*/
207216
public function primaryKey() {
208-
// Use the primary key defined in the entity definition.
209-
$definition = $this->entityTypeManager->getDefinition(static::ENTITY_TYPE);
210-
return ($definition->getKeys())['id'];
217+
// The primary key for entity API is 'id'. But the primary key for the
218+
// database table interface is 'data_uuid'. This is mostly arbitrary for our
219+
// purposes because we're not actually subclassing AbstractDatabaseTable.
220+
return 'data_uuid';
211221
}
212222

213223
/**
@@ -241,18 +251,18 @@ public function getSchema(): array {
241251
/**
242252
* Helper method to load an entity given an ID.
243253
*
244-
* @param string $id
254+
* @param string $data_uuid
245255
* Entity ID.
246256
*
247257
* @return \Drupal\harvest\HarvestHashInterface|null
248258
* The loaded entity or NULL if none could be loaded.
249259
*/
250-
protected function loadEntity(string $id): ?HarvestHashInterface {
251-
if (!$id) {
260+
protected function loadEntity(string $data_uuid): ?HarvestHashInterface {
261+
if (!$data_uuid) {
252262
return NULL;
253263
}
254264
if ($ids = $this->entityStorage->getQuery()
255-
->condition($this->primaryKey(), $id)
265+
->condition('data_uuid', $data_uuid)
256266
->condition('harvest_plan_id', $this->planId)
257267
->range(0, 1)
258268
->accessCheck(FALSE)

0 commit comments

Comments
 (0)