-
Notifications
You must be signed in to change notification settings - Fork 27
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 (#246)
* EZP-29104: Implemented ImageAsset Field Type Co-authored-by: Dawid Parafiński <[email protected]>
- Loading branch information
Showing
4 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
lib/FieldType/DataTransformer/ImageAssetValueTransformer.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,63 @@ | ||
<?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 EzSystems\RepositoryForms\FieldType\DataTransformer; | ||
|
||
use eZ\Publish\Core\FieldType\ImageAsset\Value; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class ImageAssetValueTransformer extends AbstractBinaryBaseTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* @param \eZ\Publish\Core\FieldType\ImageAsset\Value|null $value | ||
* | ||
* @throws \Symfony\Component\Form\Exception\TransformationFailedException | ||
* | ||
* @return array|null | ||
*/ | ||
public function transform($value): ?array | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof Value) { | ||
throw new TransformationFailedException( | ||
sprintf('Expected a %s, got %s instead', Value::class, gettype($value)) | ||
); | ||
} | ||
|
||
return array_merge( | ||
$this->getDefaultProperties(), | ||
['destinationContentId' => $value->destinationContentId] | ||
); | ||
} | ||
|
||
/** | ||
* @param array|null $value | ||
* | ||
* @throws \Symfony\Component\Form\Exception\TransformationFailedException | ||
* | ||
* @return \eZ\Publish\Core\FieldType\ImageAsset\Value|null | ||
*/ | ||
public function reverseTransform($value): ?Value | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!is_array($value)) { | ||
throw new TransformationFailedException( | ||
sprintf('Expected a array, got %s instead', gettype($value)) | ||
); | ||
} | ||
|
||
return new Value($value['destinationContentId']); | ||
} | ||
} |
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,74 @@ | ||
<?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 EzSystems\RepositoryForms\FieldType\Mapper; | ||
|
||
use eZ\Publish\API\Repository\FieldTypeService; | ||
use eZ\Publish\Core\FieldType\ImageAsset\Value; | ||
use EzSystems\RepositoryForms\Data\Content\FieldData; | ||
use EzSystems\RepositoryForms\FieldType\DataTransformer\ImageAssetValueTransformer; | ||
use EzSystems\RepositoryForms\FieldType\FieldValueFormMapperInterface; | ||
use EzSystems\RepositoryForms\Form\Type\FieldType\ImageAssetFieldType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class ImageAssetFormMapper implements FieldValueFormMapperInterface | ||
{ | ||
/** @var \eZ\Publish\API\Repository\FieldTypeService */ | ||
private $fieldTypeService; | ||
|
||
/** | ||
* @param \eZ\Publish\API\Repository\FieldTypeService $fieldTypeService | ||
*/ | ||
public function __construct(FieldTypeService $fieldTypeService) | ||
{ | ||
$this->fieldTypeService = $fieldTypeService; | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\Form\FormInterface $fieldForm | ||
* @param \EzSystems\RepositoryForms\Data\Content\FieldData $data | ||
*/ | ||
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): void | ||
{ | ||
$fieldDefinition = $data->fieldDefinition; | ||
$formConfig = $fieldForm->getConfig(); | ||
$names = $fieldDefinition->getNames(); | ||
$fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier); | ||
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names); | ||
|
||
$fieldForm | ||
->add( | ||
$formConfig->getFormFactory()->createBuilder() | ||
->create( | ||
'value', | ||
ImageAssetFieldType::class, | ||
[ | ||
'required' => $fieldDefinition->isRequired, | ||
'label' => $label, | ||
] | ||
) | ||
->addModelTransformer(new ImageAssetValueTransformer($fieldType, $data->value, Value::class)) | ||
->setAutoInitialize(false) | ||
->getForm() | ||
); | ||
} | ||
|
||
/** | ||
* Fake method to set the translation domain for the extractor. | ||
* | ||
* @param OptionsResolver $resolver | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver | ||
->setDefaults([ | ||
'translation_domain' => 'ezrepoforms_content_type', | ||
]); | ||
} | ||
} |
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,131 @@ | ||
<?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 EzSystems\RepositoryForms\Form\Type\FieldType; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Exceptions\NotFoundException; | ||
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; | ||
use eZ\Publish\Core\FieldType\ImageAsset\AssetMapper; | ||
use EzSystems\RepositoryForms\ConfigResolver\MaxUploadSize; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | ||
use Symfony\Component\Form\Extension\Core\Type\FileType; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class ImageAssetFieldType extends AbstractType | ||
{ | ||
/** @var \eZ\Publish\API\Repository\ContentService */ | ||
private $contentService; | ||
|
||
/** @var \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper */ | ||
private $assetMapper; | ||
|
||
/** @var \EzSystems\RepositoryForms\ConfigResolver\MaxUploadSize */ | ||
private $maxUploadSize; | ||
|
||
/** | ||
* @param \eZ\Publish\API\Repository\ContentService $contentService | ||
* @param \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper $mapper | ||
* @param \EzSystems\RepositoryForms\ConfigResolver\MaxUploadSize $maxUploadSize | ||
*/ | ||
public function __construct(ContentService $contentService, AssetMapper $mapper, MaxUploadSize $maxUploadSize) | ||
{ | ||
$this->contentService = $contentService; | ||
$this->maxUploadSize = $maxUploadSize; | ||
$this->assetMapper = $mapper; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->getBlockPrefix(); | ||
} | ||
|
||
public function getBlockPrefix() | ||
{ | ||
return 'ezplatform_fieldtype_ezimageasset'; | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add('destinationContentId', HiddenType::class) | ||
->add( | ||
'remove', | ||
CheckboxType::class, | ||
[ | ||
'label' => /** @Desc("Remove") */ 'content.field_type.binary_base.remove', | ||
'mapped' => false, | ||
] | ||
) | ||
->add( | ||
'file', | ||
FileType::class, | ||
[ | ||
'label' => /** @Desc("File") */ 'content.field_type.binary_base.file', | ||
'required' => $options['required'], | ||
'constraints' => [ | ||
new Assert\File([ | ||
'maxSize' => $this->getMaxFileSize(), | ||
]), | ||
], | ||
'mapped' => false, | ||
] | ||
); | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options) | ||
{ | ||
$view->vars['destination_content'] = null; | ||
|
||
if ($view->vars['value']['destinationContentId']) { | ||
try { | ||
$content = $this->contentService->loadContent( | ||
$view->vars['value']['destinationContentId'] | ||
); | ||
|
||
if (!$content->contentInfo->isTrashed()) { | ||
$view->vars['destination_content'] = $content; | ||
} | ||
} catch (NotFoundException | UnauthorizedException $exception) { | ||
} | ||
} | ||
|
||
$view->vars['max_file_size'] = $this->getMaxFileSize(); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'translation_domain' => 'ezrepoforms_fieldtype', | ||
]); | ||
} | ||
|
||
/** | ||
* Returns max size of uploaded file in bytes. | ||
* | ||
* @return int | ||
*/ | ||
private function getMaxFileSize(): int | ||
{ | ||
$validatorConfiguration = $this->assetMapper | ||
->getAssetFieldDefinition() | ||
->getValidatorConfiguration(); | ||
|
||
$maxFileSize = $validatorConfiguration['FileSizeValidator']['maxFileSize']; | ||
if ($maxFileSize > 0) { | ||
return min($maxFileSize * 1024 * 1024, $this->maxUploadSize->get()); | ||
} | ||
|
||
return $this->maxUploadSize->get(); | ||
} | ||
} |