public function EntityUsageTrackBase::trackOnEntityUpdate in Entity Usage 8.2
Same name and namespace in other branches
- 8 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityUpdate()
- 8.3 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityUpdate()
Track usage updates on the edition of entities.
Parameters
\Drupal\Core\Entity\EntityInterface $source_entity: The source entity.
Overrides EntityUsageTrackInterface::trackOnEntityUpdate
File
- src/
EntityUsageTrackBase.php, line 162
Class
- EntityUsageTrackBase
- Base implementation for track plugins.
Namespace
Drupal\entity_usageCode
public function trackOnEntityUpdate(EntityInterface $source_entity) {
$trackable_field_types = $this
->getApplicableFieldTypes();
$fields = array_keys($this
->getReferencingFields($source_entity, $trackable_field_types));
foreach ($fields as $field_name) {
if ($source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() != $source_entity->original
->getRevisionId() && $source_entity
->hasField($field_name) && !$source_entity->{$field_name}
->isEmpty()) {
$this
->trackOnEntityCreation($source_entity);
return;
}
// We are updating an existing revision, compare target entities to see if
// we need to add or remove tracking records.
$current_targets = [];
if ($source_entity
->hasField($field_name) && !$source_entity->{$field_name}
->isEmpty()) {
foreach ($source_entity->{$field_name} as $field_item) {
$target_entities = $this
->getTargetEntities($field_item);
foreach ($target_entities as $target_entity) {
$current_targets[] = $target_entity;
}
}
}
$original_targets = [];
if ($source_entity->original
->hasField($field_name) && !$source_entity->original->{$field_name}
->isEmpty()) {
foreach ($source_entity->original->{$field_name} as $field_item) {
$target_entities = $this
->getTargetEntities($field_item);
foreach ($target_entities as $target_entity) {
$original_targets[] = $target_entity;
}
}
}
// If a field references the same target entity, we record only one usage.
$original_targets = array_unique($original_targets);
$current_targets = array_unique($current_targets);
$added_ids = array_diff($current_targets, $original_targets);
$removed_ids = array_diff($original_targets, $current_targets);
foreach ($added_ids as $added_entity) {
list($target_type, $target_id) = explode('|', $added_entity);
$source_vid = $source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() ? $source_entity
->getRevisionId() : 0;
$this->usageService
->registerUsage($target_id, $target_type, $source_entity
->id(), $source_entity
->getEntityTypeId(), $source_entity
->language()
->getId(), $source_vid, $this->pluginId, $field_name);
}
foreach ($removed_ids as $removed_entity) {
list($target_type, $target_id) = explode('|', $removed_entity);
$source_vid = $source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() ? $source_entity
->getRevisionId() : 0;
$this->usageService
->registerUsage($target_id, $target_type, $source_entity
->id(), $source_entity
->getEntityTypeId(), $source_entity
->language()
->getId(), $source_vid, $this->pluginId, $field_name, 0);
}
}
}