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

Add Image map resolver #85

Merged
merged 4 commits into from
Apr 8, 2021
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
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