Skip to content

Commit

Permalink
Merge pull request #56 from TheCadien/feature/single_snippet_selection
Browse files Browse the repository at this point in the history
Add single_snippet_selection content type resolver
  • Loading branch information
niklasnatter authored Nov 25, 2020
2 parents 996db2e + 3845227 commit cc288eb
Show file tree
Hide file tree
Showing 3 changed files with 395 additions and 0 deletions.
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]);
}

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

0 comments on commit cc288eb

Please sign in to comment.