Skip to content

Commit

Permalink
Attempt rolling additional tracking.
Browse files Browse the repository at this point in the history
... didn't seem to work.
  • Loading branch information
adam-vessey committed Mar 4, 2024
1 parent a4a8d3e commit 01b77bb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions embargo.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ services:
- '@url_generator'
tags:
- { name: 'event_subscriber' }
embargo.tracking_event_subscriber:
class: Drupal\embargo\EventSubscriber\TrackingEventSubscriber
tags:
- { name: 'event_subscriber' }
46 changes: 46 additions & 0 deletions src/EventSubscriber/TrackingEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\embargo\EventSubscriber;

use Drupal\search_api\Event\MappingForeignRelationshipsEvent;
use Drupal\search_api\Event\SearchApiEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Inform search_api of entity_reference structures.
*/
class TrackingEventSubscriber implements EventSubscriberInterface {

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() : array {
return [
SearchApiEvents::MAPPING_FOREIGN_RELATIONSHIPS => 'foreignRelationshipMap',
];
}

/**
* Inform search_api of our computed entity_reference listings.
*
* @param \Drupal\search_api\Event\MappingForeignRelationshipsEvent $event
* The event by which to inform.
*/
public function foreignRelationshipMap(MappingForeignRelationshipsEvent $event) : void {
$mapping =& $event->getForeignRelationshipsMapping();
$index = $event->getIndex();
foreach ($index->getDatasources() as $id => $datasource) {
if (!in_array($datasource->getEntityTypeId(), ['file', 'media', 'node'])) {
continue;
}

$mapping[] = [
'datasource' => $id,
'entity_type' => 'embargo',
'property_path_to_foreign_entity' => 'embargo:entity:id',
'field_name' => 'id',
];
}
}

}

0 comments on commit 01b77bb

Please sign in to comment.