Skip to content

Commit

Permalink
Add property for the routePathName
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Jul 11, 2024
1 parent 566dbf6 commit e39eadb
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class RoutableSubscriber implements EventSubscriberInterface
{
public const ROUTE_FIELD = 'routePath';

public const ROUTE_FIELD_NAME = self::ROUTE_FIELD . 'Name';

public const ROUTES_PROPERTY = 'suluRoutes';

public const TAG_NAME = 'sulu_article.article_route';
Expand Down Expand Up @@ -308,6 +310,7 @@ private function updateRoute(RoutablePageBehavior $document): void

$node = $this->documentInspector->getNode($document);
$node->setProperty($propertyName, $route->getPath());
$node->setProperty($this->propertyEncoder->localizedContentName(self::ROUTE_FIELD_NAME, $locale), $propertyName);

Check failure on line 313 in Document/Subscriber/RoutableSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Parameter #2 $locale of method Sulu\Component\DocumentManager\PropertyEncoder::localizedContentName() expects string, string|null given.
}

private function updateChildRoutes(ChildrenBehavior $document): void
Expand Down Expand Up @@ -398,7 +401,7 @@ private function getRoutePathPropertyName(string $structureType, string $locale)
{
$metadata = $this->metadataFactory->getStructureMetadata('article', $structureType);

if ($metadata->hasTag(self::TAG_NAME)) {
if ($metadata->hasPropertyWithTagName(self::TAG_NAME)) {

Check failure on line 404 in Document/Subscriber/RoutableSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Cannot call method hasPropertyWithTagName() on Sulu\Component\Content\Metadata\StructureMetadata|null.
return $this->getPropertyName($locale, $metadata->getPropertyByTagName(self::TAG_NAME)->getName());
}

Expand Down
138 changes: 138 additions & 0 deletions Resources/phpcr-migrations/Version202407111600.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/*
* 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;

use Jackalope\Query\Row;
use PHPCR\Migrations\VersionInterface;
use PHPCR\PhpcrMigrationsBundle\ContainerAwareInterface;
use PHPCR\SessionInterface;
use Sulu\Bundle\ArticleBundle\Document\Subscriber\RoutableSubscriber;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\DocumentManager\PropertyEncoder;
use Sulu\Component\Localization\Localization;
use Symfony\Component\DependencyInjection\ContainerInterface;


class Version202407111600 implements VersionInterface, ContainerAwareInterface
{
/**
* @var PropertyEncoder
*/
private $propertyEncoder;

/**
* @var StructureMetadataFactoryInterface
*/
private $metadataFactory;

/**
* @var ContainerInterface
*/
private $container;

public function setContainer(?ContainerInterface $container = null): void
{
if (null === $container) {
throw new \RuntimeException('Container is required to run this migration.');
}

$this->container = $container;
}

public function up(SessionInterface $session)
{
$this->propertyEncoder = $this->container->get('sulu_document_manager.property_encoder');
$this->metadataFactory = $this->container->get('sulu_page.structure.factory');

$liveSession = $this->container->get('sulu_document_manager.live_session');
$this->upgrade($liveSession);
$this->upgrade($session);

$liveSession->save();
$session->save();
}

public function down(SessionInterface $session)
{
$this->propertyEncoder = $this->container->get('sulu_document_manager.property_encoder');
$this->metadataFactory = $this->container->get('sulu_page.structure.factory');

$liveSession = $this->container->get('sulu_document_manager.live_session');
$this->downgrade($liveSession);
$this->downgrade($session);

$liveSession->save();
$session->save();
}

private function upgrade(SessionInterface $session): void
{
$queryManager = $session->getWorkspace()->getQueryManager();
$localizations = $this->container->get('sulu_core.webspace.webspace_manager')->getAllLocalizations();

$query = 'SELECT * FROM [nt:unstructured] WHERE ([jcr:mixinTypes] = "sulu:article" OR [jcr:mixinTypes] = "sulu:articlepage")';
$rows = $queryManager->createQuery($query, 'JCR-SQL2')->execute();

/** @var Localization $localization */
foreach ($localizations as $localization) {
$locale = $localization->getLocale();
$templateKey = $this->propertyEncoder->localizedContentName('template', $locale);

/** @var Row<mixed> $row */
foreach ($rows as $row) {
$node = $row->getNode();
$structureType = $node->getPropertyValue($templateKey);
$routePathPropertyName = $this->getRoutePathPropertyName($structureType, $locale);

$propertyName = $this->propertyEncoder->localizedContentName(RoutableSubscriber::ROUTE_FIELD_NAME, $locale);
$node->setProperty($propertyName, $routePathPropertyName);
}
}
}

private function downgrade(SessionInterface $session)
{
$queryManager = $session->getWorkspace()->getQueryManager();
$localizations = $this->container->get('sulu_core.webspace.webspace_manager')->getAllLocalizations();

$query = 'SELECT * FROM [nt:unstructured] WHERE ([jcr:mixinTypes] = "sulu:article" OR [jcr:mixinTypes] = "sulu:articlepage")';
$rows = $queryManager->createQuery($query, 'JCR-SQL2')->execute();

/** @var Localization $localization */
foreach ($localizations as $localization) {
$locale = $localization->getLocale();

/** @var Row<mixed> $row */
foreach ($rows as $row) {
$node = $row->getNode();
$propertyName = $this->propertyEncoder->localizedContentName(RoutableSubscriber::ROUTE_FIELD_NAME, $locale);
$node->setProperty($propertyName, null);
}
}
}

private function getRoutePathPropertyName(string $structureType, string $locale): string
{
$metadata = $this->metadataFactory->getStructureMetadata('article', $structureType);

if ($metadata->hasPropertyWithTagName(RoutableSubscriber::TAG_NAME)) {
return $this->getPropertyName($locale, $metadata->getPropertyByTagName(RoutableSubscriber::TAG_NAME)->getName());
}

return $this->getPropertyName($locale, RoutableSubscriber::ROUTE_FIELD);
}

private function getPropertyName(string $locale, string $field): string
{
return $this->propertyEncoder->localizedSystemName($field, $locale);
}
}

0 comments on commit e39eadb

Please sign in to comment.