You are here

public function SearchApiAlgoliaHelper::entityDelete in Search API Algolia 3.0.x

Implements hook_entity_delete().

Deletes all entries for this entity from the tracking table for each index that tracks this entity type.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The deleted entity.

See also

search_api_algolia_entity_delete()

File

src/SearchApiAlgoliaHelper.php, line 70

Class

SearchApiAlgoliaHelper
Class Search Api Algolia Helper.

Namespace

Drupal\search_api_algolia

Code

public function entityDelete(EntityInterface $entity) {

  // Check if the entity is a content entity.
  if (!$entity instanceof ContentEntityInterface || $entity->search_api_skip_tracking) {
    return;
  }
  $indexes = ContentEntity::getIndexesForEntity($entity);
  if (!$indexes) {
    return;
  }

  // Remove the search items for all the entity's translations.
  foreach ($indexes as $index) {
    $object_id_field = $index
      ->getOption('object_id_field');
    if ($object_id_field) {
      $object_id = $entity
        ->get($object_id_field)
        ->getString();
      if ($object_id) {

        // @todo make this work with indexes having language suffix.
        $this
          ->scheduleForDeletion($index, [
          $object_id,
        ]);
      }
    }
  }
}