Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZEE-3350: Added support for saving additional image data from adminUI #36

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "ezplatform-bundle",
"require": {
"php": "^7.3",
"ext-json": "*",
"ezsystems/ezplatform-kernel": "^1.3@dev",
"symfony/dependency-injection": "^5.0",
"symfony/http-kernel": "^5.0",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/FieldType/DataTransformer/ImageValueTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function transform($value)

return array_merge(
$this->getDefaultProperties(),
['alternativeText' => $value->alternativeText]
[
'alternativeText' => $value->alternativeText,
'additionalData' => $value->additionalData,
]
);
}

Expand All @@ -53,6 +56,7 @@ public function reverseTransform($value)
}

$valueObject->alternativeText = $value['alternativeText'];
$valueObject->additionalData = $value['additionalData'];

return $valueObject;
}
Expand Down
53 changes: 53 additions & 0 deletions src/lib/Form/Transformer/JsonToArrayTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\EzPlatformContentForms\Form\Transformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

final class JsonToArrayTransformer implements DataTransformerInterface
{
public function transform($value)
{
if ($value === null) {
return '';
}

try {
$encoded = json_encode((object) $value, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new TransformationFailedException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}

return $encoded;
}

public function reverseTransform($value)
{
if ($value === null) {
return [];
}

try {
$decoded = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new TransformationFailedException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}

return $decoded;
}
}
5 changes: 5 additions & 0 deletions src/lib/Form/Type/FieldType/ImageFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace EzSystems\EzPlatformContentForms\Form\Type\FieldType;

use EzSystems\EzPlatformContentForms\Form\Type\JsonArrayType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -43,6 +44,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => /** @Desc("Alternative text") */ 'content.field_type.ezimage.alternative_text',
'required' => false,
]
)
->add(
'additionalData',
JsonArrayType::class
);
}

Expand Down
27 changes: 27 additions & 0 deletions src/lib/Form/Type/JsonArrayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\EzPlatformContentForms\Form\Type;

use EzSystems\EzPlatformContentForms\Form\Transformer\JsonToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;

final class JsonArrayType extends AbstractType
{
public function getParent()
{
return HiddenType::class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new JsonToArrayTransformer());
}
}