You are here

public function Link::trackOnEntityUpdate in Entity Usage 8

Track usage updates on the edition of entities.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity we are dealing with.

Overrides EntityUsageTrackBase::trackOnEntityUpdate

File

src/Plugin/EntityUsage/Track/Link.php, line 104

Class

Link
Tracks usage of entities related in entity_reference fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

public function trackOnEntityUpdate(ContentEntityInterface $entity) {

  // The assumption here is that an entity that is referenced by any
  // translation of another entity should be tracked, and only once
  // (regardless if many translations point to the same entity). So the
  // process to identify them is quite simple: we build a list of all entity
  // ids referenced before the update by all translations (original included),
  // and compare it with the list of ids referenced by all translations after
  // the update.
  $translations = [];
  $originals = [];
  $languages = $entity
    ->getTranslationLanguages();
  foreach ($languages as $langcode => $language) {
    if (!$entity
      ->hasTranslation($langcode)) {
      continue;
    }
    $translations[] = $entity
      ->getTranslation($langcode);
    if (!$entity->original
      ->hasTranslation($langcode)) {
      continue;
    }
    $originals[] = $entity->original
      ->getTranslation($langcode);
  }
  foreach ($this
    ->linkFieldsAvailable($entity) as $field_name) {
    $current_targets = [];
    foreach ($translations as $translation) {
      if ($translation
        ->hasField($field_name) && !$translation->{$field_name}
        ->isEmpty()) {
        foreach ($translation->{$field_name} as $field_item) {
          $target_entity = $this
            ->getTargetEntity($field_item);
          if ($target_entity) {
            $current_targets[] = $target_entity;
          }
        }
      }
    }
    $original_targets = [];
    foreach ($originals as $original) {
      if ($original
        ->hasField($field_name) && !$original->{$field_name}
        ->isEmpty()) {
        foreach ($original->{$field_name} as $field_item) {
          $target_entity = $this
            ->getTargetEntity($field_item);
          if ($target_entity) {
            $original_targets[] = $target_entity;
          }
        }
      }
    }

    // If more than one translation 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);
      $this->usageService
        ->add($target_id, $target_type, $entity
        ->id(), $entity
        ->getEntityTypeId(), $this->pluginId);
    }
    foreach ($removed_ids as $removed_entity) {
      list($target_type, $target_id) = explode('|', $removed_entity);
      $this->usageService
        ->delete($target_id, $target_type, $entity
        ->id(), $entity
        ->getEntityTypeId());
    }
  }
}