Created
June 22, 2021 14:21
-
-
Save Sebobo/9bac7fab0e4a934d1703e6ec5af05c9f to your computer and use it in GitHub Desktop.
Flatten transformation for Neos CMS Content Repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace My\Site\ContentRepository\Transformations; | |
use Neos\ContentRepository\Domain\Model\NodeInterface; | |
use Neos\ContentRepository\Domain\Model\NodeData; | |
use Neos\ContentRepository\Migration\Transformations\AbstractTransformation; | |
use Neos\Neos\Controller\CreateContentContextTrait; | |
/** | |
* Move content of a content collection to its parent | |
*/ | |
class FlattenTransformation extends AbstractTransformation | |
{ | |
use CreateContentContextTrait; | |
public function isTransformable(NodeData $node): bool | |
{ | |
$numberOfChildNodes = $node->getNumberOfChildNodes('Neos.Neos:ContentCollection', $node->getWorkspace(), $node->getDimensions()); | |
return ($numberOfChildNodes > 0); | |
} | |
public function execute(NodeData $node): void | |
{ | |
$contentContext = $this->createContextMatchingNodeData($node); | |
$parentNode = $contentContext->getNodeByIdentifier($node->getIdentifier()); | |
if (!$parentNode) { | |
return; | |
} | |
$contentCollections = $parentNode->getChildNodes('Neos.Neos:ContentCollection'); | |
foreach ($contentCollections as $contentCollection) { | |
if ($contentCollection->hasChildNodes()) { | |
$this->moveChildNodesToParent($contentCollection->getChildNodes(), $parentNode); | |
} | |
$contentCollection->remove(); | |
} | |
} | |
/** | |
* @param NodeInterface[] $children | |
* @param NodeInterface $parentNode | |
*/ | |
protected function moveChildNodesToParent(array $children, NodeInterface $parentNode): void | |
{ | |
foreach ($children as $childNode) { | |
$childNode->moveInto($parentNode); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment