protected function RevisionHandler::removeUnused in Feeds Paragraphs 8
Removes any unused entities.
Parameters
Paragraph[] $used_entities:
Paragraph[] $attached:
FieldConfigInterface $field:
1 call to RevisionHandler::removeUnused()
- RevisionHandler::cleanUp in src/
RevisionHandler.php - Cleans up the entity and its paragraphs before saving the update.
File
- src/
RevisionHandler.php, line 161
Class
Namespace
Drupal\feeds_para_mapperCode
protected function removeUnused($used_entities, $attached, $field) {
// Collect the entities that we should remove:
$toRemove = array();
for ($i = 0; $i < count($attached); $i++) {
if (!isset($used_entities[$i])) {
$toRemove[$i] = $attached[$i];
}
}
// Check that fields in common are not using any of the entities we intend to remove:
$targetInfo = $field
->get('target_info');
$in_common_fields = $targetInfo->in_common;
foreach ($in_common_fields as $in_common_field) {
foreach ($toRemove as $key => $paragraph) {
$values = $paragraph
->get($in_common_field['name'])
->getValue();
if (count($values)) {
unset($toRemove[$key]);
}
}
}
// Remove each paragraph id from its parent entity field value:
$parent = $attached[0]
->getParentEntity();
$parent_field = $attached[0]
->get('parent_field_name')
->getValue()[0]['value'];
$removed = 0;
foreach ($toRemove as $paragraph) {
$parent_values = $parent
->get($parent_field)
->getValue();
foreach ($parent_values as $index => $parent_value) {
if (isset($parent_value['target_id']) && $parent_value['target_id'] === $paragraph
->id()) {
$parent
->get($parent_field)
->removeItem($index);
$removed++;
}
}
}
if ($removed > 0) {
$this
->createRevision($parent);
}
}