Skip to content

Commit

Permalink
✨ Add migration for flat structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto authored and Jon Uhlmann committed Aug 23, 2020
1 parent f6f7836 commit 115c789
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Jonnitto\ImagesInARow\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 collections to the container
*/
class FlatStructureTransformation extends AbstractTransformation
{
use CreateContentContextTrait;

/**
* @param NodeData $node
* @return boolean
*/
public function isTransformable(NodeData $node)
{
$numberOfChildNodes = $node->getNumberOfChildNodes('Neos.Neos:ContentCollection', $node->getWorkspace(), $node->getDimensions());
return ($numberOfChildNodes > 0);
}

/**
* @param NodeData $node
* @return void
*/
public function execute(NodeData $node)
{
$contentContext = $this->createContentContext('live', []);
$containerNode = $contentContext->getNodeByIdentifier($node->getIdentifier());
$contentCollections = $containerNode->getChildNodes('Neos.Neos:ContentCollection');

foreach ($contentCollections as $contentCollection) {
/** @var NodeInterface $contentCollection */
if ($contentCollection->hasChildNodes()) {
$this->moveChildNodes($contentCollection->getChildNodes(), $containerNode);
$contentCollection->remove();
}
}
}

/**
* @param array $children
* @param NodeInterface $container
*/
protected function moveChildNodes(array $children, NodeInterface $container)
{
foreach ($children as $childNode) {
if ($childNode instanceof NodeInterface) {
/** @var NodeInterface $childNode */
$childNode->moveInto($container);
}
}
}
}
12 changes: 12 additions & 0 deletions Migrations/ContentRepository/Version20200823164840.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
up:
comments: 'Migrate content collection of images to a flat structure'
migration:
- filters:
- type: 'NodeType'
settings:
nodeType: 'Jonnitto.ImagesInARow:Container'
transformations:
- type: 'Jonnitto\ImagesInARow\ContentRepository\Transformations\FlatStructureTransformation'
settings: []
down:
comments: 'No down migration available'

0 comments on commit 115c789

Please sign in to comment.