Created
May 10, 2021 14:00
-
-
Save grebaldi/0d41580394052368b283c181906523a3 to your computer and use it in GitHub Desktop.
`Asset` Eel-Helper that converts asset identifiers to assets
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 declare(strict_types=1); | |
namespace Vendor\Site\Application\Helper; | |
use Neos\Flow\Annotations as Flow; | |
use Neos\Eel\ProtectedContextAwareInterface; | |
use Neos\Media\Domain\Model\Asset; | |
use Neos\Media\Domain\Repository\AssetRepository; | |
/** | |
* @Flow\Scope("singleton") | |
*/ | |
final class AssetHelper implements ProtectedContextAwareInterface | |
{ | |
/** | |
* @Flow\Inject | |
* @var AssetRepository | |
*/ | |
protected $assetRepository; | |
/** | |
* This method will ask the AssetRepository from Neos.Media for the | |
* asset that belongs to the given identifier. | |
* | |
* It accepts null or a string value as an identifier, so that no | |
* further checks need to be done in Fusion. | |
* | |
* @param null|string $identifier | |
* @return null|Asset | |
*/ | |
public function byIdentifier(?string $identifier): ?Asset | |
{ | |
if ($identifier !== null) { | |
return $this->assetRepository->findByIdentifier($identifier); | |
} | |
return null; | |
} | |
/** | |
* This method is required by the ProtectedContextAwareInterface and | |
* is supposed to ensure that Fusion cannot trigger any "harmuful" operations. | |
* | |
* This helper consists of only one actual method that doesn't do any | |
* relevant I/O work or has any other relevant side-effects, so we just | |
* return `true`. | |
* | |
* @param string $methodName | |
* @return void | |
*/ | |
public function allowsCallOfMethod($methodName) | |
{ | |
return true; | |
} | |
} |
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
prototype(Vendor.Site:MyAssetRenderer) < prototype(Neos.Fusion:Component) { | |
asset = ${Asset.byIdentifier(request.arguments.asset)} | |
renderer = afx` | |
<h1>{props.asset.title}</h1> | |
` | |
// | |
// This makes sure that this element is properly cached depending on | |
// the incoming `asset` GET parameter | |
// | |
@cache { | |
mode = 'dynamic' | |
entryIdentifier { | |
node = ${node} | |
} | |
entryDiscriminator = ${request.arguments.asset} | |
context { | |
1 = 'node' | |
2 = 'documentNode' | |
} | |
entryTags { | |
1 = ${Neos.Caching.nodeTag(node)} | |
} | |
} | |
} |
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
Neos: | |
Fusion: | |
defaultContext: | |
Asset: Vendor\Site\Application\Helper\AssetHelper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment