Skip to content

Commit

Permalink
Add Image map resolver (#85)
Browse files Browse the repository at this point in the history
* Add imagemap resolver

* Add test cases

* Fix lint error

* Rename contentResolver variable
  • Loading branch information
Prokyonn authored Apr 8, 2021
1 parent 40215f2 commit fc701ca
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Content/ContentTypeResolver/BlockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function getContentType(): string
/**
* @var ContentResolverInterface
*/
private $resolver;
private $contentResolver;

/**
* @var \Traversable<BlockVisitorInterface>
Expand All @@ -41,7 +41,7 @@ public static function getContentType(): string
*/
public function __construct(ContentResolverInterface $contentResolver, \Traversable $blockVisitors)
{
$this->resolver = $contentResolver;
$this->contentResolver = $contentResolver;
$this->blockVisitors = $blockVisitors;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public function resolve($data, PropertyInterface $property, string $locale, arra
$view[$i] = [];

foreach ($blockPropertyType->getChildProperties() as $childProperty) {
$contentView = $this->resolver->resolve($childProperty->getValue(), $childProperty, $locale, $attributes);
$contentView = $this->contentResolver->resolve($childProperty->getValue(), $childProperty, $locale, $attributes);

$content[$i][$childProperty->getName()] = $contentView->getContent();
$view[$i][$childProperty->getName()] = $contentView->getView();
Expand Down
86 changes: 86 additions & 0 deletions Content/ContentTypeResolver/ImageMapResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver;

use Sulu\Bundle\HeadlessBundle\Content\ContentResolverInterface;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\MediaSerializerInterface;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;

class ImageMapResolver implements ContentTypeResolverInterface
{
/**
* @var MediaManagerInterface
*/
private $mediaManager;

/**
* @var MediaSerializerInterface
*/
private $mediaSerializer;

/**
* @var ContentResolverInterface
*/
private $contentResolver;

public static function getContentType(): string
{
return 'image_map';
}

public function __construct(
MediaManagerInterface $mediaManager,
MediaSerializerInterface $mediaSerializer,
ContentResolverInterface $contentResolver
) {
$this->mediaManager = $mediaManager;
$this->mediaSerializer = $mediaSerializer;
$this->contentResolver = $contentResolver;
}

public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView
{
$imageId = $data['imageId'] ?? null;
$hotspots = $data['hotspots'] ?? [];

$content = [];
$view = [];
if ($imageId) {
$media = $this->mediaManager->getById($imageId, $locale);
$content['image'] = $this->mediaSerializer->serialize($media->getEntity(), $locale);
$view['image'] = ['id' => $imageId];
}

foreach ($hotspots as $i => $hotspot) {
$hotspotView = [];

$propertyType = $property->initProperties($i, $hotspot['type']);
foreach ($propertyType->getChildProperties() as $childProperty) {
$key = $childProperty->getName();

$childProperty->setValue($hotspot[$key] ?? null);
$result = $this->contentResolver->resolve($childProperty->getValue(), $childProperty, $locale, $attributes);
$hotspot[$key] = $result->getContent();
$hotspotView[$key] = $result->getView();
}

$content['hotspots'][] = $hotspot;
$view['hotspots'][] = $hotspotView;
}

return new ContentView($content, $view);
}
}
12 changes: 12 additions & 0 deletions Resources/config/content-type-resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,17 @@

<tag name="sulu_headless.content_type_resolver"/>
</service>

<service
id="sulu_headless.content_resolver.image_map"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\ImageMapResolver"
lazy="true"
>
<argument type="service" id="sulu_media.media_manager"/>
<argument type="service" id="sulu_headless.serializer.media"/>
<argument type="service" id="sulu_headless.content_resolver"/>

<tag name="sulu_headless.content_type_resolver"/>
</service>
</services>
</container>
Loading

0 comments on commit fc701ca

Please sign in to comment.