Skip to content

Commit

Permalink
Merge branch '2.5' of https://github.com/sulu/SuluArticleBundle into 3.0
Browse files Browse the repository at this point in the history
 Conflicts:
	SuluArticleBundle.php
	UPGRADE.md
  • Loading branch information
alexander-schranz committed Feb 22, 2024
2 parents b0e721e + 93d992b commit 1fa29ee
Show file tree
Hide file tree
Showing 29 changed files with 554 additions and 269 deletions.
5 changes: 5 additions & 0 deletions Admin/ArticleAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function(Localization $localization) {
];
}

$previewCondition = '(__routeAttributes.locale in availableLocales) or !__routeAttributes';
$viewCollection->add(
$this->viewBuilderFactory->createListViewBuilder(static::LIST_VIEW . '_' . $typeKey, '/:locale/' . $typeKey)
->setResourceKey(ArticleDocument::RESOURCE_KEY)
Expand Down Expand Up @@ -308,6 +309,7 @@ function(Localization $localization) {
->setTabCondition('shadowOn == false')
->setTabPriority(1024)
->addToolbarActions($formToolbarActionsWithType)
->setPreviewCondition($previewCondition)
->setParent(static::EDIT_FORM_VIEW . '_' . $typeKey)
);
$viewCollection->add(
Expand All @@ -318,6 +320,7 @@ function(Localization $localization) {
->setTabCondition('shadowOn == false')
->addToolbarActions($formToolbarActionsWithoutType)
->setTitleVisible(true)
->setPreviewCondition($previewCondition)
->setParent(static::EDIT_FORM_VIEW . '_' . $typeKey)
);
$viewCollection->add(
Expand All @@ -328,6 +331,7 @@ function(Localization $localization) {
->setTabCondition('shadowOn == false')
->addToolbarActions($formToolbarActionsWithoutType)
->setTitleVisible(true)
->setPreviewCondition($previewCondition)
->setParent(static::EDIT_FORM_VIEW . '_' . $typeKey)
);
$viewCollection->add(
Expand All @@ -338,6 +342,7 @@ function(Localization $localization) {
->setTabPriority(512)
->addToolbarActions($formToolbarActionsWithoutType)
->setTitleVisible(true)
->setPreviewCondition($previewCondition)
->setParent(static::EDIT_FORM_VIEW . '_' . $typeKey)
);

Expand Down
31 changes: 30 additions & 1 deletion Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Sulu\Bundle\ArticleBundle\Document\Index\DocumentFactoryInterface;
use Sulu\Bundle\ArticleBundle\ListBuilder\ElasticSearchFieldDescriptor;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Form\Exception\InvalidFormException;
use Sulu\Component\Content\Mapper\ContentMapperInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
Expand Down Expand Up @@ -111,6 +113,11 @@ class ArticleController extends AbstractRestController implements ClassResourceI
*/
private $displayTabAll;

/**
* @var DocumentInspector|null
*/
private $documentInspector;

public function __construct(
ViewHandlerInterface $viewHandler,
DocumentManagerInterface $documentManager,
Expand All @@ -123,7 +130,8 @@ public function __construct(
RequestHashCheckerInterface $requestHashChecker,
SecurityCheckerInterface $securityChecker,
bool $displayTabAll,
?TokenStorageInterface $tokenStorage = null
?TokenStorageInterface $tokenStorage = null,
?DocumentInspector $documentInspector = null
) {
parent::__construct($viewHandler, $tokenStorage);

Expand All @@ -137,6 +145,11 @@ public function __construct(
$this->requestHashChecker = $requestHashChecker;
$this->securityChecker = $securityChecker;
$this->displayTabAll = $displayTabAll;
$this->documentInspector = $documentInspector;

if (null === $this->documentInspector) {
@trigger_deprecation('sulu/article-bundle', '2.5', 'Instantiating the ArticleController without the $documentInspector argument is deprecated!');
}
}

/**
Expand Down Expand Up @@ -413,11 +426,27 @@ private function getRangeQuery(string $field, string $from, string $to): RangeQu
public function getAction(Request $request, string $id): Response
{
$locale = $this->getRequestParameter($request, 'locale', true);
/** @var ArticleDocument $document */
$document = $this->documentManager->find(
$id,
$locale
);

if ($this->documentInspector) {
$localizationState = $this->documentInspector->getLocalizationState($document);

if (LocalizationState::GHOST === $localizationState) {
$document = $this->documentManager->find(
$id,
$locale,
[
'load_ghost_content' => false,
'structure_type' => $document->getStructureType(),
]
);
}
}

$context = new Context();
$context->setSerializeNull(true);
$context->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage']);
Expand Down
13 changes: 8 additions & 5 deletions Controller/WebsiteArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function renderArticle(
if ($partial) {
$response = $this->createResponse($request);
$response->setContent(
$this->renderBlock(
$this->renderBlockView(
$viewTemplate,
'content',
$data
Expand Down Expand Up @@ -161,18 +161,21 @@ private function createResponse(Request $request): Response
return $response;
}

protected function renderBlock(string $template, string $block, array $attributes = []): string
/**
* @param array<string, mixed> $parameters
*/
protected function renderBlockView(string $view, string $block, array $parameters = []): string
{
$twig = $this->getTwig();

$attributes = $twig->mergeGlobals($attributes);
$template = $twig->load($template);
$parameters = $twig->mergeGlobals($parameters);
$template = $twig->load($view);

$level = \ob_get_level();
\ob_start();

try {
$rendered = $template->renderBlock($block, $attributes);
$rendered = $template->renderBlock($block, $parameters);
\ob_end_clean();

return $rendered;
Expand Down
2 changes: 1 addition & 1 deletion Document/Index/ArticleGhostIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function index(ArticleDocument $document): void
}

$article = $this->createOrUpdateArticle($document, $document->getLocale());
$this->createOrUpdateShadows($document);
$this->updateShadows($document);
$this->createOrUpdateGhosts($document);
$this->dispatchIndexEvent($document, $article);
$this->manager->persist($article);
Expand Down
39 changes: 32 additions & 7 deletions Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ONGR\ElasticsearchBundle\Collection\Collection;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
Expand Down Expand Up @@ -327,6 +328,15 @@ private function getBlockContentFieldsRecursive(array $blocks, ArticleDocument $
return $contentFields;
}

protected function findViewDocument(ArticleDocument $document, string $locale): ?ArticleViewDocumentInterface
{
$articleId = $this->getViewDocumentId($document->getUuid(), $locale);
/** @var ArticleViewDocumentInterface $article */
$article = $this->manager->find($this->documentFactory->getClass('article'), $articleId);

return $article;
}

/**
* Returns view-document from index or create a new one.
*/
Expand All @@ -335,9 +345,7 @@ protected function findOrCreateViewDocument(
string $locale,
string $localizationState
): ?ArticleViewDocumentInterface {
$articleId = $this->getViewDocumentId($document->getUuid(), $locale);
/** @var ArticleViewDocumentInterface $article */
$article = $this->manager->find($this->documentFactory->getClass('article'), $articleId);
$article = $this->findViewDocument($document, $locale);

if ($article) {
// Only index ghosts when the article isn't a ghost himself.
Expand All @@ -351,7 +359,7 @@ protected function findOrCreateViewDocument(
}

$article = $this->documentFactory->create('article');
$article->setId($articleId);
$article->setId($this->getViewDocumentId($document->getUuid(), $locale));
$article->setUuid($document->getUuid());
$article->setLocale($locale);

Expand Down Expand Up @@ -446,6 +454,17 @@ public function replaceWithGhostData(ArticleDocument $document, string $locale):
$article = $this->createOrUpdateArticle($document, $locale);
$article->setLocalizationState(new LocalizationStateViewObject(LocalizationState::GHOST, $document->getOriginalLocale()));

$repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
$search = $repository->createSearch();
$search->addQuery(new TermQuery('localization_state.state', 'ghost'), BoolQuery::MUST);
$search->addQuery(new TermQuery('localization_state.locale', $locale), BoolQuery::MUST);

/** @var array<array{locale: string}> $searchResult */
$searchResult = $repository->findArray($search);
foreach ($searchResult as $result) {
$this->replaceWithGhostData($document, $result['locale']);
}

$this->manager->persist($article);
}

Expand Down Expand Up @@ -505,11 +524,12 @@ public function index(ArticleDocument $document): void
$this->dispatchIndexEvent($document, $article);
$this->manager->persist($article);

$this->createOrUpdateShadows($document);
$this->updateShadows($document);
}

protected function indexShadow(ArticleDocument $document): void
{
/** @var ArticleDocument $shadowDocument */
$shadowDocument = $this->documentManager->find(
$document->getUuid(),
$document->getOriginalLocale(),
Expand All @@ -519,12 +539,11 @@ protected function indexShadow(ArticleDocument $document): void
);

$article = $this->createOrUpdateArticle($shadowDocument, $document->getOriginalLocale(), LocalizationState::SHADOW);

$this->dispatchIndexEvent($shadowDocument, $article);
$this->manager->persist($article);
}

protected function createOrUpdateShadows(ArticleDocument $document): void
protected function updateShadows(ArticleDocument $document): void
{
if ($document->isShadowLocaleEnabled()) {
return;
Expand All @@ -534,6 +553,12 @@ protected function createOrUpdateShadows(ArticleDocument $document): void
try {
/** @var ArticleDocument $shadowDocument */
$shadowDocument = $this->documentManager->find($document->getUuid(), $shadowLocale);

// update shadow only if original document exists
if (!$this->findViewDocument($shadowDocument, $document->getLocale())) {
continue;
}

$this->indexShadow($shadowDocument);
} catch (DocumentManagerException $documentManagerException) {
// @ignoreException
Expand Down
10 changes: 9 additions & 1 deletion Document/Serializer/WebsiteArticleUrlsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,18 @@ public function addUrlsOnPostSerialize(ObjectEvent $event): void
return;
}

if (null !== ($portal = $attributes->getAttribute('portal'))) {
$allLocalizations = $portal->getLocalizations();
} else {
$allLocalizations = $webspace->getAllLocalizations();
}

$urls = [];
$localizations = [];
$publishedLocales = $this->getPublishedLocales($article, $webspace);

foreach ($this->getWebspaceLocales($webspace) as $locale) {
foreach ($allLocalizations as $localization) {
$locale = $localization->getLocale();
$published = \in_array($locale, $publishedLocales, true);
$path = '/';
$alternate = false;
Expand All @@ -160,6 +167,7 @@ public function addUrlsOnPostSerialize(ObjectEvent $event): void
$localizations[$locale] = [
'locale' => $locale,
'url' => $this->webspaceManager->findUrlByResourceLocator($path, null, $locale),
'country' => $localization->getCountry(),
'alternate' => $alternate,
];
}
Expand Down
14 changes: 14 additions & 0 deletions Document/Structure/ArticleBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,18 @@ public function setWebspaceKey($webspace)
{
$this->webspaceKey = $webspace;
}

public function getLanguageCode()
{
if (!$this->document) {
return $this->locale;
}

// return original locale for shadow or ghost pages
if ($this->getIsShadow() || ($this->getType() && 'ghost' === $this->getType()->getName())) {
return $this->inspector->getOriginalLocale($this->getDocument());
}

return parent::getLanguageCode();
}
}
13 changes: 12 additions & 1 deletion Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ public function handleRemoveLocale(RemoveLocaleEvent $event)
return;
}

$concreteLocales = $this->documentInspector->getConcreteLocales($document);
$ghostLocale = $document->getOriginalLocale();
if (!\in_array($ghostLocale, $concreteLocales, true)) {
$ghostLocale = $concreteLocales[0] ?? null;
}

if (null !== $ghostLocale) {
/** @var ArticleDocument $document */
$document = $this->documentManager->find($document->getUuid(), $ghostLocale);
}

$this->indexer->replaceWithGhostData($document, $event->getLocale());
$this->indexer->flush();
}
Expand Down Expand Up @@ -495,7 +506,7 @@ public function handleRemoveLocaleLive(RemoveLocaleEvent $event)
return;
}

$this->liveIndexer->replaceWithGhostData($document, $event->getLocale());
$this->liveIndexer->remove($document, $event->getLocale());
$this->liveIndexer->flush();
}

Expand Down
Loading

0 comments on commit 1fa29ee

Please sign in to comment.