From 71d54ab2dc9d054ace964a4b92f7a0329a2212f2 Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Mon, 5 Nov 2018 22:35:26 +0100 Subject: [PATCH] FEATURE: Render copyrightNotice from configured template Renders and returns the copyright notice according to unsplashs guidelines. This data is stored in the asset model in Neos >= 4.2 --- Classes/AssetSource/UnsplashAssetProxy.php | 29 ++++++++++++++++++++- Classes/AssetSource/UnsplashAssetSource.php | 17 +++++++++++- Configuration/Settings.yaml | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Classes/AssetSource/UnsplashAssetProxy.php b/Classes/AssetSource/UnsplashAssetProxy.php index 3a0caaa..9e53908 100644 --- a/Classes/AssetSource/UnsplashAssetProxy.php +++ b/Classes/AssetSource/UnsplashAssetProxy.php @@ -10,6 +10,8 @@ */ use Neos\Flow\Annotations as Flow; +use Neos\Eel\EelEvaluatorInterface; +use Neos\Eel\Utility; use Behat\Transliterator\Transliterator; use Crew\Unsplash\Photo; use Neos\Flow\Http\Uri; @@ -43,6 +45,18 @@ final class UnsplashAssetProxy implements AssetProxyInterface, HasRemoteOriginal */ private $iptcProperties; + /** + * @var array + * @Flow\InjectConfiguration(path="defaultContext", package="Neos.Fusion") + */ + protected $defaultContextConfiguration; + + /** + * @var EelEvaluatorInterface + * @Flow\Inject(lazy=false) + */ + protected $eelEvaluator; + /** * UnsplashAssetProxy constructor. * @param Photo $photo @@ -230,6 +244,7 @@ protected function getImageUrl(string $size): string * * @param string $propertyName * @return bool + * @throws \Neos\Eel\Exception */ public function hasIptcProperty(string $propertyName): bool { @@ -241,6 +256,7 @@ public function hasIptcProperty(string $propertyName): bool * * @param string $propertyName * @return string + * @throws \Neos\Eel\Exception */ public function getIptcProperty(string $propertyName): string { @@ -251,17 +267,28 @@ public function getIptcProperty(string $propertyName): string * Returns all known IPTC metadata properties as key => value (e.g. "Title" => "My Photo") * * @return array + * @throws \Neos\Eel\Exception */ public function getIptcProperties(): array { if ($this->iptcProperties === null) { $this->iptcProperties = [ 'Title' => $this->getLabel(), - 'CopyrightNotice' => sprintf('Photo by %s on Unsplash', $this->photo->user['name']), + 'CopyrightNotice' => $this->compileCopyrightNotice($this->photo->user), 'Creator' => $this->photo->user['name'] ]; } return $this->iptcProperties; } + + /** + * @param array $userProperties + * @return string + * @throws \Neos\Eel\Exception + */ + protected function compileCopyrightNotice(array $userProperties): string + { + return Utility::evaluateEelExpression($this->assetSource->getCopyRightNoticeTemplate(), $this->eelEvaluator, ['user' => $userProperties], $this->defaultContextConfiguration); + } } diff --git a/Classes/AssetSource/UnsplashAssetSource.php b/Classes/AssetSource/UnsplashAssetSource.php index ad455a6..8a1532d 100644 --- a/Classes/AssetSource/UnsplashAssetSource.php +++ b/Classes/AssetSource/UnsplashAssetSource.php @@ -27,6 +27,11 @@ final class UnsplashAssetSource implements AssetSourceInterface */ private $assetProxyRepository; + /** + * @var string + */ + private $copyRightNoticeTemplate; + /** * UnsplashAssetSource constructor. * @param string $assetSourceIdentifier @@ -36,9 +41,10 @@ final class UnsplashAssetSource implements AssetSourceInterface public function __construct(string $assetSourceIdentifier, array $assetSourceOptions) { $this->assetSourceIdentifier = $assetSourceIdentifier; + $this->copyRightNoticeTemplate = $assetSourceOptions['copyRightNoticeTemplate'] ?? ''; if (!isset($assetSourceOptions['accessKey']) || trim($assetSourceOptions['accessKey']) === '') { - throw new InvalidConfigurationException(sprintf('Access Key for the %s data source not set.',$this->getLabel()), 1526326192); + throw new InvalidConfigurationException(sprintf('Access Key for the %s data source not set.', $this->getLabel()), 1526326192); } Unsplash\HttpClient::init([ @@ -54,6 +60,7 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt * @param string $assetSourceIdentifier * @param array $assetSourceOptions * @return AssetSourceInterface + * @throws InvalidConfigurationException */ public static function createFromConfiguration(string $assetSourceIdentifier, array $assetSourceOptions): AssetSourceInterface { @@ -79,6 +86,14 @@ public function getLabel(): string return 'Unsplash'; } + /** + * @return string + */ + public function getCopyRightNoticeTemplate(): string + { + return $this->copyRightNoticeTemplate; + } + /** * @return AssetProxyRepositoryInterface */ diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 8ecb410..3b652f2 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -6,3 +6,4 @@ Neos: assetSourceOptions: accessKey: your-access-key utmSource: 'Neos CMS Unsplash Asset Source https://neos.io' + copyRightNoticeTemplate: '${"Photo by " + user.name + " on Unsplash"}'