Skip to content

Instantly share code, notes, and snippets.

@lorenzulrich
Created November 18, 2020 18:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenzulrich/ec4b84b6bd3e6f5e93d8ace79a66ae65 to your computer and use it in GitHub Desktop.
Save lorenzulrich/ec4b84b6bd3e6f5e93d8ace79a66ae65 to your computer and use it in GitHub Desktop.
Custom SetPropertyUsingEel transformation for Neos CMS Node Migrations
<?php
namespace My\FoobarCom\Migration\Transformations;
use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Factory\NodeFactory;
use Neos\ContentRepository\Domain\Service\ContextFactory;
use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\ContentRepository\Migration\Transformations\AbstractTransformation;
use Neos\Eel\EelEvaluatorInterface;
use Neos\Eel\Utility;
/**
* Set a property to the value evaluated by an EEL expression
*/
class SetPropertyUsingEel extends AbstractTransformation
{
/**
* @var array
* @Flow\InjectConfiguration(path="defaultContext", package="Neos.Fusion")
*/
protected $defaultContextConfiguration;
/**
* @var EelEvaluatorInterface
* @Flow\Inject(lazy=false)
*/
protected $eelEvaluator;
/**
* @var ContextFactory
* @Flow\Inject
*/
protected $contextFactory;
/**
* @var NodeFactory
* @Flow\Inject
*/
protected $nodeFactory;
/**
* @var string
*/
protected $propertyName;
/**
* @var string
*/
protected $value;
/**
* Sets the name of the new property to be added.
*
* @param string $propertyName
* @return void
*/
public function setPropertyName($propertyName)
{
$this->propertyName = $propertyName;
}
/**
* Property value to be set.
*
* @param string $value
* @return void
*/
public function setValue($value)
{
$this->value = $value;
}
/**
* @param NodeData $node
* @return boolean
*/
public function isTransformable(NodeData $node)
{
return true;
}
/**
* Add the new property with the given value on the given node.
*
* @param NodeData $nodeData
* @return void
* @throws \Neos\ContentRepository\Exception\NodeConfigurationException
* @throws \Neos\Eel\Exception
*/
public function execute(NodeData $nodeData)
{
$context = $this->contextFactory->create(
[
'workspaceName' => 'live',
'invisibleContentShown' => true,
'removedContentShown' => true,
'inaccessibleContentShown' => true
]
);
$node = $this->nodeFactory->createFromNodeData($nodeData, $context);
$contextProperties = [
'workspaceName' => 'live',
'node' => $node,
'invisibleContentShown' => true,
'removedContentShown' => true,
'inaccessibleContentShown' => true
];
$value = Utility::evaluateEelExpression(
$this->value,
$this->eelEvaluator,
$contextProperties,
$this->defaultContextConfiguration
);
$nodeData->setProperty($this->propertyName, $value);
}
}
up:
comments: 'Migrates NodeType to NodeType'
warnings: 'There is no way of reverting this migration.'
migration:
-
filters:
-
type: 'NodeType'
settings:
nodeType: 'My.FoobarCom:NodeTypeToMigrate'
transformations:
-
type: 'My\FoobarCom\Migration\Transformations\SetPropertyUsingEel'
settings:
propertyName: 'title'
value: "${node ? q(node).find('[instanceof My.FoobarCom:SomeNodeType]').count() > 0 ? q(node).find('[instanceof My.FoobarCom:SomeNodeType]').first().property('text') : '' : ''}"
down:
warnings: 'There is no way of reverting this migration.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment