Skip to content

Commit

Permalink
FEATURE: Render copyrightNotice from configured template
Browse files Browse the repository at this point in the history
Renders and returns the copyright notice according to
unsplashs guidelines. This data is stored in the asset
model in Neos >= 4.2
  • Loading branch information
daniellienert committed Nov 5, 2018
1 parent a8aff47 commit 71d54ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
29 changes: 28 additions & 1 deletion Classes/AssetSource/UnsplashAssetProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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);
}
}
17 changes: 16 additions & 1 deletion Classes/AssetSource/UnsplashAssetSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ final class UnsplashAssetSource implements AssetSourceInterface
*/
private $assetProxyRepository;

/**
* @var string
*/
private $copyRightNoticeTemplate;

/**
* UnsplashAssetSource constructor.
* @param string $assetSourceIdentifier
Expand All @@ -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([
Expand All @@ -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
{
Expand All @@ -79,6 +86,14 @@ public function getLabel(): string
return 'Unsplash';
}

/**
* @return string
*/
public function getCopyRightNoticeTemplate(): string
{
return $this->copyRightNoticeTemplate;
}

/**
* @return AssetProxyRepositoryInterface
*/
Expand Down
1 change: 1 addition & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Neos:
assetSourceOptions:
accessKey: your-access-key
utmSource: 'Neos CMS Unsplash Asset Source https://neos.io'
copyRightNoticeTemplate: '${"Photo by <a href=\"https://unsplash.com/" + user.username + "\">" + user.name + "</a> on <a href=\"https://unsplash.com\">Unsplash</a>"}'

0 comments on commit 71d54ab

Please sign in to comment.