Skip to content

Commit

Permalink
Add Article Selection Content Type (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCadien authored and Prokyonn committed Jul 24, 2024
1 parent 29da44d commit bcfa92d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Infrastructure/Sulu/Content/ArticleSelectionContentType.php
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);
}
}
}
10 changes: 10 additions & 0 deletions src/Infrastructure/Symfony/HttpKernel/SuluArticleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sulu\Article\Infrastructure\Doctrine\Repository\ArticleRepository;
use Sulu\Article\Infrastructure\Sulu\Admin\ArticleAdmin;
use Sulu\Article\Infrastructure\Sulu\Content\ArticleLinkProvider;
use Sulu\Article\Infrastructure\Sulu\Content\ArticleSelectionContentType;
use Sulu\Article\Infrastructure\Sulu\Content\ArticleSitemapProvider;
use Sulu\Article\Infrastructure\Sulu\Content\ArticleTeaserProvider;
use Sulu\Article\UserInterface\Controller\Admin\ArticleController;
Expand Down Expand Up @@ -245,6 +246,15 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
->class(ReferenceStore::class)
->tag('sulu_website.reference_store', ['alias' => ArticleInterface::RESOURCE_KEY]);

$services->set('sulu_article.content_types.article_selection')
->class(ArticleSelectionContentType::class)
->args([
new Reference('sulu_article.article_repository'),
new Reference('sulu_content.content_manager'),
new Reference('sulu_article.article_reference_store')
])
->tag('sulu.content.type', ['alias' => 'article_selection']);

$services->set('sulu_article.article_data_provider')
->class(ContentDataProvider::class) // TODO this should not be handled via Content Bundle instead own service which uses the ArticleRepository
->args([
Expand Down

0 comments on commit bcfa92d

Please sign in to comment.