Skip to content

Instantly share code, notes, and snippets.

@jobee
Last active January 15, 2019 08:39
Show Gist options
  • Save jobee/500c9ebe2aa7dcb77eed662abbadba0d to your computer and use it in GitHub Desktop.
Save jobee/500c9ebe2aa7dcb77eed662abbadba0d to your computer and use it in GitHub Desktop.
Example node creation handler for Neos that converts the given Asset Identifier into an Asset Object
<?php
namespace UseYourOwn\Namespace\NodeCreationHandler;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Property\PropertyMapper;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Neos\Ui\NodeCreationHandler\NodeCreationHandlerInterface;
class ExampleNodeCreationHandler implements NodeCreationHandlerInterface
{
/**
* Handle creation dialog asset
*
* @param NodeInterface $node The newly created node
* @param array $data incoming data from the creationDialog
* @return void
*/
public function handle(NodeInterface $node, array $data)
{
if (isset($data['assetPropertyName'])) {
try {
// If you just want to set the asset as a nodes property
$node->setProperty('assetPropertyName', $data['assetPropertyName']);
// If you need to get the resource uri for some reason
$propertyMapper = new PropertyMapper();
$assetObject = $propertyMapper->convert($data['assetPropertyName'], Asset::class);
if ($assetObject instanceof AssetInterface) {
$resourceManager = new ResourceManager();
$assetResourceUri = $resourceManager->getPublicPersistentResourceUri($assetObject->getResource());
// Do whatever you want with $assetResourceUri
// ...
}
} catch (\Exception $e) {
// Log exception or do whatever is needed...
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment