forked from ezsystems/ezpublish-kernel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-29104: Implemented ImageAsset Field Type (ezsystems#2403)
* EZP-29104: ImageAsset field type (tests) * EZP-29104: ImageAsset field type (implementation) * EZP-29104: Impl. \eZ\Publish\SPI\FieldType\Indexable for ezimageasset FT
- Loading branch information
Showing
31 changed files
with
2,164 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...dle/EzPublishCoreBundle/DependencyInjection/Configuration/Parser/FieldType/ImageAsset.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\FieldType; | ||
|
||
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\AbstractFieldTypeParser; | ||
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface; | ||
use eZ\Publish\Core\FieldType\ImageAsset\Type as ImageAssetFieldType; | ||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
|
||
class ImageAsset extends AbstractFieldTypeParser | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFieldTypeIdentifier(): string | ||
{ | ||
return ImageAssetFieldType::FIELD_TYPE_IDENTIFIER; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFieldTypeSemanticConfig(NodeBuilder $nodeBuilder): void | ||
{ | ||
$nodeBuilder | ||
->scalarNode('content_type_identifier') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('content_field_identifier') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('name_field_identifier') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('parent_location_id') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer): void | ||
{ | ||
$fieldTypeIdentifier = $this->getFieldTypeIdentifier(); | ||
|
||
if (isset($scopeSettings['fieldtypes'][$fieldTypeIdentifier])) { | ||
$contextualizer->setContextualParameter( | ||
"fieldtypes.{$fieldTypeIdentifier}.mappings", | ||
$currentScope, | ||
$scopeSettings['fieldtypes'][$fieldTypeIdentifier] | ||
); | ||
} | ||
} | ||
} |
This file contains 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
87 changes: 87 additions & 0 deletions
87
eZ/Bundle/EzPublishCoreBundle/Imagine/ImageAsset/AliasGenerator.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Bundle\EzPublishCoreBundle\Imagine\ImageAsset; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\API\Repository\Values\Content\VersionInfo; | ||
use eZ\Publish\Core\FieldType\ImageAsset\AssetMapper; | ||
use eZ\Publish\SPI\FieldType\Value; | ||
use eZ\Publish\SPI\Variation\Values\Variation; | ||
use eZ\Publish\SPI\Variation\VariationHandler; | ||
use eZ\Publish\Core\FieldType\ImageAsset\Value as ImageAssetValue; | ||
|
||
/** | ||
* Alias Generator Decorator allowing generate variations based on passed ImageAsset\Value. | ||
*/ | ||
class AliasGenerator implements VariationHandler | ||
{ | ||
/** | ||
* @var \eZ\Publish\SPI\Variation\VariationHandler | ||
*/ | ||
private $innerAliasGenerator; | ||
|
||
/** | ||
* @var \eZ\Publish\API\Repository\ContentService | ||
*/ | ||
private $contentService; | ||
|
||
/** | ||
* @var \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper | ||
*/ | ||
private $assetMapper; | ||
|
||
/** | ||
* @param \eZ\Publish\SPI\Variation\VariationHandler $innerAliasGenerator | ||
* @param \eZ\Publish\API\Repository\ContentService $contentService | ||
* @param \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper $assetMapper | ||
*/ | ||
public function __construct( | ||
VariationHandler $innerAliasGenerator, | ||
ContentService $contentService, | ||
AssetMapper $assetMapper) | ||
{ | ||
$this->innerAliasGenerator = $innerAliasGenerator; | ||
$this->contentService = $contentService; | ||
$this->assetMapper = $assetMapper; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = []): Variation | ||
{ | ||
if ($this->supportsValue($field->value)) { | ||
$destinationContent = $this->contentService->loadContent( | ||
$field->value->destinationContentId | ||
); | ||
|
||
return $this->innerAliasGenerator->getVariation( | ||
$this->assetMapper->getAssetField($destinationContent), | ||
$destinationContent->versionInfo, | ||
$variationName, | ||
$parameters | ||
); | ||
} | ||
|
||
return $this->innerAliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters); | ||
} | ||
|
||
/** | ||
* Returns TRUE if the value is supported by alias generator. | ||
* | ||
* @param \eZ\Publish\SPI\FieldType\Value $value | ||
* | ||
* @return bool | ||
*/ | ||
public function supportsValue(Value $value): bool | ||
{ | ||
return $value instanceof ImageAssetValue; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
7 changes: 7 additions & 0 deletions
7
eZ/Bundle/EzPublishCoreBundle/Resources/views/default/content/asset_image.html.twig
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% set image_field_identifier = ez_image_asset_content_field_identifier() %} | ||
|
||
{% if image_field_identifier is not null %} | ||
{{ ez_render_field(content, image_field_identifier, { | ||
parameters: parameters | ||
}) }} | ||
{% endif %} |
This file contains 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
86 changes: 86 additions & 0 deletions
86
...ishCoreBundle/Tests/DependencyInjection/Configuration/Parser/FieldType/ImageAssetTest.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Configuration\Parser\FieldType; | ||
|
||
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\FieldType\ImageAsset as ImageAssetConfigParser; | ||
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension; | ||
use eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Configuration\Parser\AbstractParserTestCase; | ||
|
||
class ImageAssetTest extends AbstractParserTestCase | ||
{ | ||
/** | ||
* @{@inheritdoc} | ||
*/ | ||
protected function getContainerExtensions() | ||
{ | ||
return [ | ||
new EzPublishCoreExtension([new ImageAssetConfigParser()]), | ||
]; | ||
} | ||
|
||
public function testDefaultImageAssetSettings() | ||
{ | ||
$this->load(); | ||
|
||
$this->assertConfigResolverParameterValue( | ||
'fieldtypes.ezimageasset.mappings', | ||
[ | ||
'content_type_identifier' => 'image', | ||
'content_field_identifier' => 'image', | ||
'name_field_identifier' => 'name', | ||
'parent_location_id' => 51, | ||
], | ||
'ezdemo_site' | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider imageAssetSettingsProvider | ||
*/ | ||
public function testImageAssetSettings(array $config, array $expected) | ||
{ | ||
$this->load( | ||
[ | ||
'system' => [ | ||
'ezdemo_site' => $config, | ||
], | ||
] | ||
); | ||
|
||
foreach ($expected as $key => $val) { | ||
$this->assertConfigResolverParameterValue($key, $val, 'ezdemo_site'); | ||
} | ||
} | ||
|
||
public function imageAssetSettingsProvider(): array | ||
{ | ||
return [ | ||
[ | ||
[ | ||
'fieldtypes' => [ | ||
'ezimageasset' => [ | ||
'content_type_identifier' => 'photo', | ||
'content_field_identifier' => 'file', | ||
'name_field_identifier' => 'title', | ||
'parent_location_id' => 68, | ||
], | ||
], | ||
], | ||
[ | ||
'fieldtypes.ezimageasset.mappings' => [ | ||
'content_type_identifier' => 'photo', | ||
'content_field_identifier' => 'file', | ||
'name_field_identifier' => 'title', | ||
'parent_location_id' => 68, | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.