diff --git a/oe_search.services.yml b/oe_search.services.yml index 9fd2ced..3aae44d 100644 --- a/oe_search.services.yml +++ b/oe_search.services.yml @@ -4,8 +4,8 @@ services: arguments: [ '@entity_type.manager' ] tags: - { name: 'event_subscriber' } - oe_search.metadata_mapping_event_subscriber: - class: Drupal\oe_search\EventSubscriber\MetadataMappingSubscriber + oe_search.europa_entity_creation_subscriber: + class: Drupal\oe_search\EventSubscriber\EuropaEntityCreationSubscriber arguments: [ '@entity_type.manager' ] tags: - { name: 'event_subscriber' } diff --git a/src/EntityMapper.php b/src/EntityMapper.php index 349ee2e..c4e59f2 100644 --- a/src/EntityMapper.php +++ b/src/EntityMapper.php @@ -10,7 +10,6 @@ use Drupal\Core\Entity\Plugin\DataType\EntityAdapter; use Drupal\Core\Field\TypedData\FieldItemDataDefinition; use Drupal\oe_search\Event\EuropaEntityCreationEvent; -use Drupal\oe_search\Event\MetadataMappingEvent; use Drupal\search_api\Plugin\search_api\datasource\ContentEntity; use Drupal\search_api\Query\QueryInterface; @@ -89,10 +88,6 @@ public function map(array $metadata, QueryInterface $query) : ?EntityAdapter { } } - $event = new MetadataMappingEvent($query, $metadata, $index_fields, $entity_values); - $this->eventDispatcher->dispatch($event, MetadataMappingEvent::class); - $entity_values = $event->getValues(); - // 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; @@ -103,7 +98,7 @@ public function map(array $metadata, QueryInterface $query) : ?EntityAdapter { $entity->in_preview = TRUE; // Allow event subscribers to alter the created entity. $event = new EuropaEntityCreationEvent($entity, $metadata, $query); - $this->eventDispatcher->dispatch($event); + $this->eventDispatcher->dispatch($event, EuropaEntityCreationEvent::EUROPA_ENTITY_CREATED); $mapped_entity = EntityAdapter::createFromEntity($entity); } catch (EntityStorageException $e) { diff --git a/src/Event/EuropaEntityCreationEvent.php b/src/Event/EuropaEntityCreationEvent.php index 9a03dab..cad15b4 100644 --- a/src/Event/EuropaEntityCreationEvent.php +++ b/src/Event/EuropaEntityCreationEvent.php @@ -37,6 +37,13 @@ class EuropaEntityCreationEvent extends Event { */ protected $query; + /** + * The name of the event dispatched when a new europa entity is instatiated. + * + * Allows subscribers to alter the entity values. + */ + const EUROPA_ENTITY_CREATED = 'oe_search.europa_entity_created'; + /** * Constructs a new EuropaEntityCreationEvent object. * diff --git a/src/Event/MetadataMappingEvent.php b/src/Event/MetadataMappingEvent.php deleted file mode 100644 index b8147d2..0000000 --- a/src/Event/MetadataMappingEvent.php +++ /dev/null @@ -1,145 +0,0 @@ -query = $query; - $this->metadata = $metadata; - $this->indexFields = $index_fields; - $this->values = $values; - } - - /** - * Get the metadata. - * - * @return array - * The metadata. - */ - public function getMetadata(): array { - return $this->metadata; - } - - /** - * Set the metadata. - * - * @param array $metadata - * The metadata. - */ - public function setMetadata(array $metadata): void { - $this->metadata = $metadata; - } - - /** - * Get the mapped values. - * - * @return array - * The mapped values. - */ - public function getValues(): array { - return $this->values; - } - - /** - * Sets the mapped values. - * - * @param array $values - * The mapped values. - */ - public function setValues(array $values): void { - $this->values = $values; - } - - /** - * Get the query. - * - * @return \Drupal\search_api\Query\Query - * The query. - */ - public function getQuery(): Query { - return $this->query; - } - - /** - * Set the query. - * - * @param \Drupal\search_api\Query\Query $query - * The query. - */ - public function setQuery(Query $query): void { - $this->query = $query; - } - - /** - * Get the index fields. - * - * @return array - * The index fields. - */ - public function getIndexFields(): array { - return $this->indexFields; - } - - /** - * Sets the index fields. - * - * @param array $indexFields - * The index Fields. - */ - public function setIndexFields(array $indexFields): void { - $this->indexFields = $indexFields; - } - -} diff --git a/src/EventSubscriber/MetadataMappingSubscriber.php b/src/EventSubscriber/EuropaEntityCreationSubscriber.php similarity index 68% rename from src/EventSubscriber/MetadataMappingSubscriber.php rename to src/EventSubscriber/EuropaEntityCreationSubscriber.php index 54bd859..4e39c5b 100644 --- a/src/EventSubscriber/MetadataMappingSubscriber.php +++ b/src/EventSubscriber/EuropaEntityCreationSubscriber.php @@ -5,14 +5,15 @@ namespace Drupal\oe_search\EventSubscriber; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\oe_search\Event\MetadataMappingEvent; +use Drupal\Core\Field\TypedData\FieldItemDataDefinitionInterface; +use Drupal\oe_search\Event\EuropaEntityCreationEvent; use Drupal\oe_search\Utility; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Event subscriber for ES Entity Mapping. */ -class MetadataMappingSubscriber implements EventSubscriberInterface { +class EuropaEntityCreationSubscriber implements EventSubscriberInterface { /** * The entity type manager. @@ -36,28 +37,30 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) { */ public static function getSubscribedEvents(): array { return [ - MetadataMappingEvent::class => 'map', + EuropaEntityCreationEvent::EUROPA_ENTITY_CREATED => 'map', ]; } /** * Subscribes to the metadata mapping creation event. * - * @param \Drupal\oe_search\Event\MetadataMappingEvent $event + * @param \Drupal\oe_search\Event\EuropaEntityCreationEvent $event * The event object. * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function map(MetadataMappingEvent $event): void { - + public function map(EuropaEntityCreationEvent $event): void { $metadata = $event->getMetadata(); - $index_fields = $event->getIndexFields(); - $values = $event->getValues(); + $entity = $event->getEntity(); $query = $event->getQuery(); $datasource = $event->getQuery() ->getIndex() ->getDatasource($metadata['SEARCH_API_DATASOURCE'][0]); + + $datasource_id = $metadata[Utility::getEsFieldName('search_api_datasource', $query)][0]; + $index_fields = $query->getIndex()->getFieldsByDatasource($datasource_id); + $entity_type_id = $datasource->getDerivativeId(); $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $entity_bundle_key = $entity_type->getKey('bundle'); @@ -65,14 +68,14 @@ public function map(MetadataMappingEvent $event): void { foreach ($index_fields as $field) { $metadata_key = Utility::getEsFieldName($field->getFieldIdentifier(), $query); $original_field_id = $field->getOriginalFieldIdentifier(); - $original_field_type = $field->getDataDefinition() - ->getFieldDefinition() - ->getType(); + $data_definition = $field->getDataDefinition(); - // We only map here values present in metadata. - if (empty($values[$original_field_id])) { + if (!$data_definition instanceof FieldItemDataDefinitionInterface) { continue; } + $original_field_type = $data_definition + ->getFieldDefinition() + ->getType(); // Drop entity references, unless they are the bundle key. $entity_reference_types = [ @@ -80,13 +83,18 @@ public function map(MetadataMappingEvent $event): void { 'entity_reference_revisions', ]; + // We only alter values present in metadata. + if (empty($metadata[$metadata_key][0])) { + continue; + } + if ($metadata_key != Utility::getEsFieldName($entity_bundle_key, $query) && in_array($original_field_type, $entity_reference_types)) { - unset($values[$original_field_id]); + $entity->get($original_field_id)->removeItem(0); } // Support for booleans. if ($field->getType() == 'boolean') { - $values[$original_field_id] = filter_var($values[$original_field_id], FILTER_VALIDATE_BOOLEAN); + $entity->set($original_field_id, filter_var($entity->get($original_field_id)->value, FILTER_VALIDATE_BOOLEAN)); } elseif ($field->getType() == 'date') { $date = \DateTime::createFromFormat('Y-m-d\TH:i:s.vP', $metadata[$metadata_key][0]); @@ -101,26 +109,24 @@ public function map(MetadataMappingEvent $event): void { // Date time fields with date only. if ($date_type == 'datetime' && $datetime_type == 'date') { - $values[$original_field_id] = date('Y-m-d', $date->getTimestamp()); + $entity->set($original_field_id, date('Y-m-d', $date->getTimestamp())); } // Date time fields with date and time. elseif ($date_type == 'datetime' && $datetime_type == 'datetime') { - $values[$original_field_id] = date('Y-m-d\TH:i:s', $date->getTimestamp()); + $entity->set($original_field_id, date('Y-m-d\TH:i:s', $date->getTimestamp())); } elseif ($date_type == 'daterange_timezone') { - $values[$field->getOriginalFieldIdentifier()] = [ + $entity->set($field->getOriginalFieldIdentifier(), [ 'value' => date('Y-m-d\TH:i:s', $date->getTimestamp()), 'end_value' => date('Y-m-d\TH:i:s', $date->getTimestamp()), 'timezone' => $date->getTimezone()->getName(), - ]; + ]); } else { - $values[$original_field_id] = $date->getTimestamp(); + $entity->set($original_field_id, $date->getTimestamp()); } } } - - $event->setValues($values); } } diff --git a/src/Plugin/search_api/backend/SearchApiEuropaSearchBackend.php b/src/Plugin/search_api/backend/SearchApiEuropaSearchBackend.php index d257fd1..7cfbbcf 100644 --- a/src/Plugin/search_api/backend/SearchApiEuropaSearchBackend.php +++ b/src/Plugin/search_api/backend/SearchApiEuropaSearchBackend.php @@ -447,10 +447,11 @@ public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL */ public function search(QueryInterface $query): void { $results = $query->getResults(); - $page_number = $limit = NULL; + $page_number = NULL; + $limit = $query->getOptions()['limit'] ?? NULL; // Set page number. - if (!empty($query->getOptions()['offset']) && !empty($query->getOptions()['limit'])) { + if (!empty($query->getOptions()['offset']) && !empty($limit)) { $offset = $query->getOptions()['offset']; $limit = $query->getOptions()['limit']; $page_number = ($offset / $limit) + 1;