public function Link::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/ Link.php, line 171
Class
- Link
- Tracks usage of entities related in entity_reference fields.
Namespace
Drupal\entity_usage\Plugin\EntityUsage\TrackCode
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 ($this
->linkFieldsAvailable($entity) as $field_name) {
foreach ($translations as $translation) {
if ($translation
->hasField($field_name) && !$translation->{$field_name}
->isEmpty()) {
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $field_item */
foreach ($translation->{$field_name} as $field_item) {
// This item got deleted. Track the usage down.
$target_entity = $this
->getTargetEntity($field_item);
if ($target_entity) {
list($target_type, $target_id) = explode('|', $target_entity);
$this->usageService
->delete($target_id, $target_type, $entity
->id(), $entity
->getEntityTypeId());
}
}
}
}
}
}