-
Notifications
You must be signed in to change notification settings - Fork 25
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
niklasnatter
merged 3 commits into
sulu:master
from
TheCadien:feature/single_snippet_selection
Nov 25, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
Content/ContentTypeResolver/SingleSnippetSelectionResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 selectionsThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
SuluHeadlessBundle/Content/ContentTypeResolver/SingleContactSelectionResolver.php
Lines 49 to 51 in 1bea2a1
but we dont need to do this in this pull request
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 🙂