public function ContentEntityTrackingManager::trackEntityChange in Search API 8
Queues an entity for indexing.
If "Index items immediately" is enabled for the index, the entity will be indexed right at the end of the page request.
When calling this method with an existing entity (
$new = FALSE;
), changes in the existing translations will only be recognized if an appropriate
$entity->original;
value is set.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The content entity to be indexed.
bool $new: (optional) TRUE if this is a new entity, FALSE if it already existed (and should already be known to the tracker).
2 calls to ContentEntityTrackingManager::trackEntityChange()
- ContentEntityTrackingManager::entityInsert in src/
Plugin/ search_api/ datasource/ ContentEntityTrackingManager.php - Implements hook_entity_insert().
- ContentEntityTrackingManager::entityUpdate in src/
Plugin/ search_api/ datasource/ ContentEntityTrackingManager.php - Implements hook_entity_update().
File
- src/
Plugin/ search_api/ datasource/ ContentEntityTrackingManager.php, line 124
Class
- ContentEntityTrackingManager
- Provides hook implementations on behalf of the Content Entity datasource.
Namespace
Drupal\search_api\Plugin\search_api\datasourceCode
public function trackEntityChange(ContentEntityInterface $entity, bool $new = FALSE) {
// Check if the entity is a content entity.
if (!empty($entity->search_api_skip_tracking)) {
return;
}
$indexes = $this
->getIndexesForEntity($entity);
if (!$indexes) {
return;
}
// Compare old and new languages for the entity to identify inserted,
// updated and deleted translations (and, therefore, search items).
$entity_id = $entity
->id();
$new_translations = array_keys($entity
->getTranslationLanguages());
$old_translations = [];
if (!$new) {
// In case we don't have the original, fall back to the current entity,
// and assume no new translations were added.
$original = $entity->original ?? $entity;
$old_translations = array_keys($original
->getTranslationLanguages());
}
$deleted_translations = array_diff($old_translations, $new_translations);
$inserted_translations = array_diff($new_translations, $old_translations);
$updated_translations = array_diff($new_translations, $inserted_translations);
$datasource_id = 'entity:' . $entity
->getEntityTypeId();
$get_ids = function (string $langcode) use ($entity_id) : string {
return $entity_id . ':' . $langcode;
};
$inserted_ids = array_map($get_ids, $inserted_translations);
$updated_ids = array_map($get_ids, $updated_translations);
$deleted_ids = array_map($get_ids, $deleted_translations);
foreach ($indexes as $index) {
if ($inserted_ids) {
$filtered_item_ids = $this
->filterValidItemIds($index, $datasource_id, $inserted_ids);
$index
->trackItemsInserted($datasource_id, $filtered_item_ids);
}
if ($updated_ids) {
$index
->trackItemsUpdated($datasource_id, $updated_ids);
}
if ($deleted_ids) {
$index
->trackItemsDeleted($datasource_id, $deleted_ids);
}
}
}