forked from sulu/SuluArticleBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add single article selection content type (#2)
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/Infrastructure/Sulu/Content/SingleArticleSelectionContentType.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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sulu\Article\Infrastructure\Sulu\Content; | ||
|
||
use Sulu\Article\Domain\Repository\ArticleRepositoryInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; | ||
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
use Sulu\Component\Content\PreResolvableContentTypeInterface; | ||
use Sulu\Component\Content\SimpleContentType; | ||
|
||
class SingleArticleSelectionContentType extends SimpleContentType implements PreResolvableContentTypeInterface | ||
{ | ||
private ArticleRepositoryInterface $articleRepository; | ||
|
||
private ContentManagerInterface $contentManager; | ||
|
||
public function __construct( | ||
ArticleRepositoryInterface $articleRepository, | ||
ContentManagerInterface $contentManager, | ||
ReferenceStoreInterface $referenceStore, | ||
) | ||
{ | ||
parent::__construct('Article'); | ||
|
||
$this->articleRepository = $articleRepository; | ||
$this->contentManager = $contentManager; | ||
$this->referenceStore = $referenceStore; | ||
} | ||
|
||
public function getContentData(PropertyInterface $property) | ||
{ | ||
$uuid = $property->getValue(); | ||
if (null === $uuid) { | ||
return null; | ||
} | ||
|
||
$dimensionAttributes = [ | ||
'locale' => $property->getStructure()->getLanguageCode(), | ||
'stage' => DimensionContentInterface::STAGE_LIVE, | ||
]; | ||
|
||
$article = $this->articleRepository->getOneBy( | ||
filters: \array_merge( | ||
[ | ||
'uuid' => $uuid, | ||
], | ||
$dimensionAttributes, | ||
), | ||
selects: [ | ||
ArticleRepositoryInterface::GROUP_SELECT_ARTICLE_WEBSITE => true, | ||
]); | ||
|
||
|
||
$dimensionContent = $this->contentManager->resolve($article, $dimensionAttributes); | ||
return $this->contentManager->normalize($dimensionContent); | ||
} | ||
|
||
public function preResolve(PropertyInterface $property) | ||
{ | ||
$uuid = $property->getValue(); | ||
if (null === $uuid) { | ||
return; | ||
} | ||
|
||
$this->referenceStore->add($uuid); | ||
} | ||
} | ||
|
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