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 single_snippet_selection content type resolver #56

Merged
merged 3 commits into from
Nov 25, 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
115 changes: 115 additions & 0 deletions Content/ContentTypeResolver/SingleSnippetSelectionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface;
use Sulu\Bundle\SnippetBundle\Document\SnippetDocument;
use Sulu\Bundle\SnippetBundle\Snippet\DefaultSnippetManagerInterface;
use Sulu\Bundle\SnippetBundle\Snippet\WrongSnippetTypeException;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\Compat\Structure\SnippetBridge;
use Sulu\Component\Content\Compat\Structure\StructureBridge;
use Sulu\Component\Content\Mapper\ContentMapperInterface;

class SingleSnippetSelectionResolver implements ContentTypeResolverInterface
{
/**
* @var StructureResolverInterface
*/
private $structureResolver;

/**
* @var ContentMapperInterface
*/
private $contentMapper;

/**
* @var DefaultSnippetManagerInterface
*/
private $defaultSnippetManager;

public function __construct(
ContentMapperInterface $contentMapper,
StructureResolverInterface $structureResolver,
DefaultSnippetManagerInterface $defaultSnippetManager
) {
$this->contentMapper = $contentMapper;
$this->structureResolver = $structureResolver;
$this->defaultSnippetManager = $defaultSnippetManager;
}

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

/**
* {@inheritdoc}
*/
public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView
{
/** @var StructureBridge $structure */
$structure = $property->getStructure();
$webspaceKey = $structure->getWebspaceKey();
$shadowLocale = $structure->getIsShadow() ? $structure->getShadowBaseLanguage() : null;

$params = $property->getParams();
/** @var bool $loadExcerpt */
$loadExcerpt = isset($params['loadExcerpt']) ? $params['loadExcerpt']->getValue() : false;
/** @var string $defaultArea */
$defaultArea = isset($params['default']) ? $params['default']->getValue() : null;

$snippetId = $data ?? null;
if (empty($snippetId) && $defaultArea) {
$defaultSnippetId = $this->getDefaultSnippetId($webspaceKey, $defaultArea, $locale);
$snippetId = $defaultSnippetId ?: null;
}

if (empty($snippetId)) {
return new ContentView(null);
}

/** @var SnippetBridge $snippet */
$snippet = $this->contentMapper->load($snippetId, $webspaceKey, $locale);

if (!$snippet->getHasTranslation() && null !== $shadowLocale) {
/** @var SnippetBridge $snippet */
$snippet = $this->contentMapper->load($snippetId, $webspaceKey, $shadowLocale);
}

$snippet->setIsShadow(null !== $shadowLocale);
$snippet->setShadowBaseLanguage($shadowLocale);

$resolvedSnippet = $this->structureResolver->resolve($snippet, $locale, $loadExcerpt);

return new ContentView($resolvedSnippet, ['id' => $data]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind to adjust the existing SnippetSelectionResolver to use this format too? 🙂 (['ids' => ...]). then this would be consistent for the snippet selections

Copy link
Member

@alexander-schranz alexander-schranz Nov 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we forget that when fixing #46 same should be done for the PageSelectionResolver and SinglePageSelectionResolver. Then I think all should be consistent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess it would also make sense to use that format in case there is no value set 🙂 for example here:

if (null === $data) {
return new ContentView(null);
}

but we dont need to do this in this pull request

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i'm not sure anymore :D should i change something more ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no - i will create a PR for the other resolvers 🙂

}

private function getDefaultSnippetId(string $webspaceKey, string $snippetArea, string $locale): ?string
{
try {
/** @var SnippetDocument|null $snippet */
$snippet = $this->defaultSnippetManager->load($webspaceKey, $snippetArea, $locale);
} catch (WrongSnippetTypeException $exception) {
return null;
}

if (!$snippet) {
return null;
}

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

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

<service
id="sulu_headless.content_resolver.single_snippet_selection"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\SingleSnippetSelectionResolver"
lazy="true"
>
<argument type="service" id="sulu.content.mapper"/>
<argument type="service" id="sulu_headless.structure_resolver"/>
<argument type="service" id="sulu_snippet.default_snippet.manager"/>

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