Neos\ContentRepository\Domain\Service\Context::adoptNode PHP Method

adoptNode() public method

Checks if a node variant matching the exact dimensions already exists for this context and return it if found. Otherwise a new node variant for this context is created. In case the node already exists in the context but does not match the target dimensions a new, more specific node is created and returned.
public adoptNode ( Neos\ContentRepository\Domain\Model\NodeInterface $node, boolean $recursive = false ) : Neos\ContentRepository\Domain\Model\NodeInterface
$node Neos\ContentRepository\Domain\Model\NodeInterface The node with a different context. If the context of the given node is the same as this context the operation will have no effect.
$recursive boolean If TRUE also adopt all descendant nodes which are non-aggregate
return Neos\ContentRepository\Domain\Model\NodeInterface A new or existing node that matches this context
    public function adoptNode(NodeInterface $node, $recursive = false)
    {
        if ($node->getContext() === $this && $node->dimensionsAreMatchingTargetDimensionValues()) {
            return $node;
        }
        $this->emitBeforeAdoptNode($node, $this, $recursive);
        $existingNode = $this->getNodeByIdentifier($node->getIdentifier());
        if ($existingNode !== null) {
            if ($existingNode->dimensionsAreMatchingTargetDimensionValues()) {
                $adoptedNode = $existingNode;
            } else {
                $adoptedNode = $existingNode->createVariantForContext($this);
            }
        } else {
            $adoptedNode = $node->createVariantForContext($this);
        }
        $this->firstLevelNodeCache->setByIdentifier($adoptedNode->getIdentifier(), $adoptedNode);
        if ($recursive) {
            $childNodes = $node->getChildNodes();
            /** @var NodeInterface $childNode */
            foreach ($childNodes as $childNode) {
                if (!$childNode->getNodeType()->isAggregate()) {
                    $this->adoptNode($childNode, true);
                }
            }
        }
        $this->emitAfterAdoptNode($node, $this, $recursive);
        return $adoptedNode;
    }