-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4' of https://github.com/sulu/SuluArticleBundle into 2.5
Conflicts: Resources/config/services_headless.xml
- Loading branch information
Showing
17 changed files
with
1,785 additions
and
9 deletions.
There are no files selected for viewing
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
112 changes: 112 additions & 0 deletions
112
Infrastructure/SuluHeadlessBundle/ContentTypeResolver/ArticleSelectionResolver.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,112 @@ | ||
<?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\ArticleBundle\Infrastructure\SuluHeadlessBundle\ContentTypeResolver; | ||
|
||
use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\ContentTypeResolverInterface; | ||
use Sulu\Bundle\HeadlessBundle\Content\ContentView; | ||
use Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
use Sulu\Component\Content\Compat\PropertyParameter; | ||
use Sulu\Component\Content\Mapper\ContentMapperInterface; | ||
use Sulu\Component\Content\Query\ContentQueryBuilderInterface; | ||
|
||
class ArticleSelectionResolver implements ContentTypeResolverInterface | ||
{ | ||
public static function getContentType(): string | ||
{ | ||
return 'article_selection'; | ||
} | ||
|
||
/** | ||
* @var StructureResolverInterface | ||
*/ | ||
private $structureResolver; | ||
|
||
/** | ||
* @var ContentQueryBuilderInterface | ||
*/ | ||
private $contentQueryBuilder; | ||
|
||
/** | ||
* @var ContentMapperInterface | ||
*/ | ||
private $contentMapper; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $showDrafts; | ||
|
||
public function __construct( | ||
StructureResolverInterface $structureResolver, | ||
ContentQueryBuilderInterface $contentQueryBuilder, | ||
ContentMapperInterface $contentMapper, | ||
bool $showDrafts | ||
) { | ||
$this->structureResolver = $structureResolver; | ||
$this->contentQueryBuilder = $contentQueryBuilder; | ||
$this->contentMapper = $contentMapper; | ||
$this->showDrafts = $showDrafts; | ||
} | ||
|
||
public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView | ||
{ | ||
/** @var string[]|null $ids */ | ||
$ids = $data; | ||
|
||
if (empty($ids)) { | ||
return new ContentView([], ['ids' => []]); | ||
} | ||
|
||
/** @var PropertyParameter[] $params */ | ||
$params = $property->getParams(); | ||
/** @var PropertyParameter[] $propertiesParamValue */ | ||
$propertiesParamValue = isset($params['properties']) ? $params['properties']->getValue() : []; | ||
|
||
$this->contentQueryBuilder->init([ | ||
'ids' => $ids, | ||
'properties' => $propertiesParamValue, | ||
'published' => !$this->showDrafts, | ||
]); | ||
|
||
/** @var array{string, mixed[]} $queryBuilderResult */ | ||
$queryBuilderResult = $this->contentQueryBuilder->build($property->getStructure()->getWebspaceKey(), [$locale]); | ||
list($articlesQuery) = $queryBuilderResult; | ||
|
||
$articleStructures = $this->contentMapper->loadBySql2( | ||
$articlesQuery, | ||
$locale, | ||
$property->getStructure()->getWebspaceKey() | ||
); | ||
|
||
$propertyMap = [ | ||
'title' => 'title', | ||
'routePath' => 'routePath', | ||
]; | ||
|
||
foreach ($propertiesParamValue as $propertiesParamEntry) { | ||
$paramName = $propertiesParamEntry->getName(); | ||
$paramValue = $propertiesParamEntry->getValue(); | ||
$propertyMap[$paramName] = \is_string($paramValue) ? $paramValue : $paramName; | ||
} | ||
|
||
$articles = \array_fill_keys($ids, null); | ||
|
||
foreach ($articleStructures as $articleStructure) { | ||
$articles[$articleStructure->getUuid()] = $this->structureResolver->resolveProperties($articleStructure, $propertyMap, $locale); | ||
} | ||
|
||
return new ContentView(\array_values(\array_filter($articles)), ['ids' => $ids]); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Infrastructure/SuluHeadlessBundle/ContentTypeResolver/SingleArticleSelectionResolver.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,53 @@ | ||
<?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\ArticleBundle\Infrastructure\SuluHeadlessBundle\ContentTypeResolver; | ||
|
||
use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\ContentTypeResolverInterface; | ||
use Sulu\Bundle\HeadlessBundle\Content\ContentView; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
|
||
class SingleArticleSelectionResolver implements ContentTypeResolverInterface | ||
{ | ||
public static function getContentType(): string | ||
{ | ||
return 'single_article_selection'; | ||
} | ||
|
||
/** | ||
* @var ContentTypeResolverInterface | ||
*/ | ||
private $articleSelectionResolver; | ||
|
||
public function __construct(ContentTypeResolverInterface $articleSelectionResolver) | ||
{ | ||
$this->articleSelectionResolver = $articleSelectionResolver; | ||
} | ||
|
||
public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView | ||
{ | ||
/** @var string|null $id */ | ||
$id = $data; | ||
|
||
if (empty($id)) { | ||
return new ContentView(null, ['id' => null]); | ||
} | ||
|
||
$content = $this->articleSelectionResolver->resolve([$id], $property, $locale, $attributes); | ||
|
||
/** @var mixed[]|null $contentData */ | ||
$contentData = $content->getContent(); | ||
|
||
return new ContentView($contentData[0] ?? null, ['id' => $id]); | ||
} | ||
} |
Oops, something went wrong.