Skip to content

Commit

Permalink
Merge pull request #106 from openeuropa/EWPP-3280
Browse files Browse the repository at this point in the history
EWPP-3280: Prevent entity mapper failure for missing or different fields.
  • Loading branch information
upchuk authored Nov 6, 2023
2 parents 24eaf9b + c675c24 commit 3224f07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
25 changes: 23 additions & 2 deletions src/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function map(array $metadata, QueryInterface $query) : ?EntityAdapter {
$entity_id_key = $entity_type->getKey('id');
$entity_values = [];
$entity_bundle_key = $entity_type->getKey('bundle');
$entity_bundle_metadata_key = Utility::getEsFieldName($entity_bundle_key, $query);

foreach ($index_fields as $field) {
$metadata_key = Utility::getEsFieldName($field->getFieldIdentifier(), $query);
Expand All @@ -93,7 +94,7 @@ public function map(array $metadata, QueryInterface $query) : ?EntityAdapter {
'entity_reference_revisions',
];

if (in_array($original_field_type, $entity_reference_types) && $metadata_key !== Utility::getEsFieldName($entity_bundle_key, $query)) {
if (in_array($original_field_type, $entity_reference_types) && $entity_bundle_metadata_key) {
continue;
}

Expand All @@ -104,12 +105,32 @@ public function map(array $metadata, QueryInterface $query) : ?EntityAdapter {
}
}

if (!isset($entity_values[$entity_bundle_key])) {
// If we haven't found the bundle value based on the bundle key name
// in the metadata fields, iterate again through the index fields and
// see if any of them maps to the actual entity bundle key.
foreach ($index_fields as $field) {
if ($field->getPropertyPath() === $entity_bundle_key) {
$metadata_key = Utility::getEsFieldName($field->getFieldIdentifier(), $query);
$entity_values[$entity_bundle_key] = $metadata[$metadata_key][0];
break;
}
}
}

// We want to be able to call getUrl() on the entity, so we set a fake id.
$entity_values[$entity_id_key] = PHP_INT_MAX;

// Create entity from array of values.
try {
$entity = $this->entityTypeManager->getStorage($entity_type_id)->create($entity_values);
$entity = $this->entityTypeManager->getStorage($entity_type_id)->create([
$entity_bundle_key => $entity_values[$entity_bundle_key],
]);
foreach ($entity_values as $field_name => $value) {
if ($entity->hasField($field_name)) {
$entity->set($field_name, $value);
}
}
// Needed to avoid loading entity in translations.
$entity->in_preview = TRUE;
// Allow event subscribers to alter the created entity.
Expand Down
10 changes: 4 additions & 6 deletions src/EventSubscriber/EuropaEntityCreationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,9 @@ public function map(EuropaEntityCreationEvent $event): void {
$entity = $event->getEntity();
$query = $event->getQuery();
$datasource_id = $metadata[Utility::getEsFieldName('search_api_datasource', $query)][0];
$datasource = $event->getQuery()
->getIndex()
->getDatasource($datasource_id);

$index_fields = $query->getIndex()->getFieldsByDatasource($datasource_id);

$entity_type_id = $datasource->getDerivativeId();
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);

foreach ($index_fields as $field) {
$metadata_key = Utility::getEsFieldName($field->getFieldIdentifier(), $query);
$original_field_id = $field->getOriginalFieldIdentifier();
Expand All @@ -78,6 +72,10 @@ public function map(EuropaEntityCreationEvent $event): void {
continue;
}

if (!$entity->hasField($original_field_id)) {
continue;
}

// Support for booleans.
if ($field->getType() === 'boolean') {
$entity->set($original_field_id, filter_var($entity->get($original_field_id)->value, FILTER_VALIDATE_BOOLEAN));
Expand Down

0 comments on commit 3224f07

Please sign in to comment.