Skip to content

Commit

Permalink
Fix hash entity update fails (#4152)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-m authored Mar 29, 2024
1 parent 71f2789 commit d0042c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
8 changes: 6 additions & 2 deletions modules/harvest/src/Entity/HarvestHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* },
* base_table = "harvest_hashes",
* entity_keys = {
* "id" = "data_uuid",
* "id" = "id",
* },
* internal = TRUE,
* )
Expand All @@ -45,10 +45,14 @@ class HarvestHash extends ContentEntityBase implements HarvestHashInterface {
* {@inheritdoc}
*/
public static function baseFieldDefinitions($entity_type) {
// Parent will give us an 'id' field because we annotated it as the id key.
$fields = parent::baseFieldDefinitions($entity_type);

// Data node UUID. This is a UUID to a Data node, probably of type
// 'dataset'.
// Note that this is a UUID by convention, and could be any string.
// @todo Add uuid constraint.
$fields['data_uuid'] = BaseFieldDefinition::create('uuid')
$fields['data_uuid'] = BaseFieldDefinition::create('string')
->setLabel(t('Data node UUID'))
->setDescription(t('The Data node UUID.'))
->setReadOnly(FALSE)
Expand Down
38 changes: 24 additions & 14 deletions modules/harvest/src/Storage/HarvestHashesEntityDatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function store($data, string $id = NULL) : string {
]);
}
$entity->save();
return $entity->get($this->primaryKey())->getString();
return $entity->get('data_uuid')->getString();
}

/**
Expand Down Expand Up @@ -150,7 +150,9 @@ public function remove(string $id) {
->execute()
) {
$entity_id = reset($ids);
$this->entityStorage->delete([$this->loadEntity($entity_id)]);
$this->entityStorage->delete([
$this->entityStorage->load($entity_id),
]);
}
return $id;
}
Expand All @@ -159,16 +161,23 @@ public function remove(string $id) {
* {@inheritDoc}
*/
public function retrieveAll(): array {
// Some calling code is very particular about the output being an array,
// both as a return value here and after json_encode(). Since the entity
// query returns a keyed array, json_encode() will think it's an object. We
// don't want that, so we use array_values() to yield an indexed array.
return array_values(
$data_uuids = [];
$entities = $this->entityStorage->loadMultiple(
$this->entityStorage->getQuery()
->condition('harvest_plan_id', $this->planId)
->accessCheck(FALSE)
->execute()
);
/** @var \Drupal\harvest\HarvestHashInterface $entity*/
foreach ($entities as $entity) {
$uuid = $entity->get('data_uuid')->getString();
$data_uuids[$uuid] = $uuid;
}
// Some calling code is very particular about the output being an array,
// both as a return value here and after json_encode(). Since we're using a
// keyed array, json_encode() will think it's an object. We don't want that,
// so we use array_values() to yield an indexed array.
return array_values($data_uuids);
}

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

/**
Expand Down Expand Up @@ -241,18 +251,18 @@ public function getSchema(): array {
/**
* Helper method to load an entity given an ID.
*
* @param string $id
* @param string $data_uuid
* Entity ID.
*
* @return \Drupal\harvest\HarvestHashInterface|null
* The loaded entity or NULL if none could be loaded.
*/
protected function loadEntity(string $id): ?HarvestHashInterface {
if (!$id) {
protected function loadEntity(string $data_uuid): ?HarvestHashInterface {
if (!$data_uuid) {
return NULL;
}
if ($ids = $this->entityStorage->getQuery()
->condition($this->primaryKey(), $id)
->condition('data_uuid', $data_uuid)
->condition('harvest_plan_id', $this->planId)
->range(0, 1)
->accessCheck(FALSE)
Expand Down

0 comments on commit d0042c6

Please sign in to comment.