Created
November 18, 2020 18:29
-
-
Save lorenzulrich/ec4b84b6bd3e6f5e93d8ace79a66ae65 to your computer and use it in GitHub Desktop.
Custom SetPropertyUsingEel transformation for Neos CMS Node Migrations
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 | |
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); | |
} | |
} |
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
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