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 Article Selection Content Type (#3)
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/Infrastructure/Sulu/Content/ArticleSelectionContentType.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,77 @@ | ||
<?php | ||
|
||
namespace Sulu\Article\Infrastructure\Sulu\Content; | ||
|
||
use Sulu\Article\Domain\Repository\ArticleRepositoryInterface; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface; | ||
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 ArticleSelectionContentType extends SimpleContentType implements PreResolvableContentTypeInterface | ||
{ | ||
private ReferenceStoreInterface $referenceStore; | ||
private ContentManagerInterface $contentManager; | ||
private ArticleRepositoryInterface $articleRepository; | ||
|
||
public function __construct( | ||
ArticleRepositoryInterface $articleRepository, | ||
ContentManagerInterface $contentManager, | ||
ReferenceStoreInterface $referenceStore | ||
) | ||
{ | ||
parent::__construct('Article', []); | ||
$this->referenceStore = $referenceStore; | ||
$this->contentManager = $contentManager; | ||
$this->articleRepository = $articleRepository; | ||
} | ||
|
||
public function getContentData(PropertyInterface $property) | ||
{ | ||
$value = $property->getValue(); | ||
if (null === $value || !\is_array($value) || 0 === \count($value)) { | ||
return []; | ||
} | ||
|
||
$dimensionAttributes = [ | ||
'locale' => $property->getStructure()->getLanguageCode(), | ||
'stage' => DimensionContentInterface::STAGE_LIVE, | ||
]; | ||
|
||
$article = $this->articleRepository->findBy( | ||
filters: \array_merge( | ||
['uuids' => $value], | ||
$dimensionAttributes, | ||
), | ||
selects: [ | ||
ArticleRepositoryInterface::GROUP_SELECT_ARTICLE_WEBSITE => true, | ||
]); | ||
|
||
|
||
$result = []; | ||
foreach ($article as $article) { | ||
$dimensionContent = $this->contentManager->resolve($article, $dimensionAttributes); | ||
$result[\array_search($article->getUuid(), $value, false)] = $this->contentManager->normalize($dimensionContent); | ||
} | ||
|
||
\ksort($result); | ||
|
||
return \array_values($result); | ||
|
||
} | ||
|
||
public function preResolve(PropertyInterface $property) | ||
{ | ||
$uuids = $property->getValue(); | ||
if (!\is_array($uuids)) { | ||
return; | ||
} | ||
|
||
foreach ($uuids as $uuid) { | ||
$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