Skip to content

Commit

Permalink
Roll in additional entity tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Mar 4, 2024
1 parent 01b77bb commit a82bdbb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 50 deletions.
76 changes: 76 additions & 0 deletions embargo.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
Expand All @@ -14,6 +15,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\embargo\EmbargoInterface;
use Drupal\embargo\EmbargoItemList;
use Drupal\embargo\EmbargoStorage;
use Drupal\islandora_hierarchical_access\LUTGeneratorInterface;

/**
* Implements hook_entity_type_alter().
Expand Down Expand Up @@ -134,3 +136,77 @@ function embargo_entity_base_field_info(EntityTypeInterface $entity_type) {

return $fields;
}

/**
* Implements hook_ENTITY_TYPE_insert() for embargo entities.
*/
function embargo_embargo_insert(EntityInterface $entity) : void {
_embargo_search_api_track($entity);
}

/**
* Implements hook_ENTITY_TYPE_update() for embargo entities.
*/
function embargo_embargo_update(EntityInterface $entity) : void {
_embargo_search_api_track($entity);
}

/**
* Implements hook_ENTITY_TYPE_delete() for embargo entities.
*/
function embargo_embargo_delete(EntityInterface $entity) : void {
_embargo_search_api_track($entity);
}

/**
* Helper; deal with updating indexes of related items.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The embargo instance.
*/
function _embargo_search_api_track(EntityInterface $entity) : void {
assert($entity instanceof EmbargoInterface);
if (!\Drupal::moduleHandler()->moduleExists('search_api')) {
return;
}

// On updates, deal with the original value, in addition to the new.
if (isset($entity->original)) {
_embargo_search_api_track($entity->original);
}

if (!($node = $entity->getEmbargoedNode())) {
// No embargoed node?
return;
}

/** @var \Drupal\search_api\Plugin\search_api\datasource\ContentEntityTrackingManager $tracking_manager */
$tracking_manager = \Drupal::getContainer()->get('search_api.entity_datasource.tracking_manager');
/** @var \Drupal\search_api\Utility\TrackingHelperInterface $tracking_helper */
$tracking_helper = \Drupal::getContainer()->get('search_api.tracking_helper');

$track = function (ContentEntityInterface $entity) use ($tracking_manager, $tracking_helper) {
$tracking_manager->trackEntityChange($entity);
$tracking_helper->trackReferencedEntityUpdate($entity);
};

$track($node);

$results = \Drupal::database()->select(LUTGeneratorInterface::TABLE_NAME, 'lut')
->fields('lut', ['mid', 'fid'])
->condition('nid', $node->id())
->execute();
$media_ids = array_unique($results->fetchCol(0));
$file_ids = array_unique($results->fetchCol(1));

$entity_type_manager = \Drupal::entityTypeManager();
/** @var \Drupal\media\MediaInterface $media */
foreach ($entity_type_manager->getStorage('media')->loadMultiple($media_ids) as $media) {
$track($media);
}
/** @var \Drupal\file\FileInterface $file */
foreach ($entity_type_manager->getStorage('file')->loadMultiple($file_ids) as $file) {
$track($file);
}

}
4 changes: 0 additions & 4 deletions embargo.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ services:
- '@url_generator'
tags:
- { name: 'event_subscriber' }
embargo.tracking_event_subscriber:
class: Drupal\embargo\EventSubscriber\TrackingEventSubscriber
tags:
- { name: 'event_subscriber' }
46 changes: 0 additions & 46 deletions src/EventSubscriber/TrackingEventSubscriber.php

This file was deleted.

0 comments on commit a82bdbb

Please sign in to comment.