You are here

public function EntityUsageTrackBase::trackOnEntityUpdate in Entity Usage 8.3

Same name and namespace in other branches
  1. 8 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityUpdate()
  2. 8.2 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.

\Drupal\Core\Entity\EntityInterface $original: The original copy of the source entity, before being updated.

Overrides EntityUsageTrackInterface::trackOnEntityUpdate

File

src/EntityUsageTrackBase.php, line 156

Class

EntityUsageTrackBase
Base implementation for track plugins.

Namespace

Drupal\entity_usage

Code

public function trackOnEntityUpdate(EntityInterface $top_entity, EntityInterface $original) {
  if (!$this
    ->isApplicable($top_entity)) {
    return;
  }
  $top_vid = $top_entity instanceof RevisionableInterface && $top_entity
    ->getRevisionId() ? $top_entity
    ->getRevisionId() : 0;
  $current_targets = $this
    ->getAllBottomLevelTargets($top_entity);

  // If we are creating a new revision, we only care about writing the
  // current targets to the DB.
  if ($original instanceof RevisionableInterface && $top_vid > $original
    ->getRevisionId()) {
    foreach ($current_targets as $target_type_and_id) {
      list($target_type, $target_id) = explode('|', $target_type_and_id);
      $this->usageService
        ->registerUsage($target_id, $target_type, $top_entity
        ->id(), $top_entity
        ->getEntityTypeId(), $top_entity
        ->language()
        ->getId(), $top_vid);
    }
  }
  else {
    $original_targets = $this
      ->getAllBottomLevelTargets($original);
    $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);
      $this->usageService
        ->registerUsage($target_id, $target_type, $top_entity
        ->id(), $top_entity
        ->getEntityTypeId(), $top_entity
        ->language()
        ->getId(), $top_vid);
    }
    foreach ($removed_ids as $removed_entity) {
      list($target_type, $target_id) = explode('|', $removed_entity);
      $this->usageService
        ->deleteUsage($target_id, $target_type, $top_entity
        ->id(), $top_entity
        ->getEntityTypeId(), $top_entity
        ->language()
        ->getId(), $top_vid);
    }
  }
}