Skip to content

Commit

Permalink
TASK: Use ProperyMapper to convert creation dialog properties
Browse files Browse the repository at this point in the history
if their type differs from the one defined in the NodeType definition
  • Loading branch information
bwaidelich committed Jan 23, 2018
1 parent c49c456 commit c60a7fd
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Classes/NodeCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@
namespace Wwwision\Neos\CreationDialogProperties;

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Property\PropertyMapper;
use Neos\Flow\Property\TypeConverter\PersistentObjectConverter;
use Neos\Neos\Ui\NodeCreationHandler\NodeCreationHandlerInterface;
use Neos\Utility\TypeHandling;

/**
* A Node Creation Handler that takes the incoming data from the Creation Dialog and sets the corresponding node property
*/
class NodeCreationHandler implements NodeCreationHandlerInterface
{

/**
* @Flow\Inject
* @var PropertyMapper
*/
protected $propertyMapper;

/**
* @param NodeInterface $node The newly created node
* @param array $data incoming data from the creationDialog
* @return void
*/
public function handle(NodeInterface $node, array $data)
{
$propertyMappingConfiguration = $this->propertyMapper->buildPropertyMappingConfiguration();
$propertyMappingConfiguration->forProperty('*')->allowAllProperties();
$propertyMappingConfiguration->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED, true);

foreach ($data as $propertyName => $propertyValue) {
$propertyType = TypeHandling::normalizeType($node->getNodeType()->getPropertyType($propertyName));
if ($propertyType !== 'references' && $propertyType !== 'reference' && $propertyType !== TypeHandling::getTypeForValue($propertyValue)) {
$propertyValue = $this->propertyMapper->convert($propertyValue, $propertyType, $propertyMappingConfiguration);
}
$node->setProperty($propertyName, $propertyValue);

}
}
}

0 comments on commit c60a7fd

Please sign in to comment.