You are here

public function EmbedBase::trackOnEntityDeletion in Entity Usage 8

Track usage updates on the deletion of entities.

Parameters

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

Overrides EntityUsageTrackBase::trackOnEntityDeletion

File

src/Plugin/EntityUsage/Track/EmbedBase.php, line 136

Class

EmbedBase
Base class for plugins tracking usage in entities embedded in WYSIWYG fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

public function trackOnEntityDeletion(ContentEntityInterface $entity) {
  $translations = [];

  // When deleting the main (untranslated) entity, loop over all translations
  // as well to release referenced entities there too.
  if ($entity === $entity
    ->getUntranslated()) {
    $languages = $entity
      ->getTranslationLanguages();
    foreach ($languages as $langcode => $language) {
      if (!$entity
        ->hasTranslation($langcode)) {
        continue;
      }
      $translations[] = $entity
        ->getTranslation($langcode);
    }
  }
  else {

    // Otherwise, this is a single translation being deleted, so we just need
    // to release usage reflected here.
    $translations = [
      $entity,
    ];
  }
  foreach ($translations as $translation) {
    $referenced_entities_by_field = $this
      ->getEmbeddedEntitiesByField($translation);
    foreach ($referenced_entities_by_field as $field => $embedded_entities) {
      foreach ($embedded_entities as $uuid => $type) {

        // Decrement the usage as embedded entity.
        $this
          ->decrementEmbeddedUsage($entity, $type, $uuid);
      }
    }
  }
}