Skip to content

Commit

Permalink
Add shadow functionality to publishing process
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Mar 23, 2023
1 parent 34f3fad commit 89f692c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
15 changes: 9 additions & 6 deletions Content/Application/ContentCopier/ContentCopier.php
Expand Up @@ -59,29 +59,32 @@ public function copy(
ContentRichEntityInterface $sourceContentRichEntity,
array $sourceDimensionAttributes,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
array $targetDimensionAttributes,
array $data = []
): DimensionContentInterface {
$sourceDimensionContent = $this->contentResolver->resolve($sourceContentRichEntity, $sourceDimensionAttributes);

return $this->copyFromDimensionContent($sourceDimensionContent, $targetContentRichEntity, $targetDimensionAttributes);
return $this->copyFromDimensionContent($sourceDimensionContent, $targetContentRichEntity, $targetDimensionAttributes, $data);
}

public function copyFromDimensionContentCollection(
DimensionContentCollectionInterface $dimensionContentCollection,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
array $targetDimensionAttributes,
array $data = []
): DimensionContentInterface {
$sourceDimensionContent = $this->contentMerger->merge($dimensionContentCollection);

return $this->copyFromDimensionContent($sourceDimensionContent, $targetContentRichEntity, $targetDimensionAttributes);
return $this->copyFromDimensionContent($sourceDimensionContent, $targetContentRichEntity, $targetDimensionAttributes, $data);
}

public function copyFromDimensionContent(
DimensionContentInterface $dimensionContent,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
array $targetDimensionAttributes,
array $data = []
): DimensionContentInterface {
$data = $this->contentNormalizer->normalize($dimensionContent);
$data = \array_merge($this->contentNormalizer->normalize($dimensionContent), $data);

return $this->contentPersister->persist($targetContentRichEntity, $data, $targetDimensionAttributes);
}
Expand Down
12 changes: 9 additions & 3 deletions Content/Application/ContentCopier/ContentCopierInterface.php
Expand Up @@ -26,14 +26,16 @@ interface ContentCopierInterface
* @param mixed[] $sourceDimensionAttributes
* @param ContentRichEntityInterface<T> $targetContentRichEntity
* @param mixed[] $targetDimensionAttributes
* @param mixed[] $data This data overwrites the data of the source content
*
* @return T
*/
public function copy(
ContentRichEntityInterface $sourceContentRichEntity,
array $sourceDimensionAttributes,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
array $targetDimensionAttributes,
array $data = []
): DimensionContentInterface;

/**
Expand All @@ -42,13 +44,15 @@ public function copy(
* @param DimensionContentCollectionInterface<T> $dimensionContentCollection
* @param ContentRichEntityInterface<T> $targetContentRichEntity
* @param mixed[] $targetDimensionAttributes
* @param mixed[] $data This data overwrites the data of the source content
*
* @return T
*/
public function copyFromDimensionContentCollection(
DimensionContentCollectionInterface $dimensionContentCollection,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
array $targetDimensionAttributes,
array $data = []
): DimensionContentInterface;

/**
Expand All @@ -57,12 +61,14 @@ public function copyFromDimensionContentCollection(
* @param T $dimensionContent
* @param ContentRichEntityInterface<T> $targetContentRichEntity
* @param mixed[] $targetDimensionAttributes
* @param mixed[] $data This data overwrites the data of the source content
*
* @return T
*/
public function copyFromDimensionContent(
DimensionContentInterface $dimensionContent,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
array $targetDimensionAttributes,
array $data = []
): DimensionContentInterface;
}
Expand Up @@ -18,6 +18,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ShadowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;
Expand Down Expand Up @@ -66,12 +67,39 @@ public function onPublish(TransitionEvent $transitionEvent): void
throw new \RuntimeException('No "contentRichEntity" given.');
}

$dimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;
$sourceDimensionAttributes = $dimensionAttributes;
$targetDimensionAttributes = $dimensionAttributes;
$targetDimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;

$this->contentCopier->copyFromDimensionContentCollection(
$dimensionContentCollection,
$currentDimensionContent = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

$shadowLocale = $currentDimensionContent instanceof ShadowInterface
? $currentDimensionContent->getShadowLocale()
: null;

if (!$shadowLocale) {
$this->contentCopier->copyFromDimensionContentCollection(
$dimensionContentCollection,
$contentRichEntity,
$targetDimensionAttributes
);

return;
}

$sourceDimensionAttributes['locale'] = $shadowLocale;
$sourceDimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;

$this->contentCopier->copy(
$contentRichEntity,
$sourceDimensionAttributes,
$contentRichEntity,
$dimensionAttributes
$targetDimensionAttributes,
[
// @see \Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\ShadowDataMapper::map
'shadowOn' => true,
'shadowLocale' => $shadowLocale,
]
);
}

Expand Down
3 changes: 0 additions & 3 deletions Content/Domain/Model/RoutableInterface.php
Expand Up @@ -13,9 +13,6 @@

namespace Sulu\Bundle\ContentBundle\Content\Domain\Model;

/**
* Marker interface for autoloading the doctrine metadata for routables.
*/
interface RoutableInterface
{
public static function getResourceKey(): string;
Expand Down
10 changes: 5 additions & 5 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Expand Up @@ -60,6 +60,11 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
$this->addIndex($metadata, 'idx_stage', ['stage']);
}

if ($reflection->implementsInterface(ShadowInterface::class)) {
$this->addField($metadata, 'shadowLocale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'shadowLocales', 'json', ['nullable' => true, 'options' => ['jsonb' => true]]);
}

if ($reflection->implementsInterface(SeoInterface::class)) {
$this->addField($metadata, 'seoTitle');
$this->addField($metadata, 'seoDescription', 'text');
Expand Down Expand Up @@ -111,11 +116,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
$this->addField($metadata, 'mainWebspace', 'string', ['nullable' => true]);
}

if ($reflection->implementsInterface(ShadowInterface::class)) {
$this->addField($metadata, 'shadowLocale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'shadowLocales', 'json', ['nullable' => true, 'options' => ['jsonb' => true]]);
}

if ($reflection->implementsInterface(AuthorInterface::class)) {
$this->addField($metadata, 'authored', 'datetime_immutable', ['nullable' => true]);
$this->addManyToOne($event, $metadata, 'author', ContactInterface::class, true);
Expand Down

0 comments on commit 89f692c

Please sign in to comment.