Skip to content
Snippets Groups Projects
Verified Commit 6599e8f0 authored by nterbogt's avatar nterbogt Committed by Lee Rowlands
Browse files

Issue #3368136 by nterbogt: Possible performance improvement

parent 6cc88d4d
No related branches found
No related tags found
No related merge requests found
...@@ -113,23 +113,31 @@ class ValidHierarchyReferenceConstraintValidator extends ConstraintValidator imp ...@@ -113,23 +113,31 @@ class ValidHierarchyReferenceConstraintValidator extends ConstraintValidator imp
/** @var \PNX\NestedSet\Storage\DbalNestedSet $storage */ /** @var \PNX\NestedSet\Storage\DbalNestedSet $storage */
$storage = $this->nestedSetStorageFactory->get($value->getFieldDefinition()->getFieldStorageDefinition()->getName(), $target_type); $storage = $this->nestedSetStorageFactory->get($value->getFieldDefinition()->getFieldStorageDefinition()->getName(), $target_type);
$descendant_nested_set_nodes = $storage->findDescendants($thisNode); $descendant_nested_set_nodes = $storage->findDescendants($thisNode);
$descendant_nested_set_node_ids = array_map(function (Node $node) {
return $node->getId(); // Cannot reference self.
}, $descendant_nested_set_nodes); $children = [$this_entity->id()];
$descendant_entities = $this->entityTypeManager->getStorage($this_entity->getEntityTypeId())->loadMultiple($descendant_nested_set_node_ids); if (!empty($descendant_nested_set_nodes)) {
$children = []; $descendant_nested_set_node_ids = array_map(function (Node $node) {
foreach ($descendant_nested_set_nodes as $descendant_nested_set_node) { return $node->getId();
$node_id = $descendant_nested_set_node->getId(); }, $descendant_nested_set_nodes);
$entity = $descendant_entities[$node_id] ?? FALSE; $has_revisions = $this_entity->getEntityType()->hasKey('revision');
if (!$entity || ($entity->getEntityType()->hasKey('revision') && $descendant_nested_set_node->getRevisionId() != $entity->getRevisionId())) { $descendant_entities = \Drupal::entityQuery($this_entity->getEntityTypeId())
// Bypass non default revisions and deleted items. ->condition($this_entity->getEntityType()->getKey('id'), $descendant_nested_set_node_ids, 'IN')
continue; ->accessCheck(false)
->execute();
$descendant_entities = array_flip($descendant_entities);
foreach ($descendant_nested_set_nodes as $descendant_nested_set_node) {
$node_id = $descendant_nested_set_node->getId();
if (!isset($descendant_entities[$node_id])) {
continue;
}
if ($has_revisions && $descendant_nested_set_node->getRevisionId() != $descendant_entities[$node_id]) {
continue;
}
$children[] = $node_id;
} }
$children[] = $node_id; $children = array_unique($children);
} }
$children = array_unique($children);
// Cannot reference self either.
$children[] = $this_entity->id();
// Add violations on deltas with a target_id that is not valid. // Add violations on deltas with a target_id that is not valid.
if ($target_ids && $children) { if ($target_ids && $children) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment