public function LinkCleanUp::cleanUpForEntity in Link checker 8
Removes non-existing links for given entity.
Parameters
\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity.
File
- src/
LinkCleanUp.php, line 116
Class
- LinkCleanUp
- Class LinkCleanUp.
Namespace
Drupal\linkcheckerCode
public function cleanUpForEntity(FieldableEntityInterface $entity) {
// Trying to reload the entity
// If it was removed then we should remove all links related to the entity.
$isEntityDeleted = !$this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
$extractedIds = [];
/** @var \Drupal\linkchecker\LinkCheckerStorage $storage */
$storage = $this->entityTypeManager
->getStorage('linkcheckerlink');
// If entity is not deleted, gather all links that exists in fields.
if (!$isEntityDeleted) {
$links = $this->extractor
->extractFromEntity($entity);
foreach ($links as $link) {
$extractedIds = array_merge($storage
->getExistingIdsFromLink($link), $extractedIds);
}
}
else {
// Remove entity from index.
$this->database
->delete('linkchecker_index')
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->execute();
}
// Get list of link IDs that should be deleted.
$query = $storage
->getQuery();
$query
->condition('entity_id.target_id', $entity
->id());
$query
->condition('entity_id.target_type', $entity
->getEntityTypeId());
if (!empty($extractedIds)) {
$query
->condition('lid', $extractedIds, 'NOT IN');
}
$ids = $query
->execute();
if (!empty($ids)) {
// Delete the links.
$linksToDelete = $storage
->loadMultiple($ids);
$storage
->delete($linksToDelete);
}
}