/** * Copy $node before, into or after $targetNode * * @param NodeInterface $node the node to be copied * @param NodeInterface $targetNode the target node to be copied "to", see $position * @param string $position where the node should be added in relation to $targetNode (allowed: before, into, after) * @param string $nodeName optional node name (if empty random node name will be generated) * @return NodeInterface The copied node * @throws NodeException */ public function copy(NodeInterface $node, NodeInterface $targetNode, $position, $nodeName = null) { if (!in_array($position, array('before', 'into', 'after'), true)) { throw new NodeException('The position should be one of the following: "before", "into", "after".', 1346832303); } $nodeName = $this->nodeService->generateUniqueNodeName($this->getDesignatedParentNode($targetNode, $position)->getPath(), !empty($nodeName) ? $nodeName : null); switch ($position) { case 'before': $copiedNode = $node->copyBefore($targetNode, $nodeName); break; case 'after': $copiedNode = $node->copyAfter($targetNode, $nodeName); break; case 'into': default: $copiedNode = $node->copyInto($targetNode, $nodeName); } return $copiedNode; }
/** * Creates and persists a node from the given $nodeTemplate as child node * * @param NodeTemplate $nodeTemplate * @param string $nodeName name of the new node. If not specified the name of the nodeTemplate will be used. * @param Workspace $workspace * @param array $dimensions * @return NodeData the freshly generated node */ public function createNodeDataFromTemplate(NodeTemplate $nodeTemplate, $nodeName = null, Workspace $workspace = null, array $dimensions = null) { $newNodeName = $nodeName !== null ? $nodeName : $nodeTemplate->getName(); $possibleNodeName = $this->nodeService->generateUniqueNodeName($this->getPath(), $newNodeName); $newNodeData = $this->createNodeData($possibleNodeName, $nodeTemplate->getNodeType(), $nodeTemplate->getIdentifier(), $workspace, $dimensions); $newNodeData->similarize($nodeTemplate); return $newNodeData; }