Skip to content

Commit

Permalink
Add single article selection content type (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCadien authored and alexander-schranz committed Aug 8, 2024
1 parent 3faa006 commit c988504
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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);
}
}

10 changes: 10 additions & 0 deletions src/Infrastructure/Symfony/HttpKernel/SuluArticleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sulu\Article\Infrastructure\Sulu\Content\ArticleSelectionContentType;
use Sulu\Article\Infrastructure\Sulu\Content\ArticleSitemapProvider;
use Sulu\Article\Infrastructure\Sulu\Content\ArticleTeaserProvider;
use Sulu\Article\Infrastructure\Sulu\Content\SingleArticleSelectionContentType;
use Sulu\Article\UserInterface\Controller\Admin\ArticleController;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\ContentObjectProvider;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Search\ContentSearchMetadataProvider;
Expand Down Expand Up @@ -246,6 +247,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.single_article_selection')
->class(SingleArticleSelectionContentType::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' => 'single_article_selection']);

$services->set('sulu_article.content_types.article_selection')
->class(ArticleSelectionContentType::class)
->args([
Expand Down

0 comments on commit c988504

Please sign in to comment.