Skip to content

Commit

Permalink
EZEE-3281: Variations are now returned for imageasset field type
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou committed Oct 9, 2020
1 parent f608d5a commit 92ef765
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 19 deletions.
14 changes: 14 additions & 0 deletions src/bundle/ApiLoader/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ public function getImageFieldTypeProcessor(RouterInterface $router)
$variationsIdentifiers
);
}

public function getImageAssetFieldTypeProcessor(
RouterInterface $router
):FieldTypeProcessor\ImageAssetFieldTypeProcessor {
$variationsIdentifiers = array_keys($this->configResolver->getParameter('image_variations'));
sort($variationsIdentifiers);

return new FieldTypeProcessor\ImageAssetFieldTypeProcessor(
$router,
$this->repository->getContentService(),
$this->configResolver->getParameter('fieldtypes.ezimageasset.mappings'),
$variationsIdentifiers
);
}
}
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ services:
parent: ezpublish_rest.controller.base
arguments:
- "@ezpublish.fieldType.ezimage.variation_service"
- '@ezpublish.config.resolver'
tags: [controller.service_arguments]

ezpublish_rest.controller.content:
Expand Down Expand Up @@ -301,6 +302,13 @@ services:
tags:
- { name: ezpublish_rest.field_type_processor, alias: ezimage }

EzSystems\EzPlatformRest\FieldTypeProcessor\ImageAssetFieldTypeProcessor:
factory: ["@ezpublish_rest.factory", getImageAssetFieldTypeProcessor]
arguments:
- "@router"
tags:
- { name: ezpublish_rest.field_type_processor, alias: ezimageasset }

ezpublish_rest.field_type_processor.ezdatetime:
class: "%ezpublish_rest.field_type_processor.ezdatetime.class%"
tags:
Expand Down
70 changes: 70 additions & 0 deletions src/lib/FieldTypeProcessor/ImageAssetFieldTypeProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\EzPlatformRest\FieldTypeProcessor;

use eZ\Publish\API\Repository\ContentService;
use EzSystems\EzPlatformRest\FieldTypeProcessor;
use Symfony\Component\Routing\RouterInterface;

class ImageAssetFieldTypeProcessor extends FieldTypeProcessor
{
/** @var \Symfony\Component\Routing\RouterInterface */
private $router;

/** @var \eZ\Publish\API\Repository\ContentService */
private $contentService;

/** @var string[] */
private $configMappings;

/** @var string[] */
private $variations;

public function __construct(
RouterInterface $router,
ContentService $contentService,
array $configMappings,
array $variations
) {
$this->router = $router;
$this->variations = $variations;
$this->contentService = $contentService;
$this->configMappings = $configMappings;
}

public function postProcessValueHash($outgoingValueHash)
{
if (!\is_array($outgoingValueHash)) {
return $outgoingValueHash;
}

if ($outgoingValueHash['destinationContentId'] === null) {
return $outgoingValueHash;
}

$imageContent = $this->contentService->loadContent((int) $outgoingValueHash['destinationContentId']);

$field = $imageContent->getField($this->configMappings['content_field_identifier']);

$imageId = $imageContent->id . '-' . $field->id . '-' . $imageContent->versionInfo->versionNo;
foreach ($this->variations as $variationIdentifier) {
$outgoingValueHash['variations'][$variationIdentifier] = [
'href' => $this->router->generate(
'ezpublish_rest_binaryContent_getImageVariation',
[
'imageId' => $imageId,
'variationIdentifier' => $variationIdentifier,
]
),
];
}

return $outgoingValueHash;
}
}
45 changes: 28 additions & 17 deletions src/lib/Server/Controller/BinaryContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@
*/
namespace EzSystems\EzPlatformRest\Server\Controller;

use eZ\Publish\API\Repository\Exceptions\InvalidVariationException;
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
use eZ\Publish\Core\FieldType\ImageAsset\Value as ImageAssetValue;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\SPI\Variation\VariationHandler;
use EzSystems\EzPlatformRest\Exceptions;
use EzSystems\EzPlatformRest\Server\Controller as RestController;
use EzSystems\EzPlatformRest\Server\Values\CachedValue;
use eZ\Publish\SPI\Variation\VariationHandler;
use eZ\Publish\API\Repository\Exceptions\InvalidVariationException;

/**
* Binary content controller.
*/
class BinaryContent extends RestController
{
/**
* @var \eZ\Publish\SPI\Variation\VariationHandler
*/
/** @var \eZ\Publish\SPI\Variation\VariationHandler */
protected $imageVariationHandler;

/**
* Construct controller.
*
* @param \eZ\Publish\SPI\Variation\VariationHandler $imageVariationHandler
*/
public function __construct(VariationHandler $imageVariationHandler)
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

public function __construct(
VariationHandler $imageVariationHandler,
ConfigResolverInterface $configResolver
) {
$this->imageVariationHandler = $imageVariationHandler;
$this->configResolver = $configResolver;
}

/**
Expand Down Expand Up @@ -64,9 +66,18 @@ public function getImageVariation($imageId, $variationIdentifier)
throw new Exceptions\NotFoundException("No image Field with ID $fieldId found");
}

// check the field's value
if ($field->value->uri === null) {
throw new Exceptions\NotFoundException("Image file {$field->value->id} doesn't exist");
$value = $field->value;

if ($value instanceof ImageValue && $value->uri === null) {
throw new Exceptions\NotFoundException("Image file {$value->id} doesn't exist");
}
if ($value instanceof ImageAssetValue) {
if ($value->destinationContentId === null) {
throw new Exceptions\NotFoundException("There is no image connected with field {$fieldId}");
}
$content = $this->repository->getContentService()->loadContent((int) $value->destinationContentId);
$mappings = $this->configResolver->getParameter('fieldtypes.ezimageasset.mappings');
$field = $content->getField($mappings['content_field_identifier']);
}

try {
Expand Down Expand Up @@ -99,9 +110,9 @@ private function parseImageId($imageId)
{
$idArray = explode('-', $imageId);

if (count($idArray) == 2) {
if (\count($idArray) == 2) {
return array_merge($idArray, [null]);
} elseif (count($idArray) == 3) {
} elseif (\count($idArray) == 3) {
return $idArray;
}

Expand Down
Loading

0 comments on commit 92ef765

Please sign in to comment.