Skip to content

Commit

Permalink
EZP-29104: Implemented ImageAsset Field Type (ezsystems#2403)
Browse files Browse the repository at this point in the history
* 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
adamwojs authored and alongosz committed Sep 11, 2018
1 parent 895cca0 commit 4892d15
Show file tree
Hide file tree
Showing 31 changed files with 2,164 additions and 1 deletion.
@@ -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]
);
}
}
}
1 change: 1 addition & 0 deletions eZ/Bundle/EzPublishCoreBundle/EzPublishCoreBundle.php
Expand Up @@ -115,6 +115,7 @@ public function getContainerExtension()
new ConfigParser\Common(),
new ConfigParser\Content(),
new ConfigParser\FieldType\RichText(),
new ConfigParser\FieldType\ImageAsset(),
new ConfigParser\FieldTemplates(),
new ConfigParser\FieldEditTemplates(),
new ConfigParser\FieldDefinitionSettingsTemplates(),
Expand Down
@@ -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;
}
}
Expand Up @@ -18,13 +18,21 @@ parameters:
ezplatform.default_view_templates.content.text_linked: 'EzPublishCoreBundle:default:content/text_linked.html.twig'
ezplatform.default_view_templates.content.embed: 'EzPublishCoreBundle:default:content/embed.html.twig'
ezplatform.default_view_templates.content.embed_image: 'EzPublishCoreBundle:default:content/embed_image.html.twig'
ezplatform.default_view_templates.content.asset_image: 'EzPublishCoreBundle:default:content/asset_image.html.twig'
ezplatform.default_view_templates.block: 'EzPublishCoreBundle:default:block/block.html.twig'

# Rich Text Custom Tags global configuration
ezplatform.ezrichtext.custom_tags: {}
# Rich Text Custom Tags default scope (for SiteAccess) configuration
ezsettings.default.fieldtypes.ezrichtext.custom_tags: []

# Image Asset mappings
ezsettings.default.fieldtypes.ezimageasset.mappings:
content_type_identifier: image
content_field_identifier: image
name_field_identifier: name
parent_location_id: 51

ezsettings.default.pagelayout: 'EzPublishCoreBundle::pagelayout.html.twig'

# List of content type identifiers to display as image when embedded
Expand All @@ -51,6 +59,10 @@ parameters:
default:
template: "%ezplatform.default_view_templates.content.embed%"
match: []
asset_image:
default:
template: '%ezplatform.default_view_templates.content.asset_image%'
match: []

ezsettings.default.block_view_defaults:
block:
Expand Down
Expand Up @@ -91,6 +91,15 @@ services:
tags:
- {name: ezpublish.fieldType.parameterProvider, alias: ezobjectrelationlist}

ezpublish.fieldType.ezimageasset.parameterProvider:
class: \eZ\Publish\Core\MVC\Symfony\FieldType\ImageAsset\ParameterProvider
arguments:
- "@ezpublish.api.service.content"
- "@ezpublish.api.service.inner_field_type"
- "@eZ\\Publish\\Core\\FieldType\\ImageAsset\\AssetMapper"
tags:
- {name: ezpublish.fieldType.parameterProvider, alias: ezimageasset}

# Page
ezpublish.fieldType.ezpage.pageService.factory:
class: "%ezpublish.fieldType.ezpage.pageService.factory.class%"
Expand Down Expand Up @@ -285,3 +294,17 @@ services:
eZ\Publish\Core\FieldType\RichText\CustomTagsValidator:
public: false
arguments: ['%ezplatform.ezrichtext.custom_tags%']

eZ\Publish\Core\FieldType\ImageAsset\NameableField:
arguments:
$handler: '@ezpublish.spi.persistence.cache.contentHandler'
tags:
- {name: ezpublish.fieldType.nameable, alias: ezimageasset}

eZ\Publish\Core\FieldType\ImageAsset\AssetMapper:
arguments:
$contentService: '@ezpublish.api.service.content'
$locationService: '@ezpublish.api.service.location'
$contentTypeService: '@ezpublish.api.service.content_type'
$mappings: '$fieldtypes.ezimageasset.mappings$'

9 changes: 9 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/image.yml
Expand Up @@ -129,6 +129,15 @@ services:
- '@ezpublish.fieldType.ezimage.io_service'
public: false

ezpublish.image_alias.imagine.alias_generator.image_asset:
class: 'eZ\Bundle\EzPublishCoreBundle\Imagine\ImageAsset\AliasGenerator'
decorates: 'ezpublish.image_alias.imagine.alias_generator'
arguments:
- '@ezpublish.image_alias.imagine.alias_generator.image_asset.inner'
- '@ezpublish.api.service.content'
- '@eZ\Publish\Core\FieldType\ImageAsset\AssetMapper'
public: false

ezpublish.image_alias.imagine.placeholder_provider.registry:
class: 'eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProviderRegistry'

Expand Down
Expand Up @@ -190,6 +190,7 @@ services:
class: "%ezpublish.twig.extension.image.class%"
arguments:
- '@ezpublish.fieldType.ezimage.variation_service'
- '@eZ\Publish\Core\FieldType\ImageAsset\AssetMapper'
tags:
- { name: twig.extension }

Expand Down
Expand Up @@ -56,6 +56,11 @@
<target>Image</target>
<note>key: ezimage.name</note>
</trans-unit>
<trans-unit id="3afdca17c734ecb3a077bcc4ba7f75a7544e845d" resname="ezimageasset.name">
<source>Image Asset</source>
<target>Image Asset</target>
<note>key: ezimageasset.name</note>
</trans-unit>
<trans-unit id="3b0f44cd332c71e78209a1a4b235bc6a1e6d374b" resname="ezinteger.name">
<source>Integer</source>
<target>Integer</target>
Expand Down
Expand Up @@ -430,6 +430,24 @@
{% endspaceless %}
{% endblock %}

{% block ezimageasset_field %}
{% spaceless %}
{% if not ez_is_field_empty(content, field) and parameters.available %}
{{ fos_httpcache_tag('relation-' ~ field.value.destinationContentId) }}
<div {{ block('field_attributes') }}>
{{ render(controller('ez_content:viewAction', {
contentId: field.value.destinationContentId,
viewType: 'asset_image',
noLayout: true,
params: {
parameters: parameters|default({'alias': 'original'})
}
}))}}
</div>
{% endif %}
{% endspaceless %}
{% endblock %}

{% block ezobjectrelation_field %}
{% spaceless %}
{% if not ez_is_field_empty( content, field ) and parameters.available %}
Expand Down
@@ -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 %}
Expand Up @@ -410,3 +410,6 @@
<div class="ez-fielddefinition-setting-value">{{ isISBN13 ? 'ISBN-13' : 'ISBN-10' }}</div>
</li>
{% endblock %}

{% block ezimageasset_settings %}
{% endblock %}
@@ -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,
],
],
],
];
}
}

0 comments on commit 4892d15

Please sign in to comment.