Neos\ContentRepository\Utility::buildAutoCreatedChildNodeIdentifier PHP Метод

buildAutoCreatedChildNodeIdentifier() публичный статический Метод

This is needed if multiple node variants are created through "createNode" with different dimension values. If child nodes with the same path and different identifiers exist, bad things can happen.
public static buildAutoCreatedChildNodeIdentifier ( string $childNodeName, string $identifier ) : string
$childNodeName string
$identifier string Identifier of the node where the child node should be created
Результат string The generated UUID like identifier
    public static function buildAutoCreatedChildNodeIdentifier($childNodeName, $identifier)
    {
        $hex = md5($identifier . '-' . $childNodeName);
        return substr($hex, 0, 8) . '-' . substr($hex, 8, 4) . '-' . substr($hex, 12, 4) . '-' . substr($hex, 16, 4) . '-' . substr($hex, 20, 12);
    }

Usage Example

 /**
  * Create missing child nodes for the given node type
  *
  * @param NodeType $nodeType
  * @param string $workspaceName
  * @param boolean $dryRun
  * @return void
  */
 protected function createChildNodesByNodeType(NodeType $nodeType, $workspaceName, $dryRun)
 {
     $createdNodesCount = 0;
     $updatedNodesCount = 0;
     $nodeCreationExceptions = 0;
     $nodeIdentifiersWhichNeedUpdate = [];
     $nodeTypes = $this->nodeTypeManager->getSubNodeTypes($nodeType->getName(), false);
     $nodeTypes[$nodeType->getName()] = $nodeType;
     if ($this->nodeTypeManager->hasNodeType((string) $nodeType)) {
         $nodeType = $this->nodeTypeManager->getNodeType((string) $nodeType);
         $nodeTypeNames[$nodeType->getName()] = $nodeType;
     } else {
         $this->output->outputLine('Node type "%s" does not exist', array((string) $nodeType));
         exit(1);
     }
     /** @var $nodeType NodeType */
     foreach ($nodeTypes as $nodeTypeName => $nodeType) {
         $childNodes = $nodeType->getAutoCreatedChildNodes();
         foreach ($this->getNodeDataByNodeTypeAndWorkspace($nodeTypeName, $workspaceName) as $nodeData) {
             $context = $this->nodeFactory->createContextMatchingNodeData($nodeData);
             $node = $this->nodeFactory->createFromNodeData($nodeData, $context);
             if (!$node instanceof NodeInterface) {
                 continue;
             }
             foreach ($childNodes as $childNodeName => $childNodeType) {
                 try {
                     $childNode = $node->getNode($childNodeName);
                     $childNodeIdentifier = Utility::buildAutoCreatedChildNodeIdentifier($childNodeName, $node->getIdentifier());
                     if ($childNode === null) {
                         if ($dryRun === false) {
                             $node->createNode($childNodeName, $childNodeType, $childNodeIdentifier);
                             $this->output->outputLine('Auto created node named "%s" in "%s"', array($childNodeName, $node->getPath()));
                         } else {
                             $this->output->outputLine('Missing node named "%s" in "%s"', array($childNodeName, $node->getPath()));
                         }
                         $createdNodesCount++;
                     } elseif ($childNode->getIdentifier() !== $childNodeIdentifier) {
                         $nodeIdentifiersWhichNeedUpdate[$childNode->getIdentifier()] = $childNodeIdentifier;
                     }
                 } catch (\Exception $exception) {
                     $this->output->outputLine('Could not create node named "%s" in "%s" (%s)', array($childNodeName, $node->getPath(), $exception->getMessage()));
                     $nodeCreationExceptions++;
                 }
             }
         }
     }
     if (count($nodeIdentifiersWhichNeedUpdate) > 0) {
         if ($dryRun === false) {
             foreach ($nodeIdentifiersWhichNeedUpdate as $oldNodeIdentifier => $newNodeIdentifier) {
                 $queryBuilder = $this->entityManager->createQueryBuilder();
                 $queryBuilder->update(NodeData::class, 'n')->set('n.identifier', $queryBuilder->expr()->literal($newNodeIdentifier))->where('n.identifier = ?1')->setParameter(1, $oldNodeIdentifier);
                 $result = $queryBuilder->getQuery()->getResult();
                 $updatedNodesCount++;
                 $this->output->outputLine('Updated node identifier from %s to %s because it was not a "stable" identifier', [$oldNodeIdentifier, $newNodeIdentifier]);
             }
         } else {
             foreach ($nodeIdentifiersWhichNeedUpdate as $oldNodeIdentifier => $newNodeIdentifier) {
                 $this->output->outputLine('Child nodes with identifier "%s" need to change their identifier to "%s"', [$oldNodeIdentifier, $newNodeIdentifier]);
                 $updatedNodesCount++;
             }
         }
     }
     if ($createdNodesCount !== 0 || $nodeCreationExceptions !== 0 || $updatedNodesCount !== 0) {
         if ($dryRun === false) {
             if ($createdNodesCount > 0) {
                 $this->output->outputLine('Created %s new child nodes', array($createdNodesCount));
             }
             if ($updatedNodesCount > 0) {
                 $this->output->outputLine('Updated identifier of %s child nodes', array($updatedNodesCount));
             }
             if ($nodeCreationExceptions > 0) {
                 $this->output->outputLine('%s Errors occurred during child node creation', array($nodeCreationExceptions));
             }
             $this->persistenceManager->persistAll();
         } else {
             if ($createdNodesCount > 0) {
                 $this->output->outputLine('%s missing child nodes need to be created', array($createdNodesCount));
             }
             if ($updatedNodesCount > 0) {
                 $this->output->outputLine('%s identifiers of child nodes need to be updated', array($updatedNodesCount));
             }
         }
     }
 }
All Usage Examples Of Neos\ContentRepository\Utility::buildAutoCreatedChildNodeIdentifier