function taxonomy_entity_index_entity_update in Taxonomy Entity Index 8
Implements hook_entity_update().
4 calls to taxonomy_entity_index_entity_update()
- BatchService::processEntityTypeReindex in src/
BatchService.php - Batch process callback.
- TaxonomyEntityIndexUuidArgumentTest::setUp in tests/
src/ Functional/ Views/ TaxonomyEntityIndexUuidArgumentTest.php - taxonomy_entity_index_entity_insert in ./
taxonomy_entity_index.module - Implements hook_entity_insert().
- taxonomy_entity_index_reindex_entity_type in ./
taxonomy_entity_index.module - Batch callback; re-index all the terms for a given entity type.
File
- ./
taxonomy_entity_index.module, line 64 - Module file for taxonomy_entity_index.
Code
function taxonomy_entity_index_entity_update(EntityInterface $entity) {
$entity_type_id = $entity
->getEntityTypeId();
$config = \Drupal::config('taxonomy_entity_index.settings');
$entity_types_to_index = $config
->get('types');
if (!in_array($entity_type_id, $entity_types_to_index)) {
return;
}
$entity_id = $entity
->id();
$bundle = $entity
->bundle();
$revision_id = NULL;
if ($entity
->getEntityType()
->isRevisionable() === TRUE) {
$revision_id = $entity
->getRevisionId();
}
if (is_null($revision_id)) {
$revision_id = $entity_id;
}
if (!empty($config
->get('index_revisions'))) {
// Clear previous taxonomy index for the specific revision ID of the entity
// since we want to keep track of previous revisions.
\Drupal::database()
->delete('taxonomy_entity_index')
->condition('entity_type', $entity_type_id)
->condition('entity_id', $entity_id)
->condition('revision_id', $revision_id)
->execute();
}
else {
// Clear all prior index records for this entity.
\Drupal::database()
->delete('taxonomy_entity_index')
->condition('entity_type', $entity_type_id)
->condition('entity_id', $entity_id)
->execute();
}
$term_ref_field_names = taxonomy_entity_index_get_taxonomy_field_names($entity_type_id);
if (empty($term_ref_field_names[$bundle])) {
return;
}
$term_ref_field_names = $term_ref_field_names[$bundle];
$terms = [];
foreach ($term_ref_field_names as $field_name) {
if ($items = $entity
->get($field_name)
->getValue()) {
foreach ($items as $delta => $item) {
// Check the terms array to see if this one is already there.
$exists = array_search($item['target_id'], array_column($terms, 'tid'));
// Check config to see if we are to index each field individually.
if (!empty($config
->get('index_per_field')) || $exists === FALSE) {
$terms[] = [
'tid' => $item['target_id'],
'field_name' => $field_name,
'delta' => $delta,
];
}
}
}
}
if (!empty($terms)) {
$query = \Drupal::database()
->insert('taxonomy_entity_index');
$query
->fields([
'entity_type',
'bundle',
'entity_id',
'revision_id',
'field_name',
'delta',
'tid',
]);
foreach ($terms as $term) {
$query
->values([
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'entity_id' => $entity_id,
'revision_id' => $revision_id,
'field_name' => $term['field_name'],
'delta' => $term['delta'],
'tid' => $term['tid'],
]);
}
$query
->execute();
}
}