diff --git a/Classes/Archivist.php b/Classes/Archivist.php index f24e372..3e4b532 100644 --- a/Classes/Archivist.php +++ b/Classes/Archivist.php @@ -86,6 +86,7 @@ public function organizeNode(NodeInterface $triggeringNode, array $sortingInstru return; } } + if (isset($sortingInstructions['affectedNode'])) { $affectedNode = $this->eelEvaluationService->evaluate($sortingInstructions['affectedNode'], ['node' => $triggeringNode]); @@ -108,7 +109,7 @@ public function organizeNode(NodeInterface $triggeringNode, array $sortingInstru } if (isset($sortingInstructions['hierarchy']) && is_array($sortingInstructions['hierarchy'])) { - $hierarchyNode = $this->hierarchyService->buildHierarchy($sortingInstructions['hierarchy'], $context); + $hierarchyNode = $this->hierarchyService->buildHierarchy($sortingInstructions['hierarchy'], $context, $sortingInstructions['publishHierarchy'] ?? false); if ($affectedNode->getParent() !== $hierarchyNode) { $affectedNode->moveInto($hierarchyNode); diff --git a/Classes/NodeSignalInterceptor.php b/Classes/NodeSignalInterceptor.php index 78b0339..72efbdc 100644 --- a/Classes/NodeSignalInterceptor.php +++ b/Classes/NodeSignalInterceptor.php @@ -34,6 +34,7 @@ class NodeSignalInterceptor * @param NodeInterface $node * @throws Exception\ArchivistConfigurationException * @throws NodeTypeNotFoundException + * @throws \Neos\Eel\Exception */ public function nodeAdded(NodeInterface $node) { @@ -48,6 +49,7 @@ public function nodeAdded(NodeInterface $node) * @param NodeInterface $node * @throws Exception\ArchivistConfigurationException * @throws NodeTypeNotFoundException + * @throws \Neos\Eel\Exception */ public function nodeUpdated(NodeInterface $node) { diff --git a/Classes/Service/HierarchyService.php b/Classes/Service/HierarchyService.php index 822f802..a67f9df 100644 --- a/Classes/Service/HierarchyService.php +++ b/Classes/Service/HierarchyService.php @@ -10,6 +10,7 @@ */ use Neos\ContentRepository\Domain\Repository\NodeDataRepository; +use Neos\ContentRepository\Domain\Service\PublishingServiceInterface; use Neos\ContentRepository\Exception\NodeTypeNotFoundException; use Neos\Flow\Annotations as Flow; use Neos\ContentRepository\Domain\Model\NodeInterface; @@ -50,6 +51,12 @@ class HierarchyService */ protected $nodeDataRepository; + /** + * @Flow\Inject + * @var PublishingServiceInterface + */ + protected $publishingService; + /** * @Flow\Inject * @var SystemLoggerInterface @@ -59,18 +66,19 @@ class HierarchyService /** * @param array $hierarchyConfiguration * @param array $context + * @param bool $publishHierarchy Automatically publish the built hierarchy node to live workspace. * @return NodeInterface * @throws ArchivistConfigurationException * @throws NodeTypeNotFoundException * @throws \Neos\Eel\Exception */ - public function buildHierarchy(array $hierarchyConfiguration, array $context): NodeInterface + public function buildHierarchy(array $hierarchyConfiguration, array $context, bool $publishHierarchy = false): NodeInterface { $targetNode = null; $parent = $context['hierarchyRoot']; foreach ($hierarchyConfiguration as $hierarchyLevelConfiguration) { - $parent = $this->buildHierarchyLevel($parent, $hierarchyLevelConfiguration, $context); + $parent = $this->buildHierarchyLevel($parent, $hierarchyLevelConfiguration, $context, $publishHierarchy); } return $parent; @@ -80,12 +88,13 @@ public function buildHierarchy(array $hierarchyConfiguration, array $context): N * @param NodeInterface $parentNode * @param array $hierarchyLevelConfiguration * @param array $context + * @param bool $publishHierarchy * @return NodeInterface The created or found hierarchy node * @throws ArchivistConfigurationException * @throws NodeTypeNotFoundException * @throws \Neos\Eel\Exception */ - protected function buildHierarchyLevel(NodeInterface $parentNode, array $hierarchyLevelConfiguration, array $context): NodeInterface + protected function buildHierarchyLevel(NodeInterface $parentNode, array $hierarchyLevelConfiguration, array $context, bool $publishHierarchy): NodeInterface { $hierarchyLevelNodeName = ''; $this->evaluateHierarchyLevelConfiguration($hierarchyLevelConfiguration); @@ -129,6 +138,12 @@ protected function buildHierarchyLevel(NodeInterface $parentNode, array $hierarc $this->sortingService->sortChildren($hierarchyLevelNode, $hierarchyLevelConfiguration['sorting'], $hierarchyLevelNodeType->getName()); } + if ($publishHierarchy === true) { + if ($hierarchyLevelNode->getWorkspace()->isPublicWorkspace() === false) { + $this->publishNodeAndChildContent($hierarchyLevelNode); + } + } + $this->nodeDataRepository->persistEntities(); return $hierarchyLevelNode; @@ -194,4 +209,20 @@ protected function findExistingHierarchyNode(NodeInterface $parentNode, array $h return (new FlowQuery([$parentNode]))->children(sprintf('[instanceof %s][%s = "%s"]', $hierarchyLevelConfiguration['type'], $identifyingPropertyName, $identifyingValue))->get(0); } + /** + * @param NodeInterface $node + */ + protected function publishNodeAndChildContent(NodeInterface $node): void + { + $contentNodes = $node->getChildNodes('Neos.Neos:Content'); + + /** @var NodeInterface $contentNode */ + foreach ($contentNodes as $contentNode) { + if ($contentNode->getWorkspace()->isPublicWorkspace() === false) { + $this->publishNodeAndChildContent($contentNode); + } + } + + $this->publishingService->publishNode($node); + } } diff --git a/Configuration/Testing/Settings.yaml b/Configuration/Testing/Settings.yaml index 12b185c..f4f556c 100644 --- a/Configuration/Testing/Settings.yaml +++ b/Configuration/Testing/Settings.yaml @@ -19,6 +19,9 @@ PunktDe: # Optional: Trigger sorting only, when condition is met. Can be used to make sure that required properties are set as expected. condition: "${node.properties.date != null}" + # Automatically publish the created document hierarchy + publishHierarchy: true + # In the context is evaluated first. You can define variables here which you can use in # the remaining configuration context: diff --git a/Readme.md b/Readme.md index 8c56e94..26b72d7 100644 --- a/Readme.md +++ b/Readme.md @@ -44,6 +44,9 @@ PunktDe: context: publishDate: "${node.properties.date}" + # Automatically publish the created document hierarchy + publishHierarchy: true + # Definition of the auto-generated hierarchy hierarchy: - diff --git a/Tests/Functional/ArchivistTest.php b/Tests/Functional/ArchivistTest.php index c08270e..4fbb543 100644 --- a/Tests/Functional/ArchivistTest.php +++ b/Tests/Functional/ArchivistTest.php @@ -13,6 +13,9 @@ use Neos\ContentRepository\Domain\Model\NodeInterface; use Neos\ContentRepository\Domain\Model\NodeTemplate; use Neos\ContentRepository\Domain\Service\NodeTypeManager; +use Neos\ContentRepository\Exception\NodeException; +use Neos\ContentRepository\Exception\NodeExistsException; +use Neos\ContentRepository\Exception\NodeTypeNotFoundException; use Neos\ContentRepository\Tests\Functional\AbstractNodeTest; use Neos\Eel\Exception; use Neos\Eel\FlowQuery\FlowQuery; @@ -41,7 +44,7 @@ public function nodeStructureIsAvailable() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException */ public function simpleCreateNode() { @@ -63,10 +66,9 @@ public function simpleCreateNode() $this->assertEquals($this->nodeContextPath . '/2018/1/trigger-node', $newNode->getPath()); } - /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException */ public function doNotSortWhenConditionIsNotMet() { @@ -79,7 +81,22 @@ public function doNotSortWhenConditionIsNotMet() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException + */ + public function autoPublishingWorksIfConfigured() { + $this->createNode('trigger-node1', ['title' => 'New Article', 'date' => new \DateTime('2018-01-19')]); + $this->assertCount(1, $this->node->getChildNodes('PunktDe.Archivist.HierarchyNode')); + + $childNodes = $this->node->getChildNodes('PunktDe.Archivist.HierarchyNode'); + /** @var NodeInterface $hierarchyNode */ + $hierarchyNode = current($childNodes); + + $this->assertTrue($hierarchyNode->getWorkspace()->isPublicWorkspace()); + } + + /** + * @test + * @throws NodeTypeNotFoundException */ public function hierarchyIsNotCreatedTwice() { @@ -91,7 +108,7 @@ public function hierarchyIsNotCreatedTwice() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException */ public function hierarchyNodesAreSortedCorrectlyWithSimpleProperty() { @@ -105,7 +122,8 @@ public function hierarchyNodesAreSortedCorrectlyWithSimpleProperty() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException + * @throws Exception */ public function hierarchyNodesAreSortedCorrectlyWithEelExpression() { @@ -119,7 +137,8 @@ public function hierarchyNodesAreSortedCorrectlyWithEelExpression() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException + * @throws Exception */ public function createdNodesAreSortedCorrectly() { @@ -135,7 +154,8 @@ public function createdNodesAreSortedCorrectly() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException + * @throws Exception */ public function nodesAreSortedIfHierarchyAlreadyExist() { @@ -158,9 +178,9 @@ public function nodesAreSortedIfHierarchyAlreadyExist() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeException - * @throws \Neos\ContentRepository\Exception\NodeExistsException - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeException + * @throws NodeExistsException + * @throws NodeTypeNotFoundException */ public function documentNodeIsSortedByTriggeringContentNode() { @@ -172,9 +192,9 @@ public function documentNodeIsSortedByTriggeringContentNode() /** * @test - * @throws \Neos\ContentRepository\Exception\NodeException - * @throws \Neos\ContentRepository\Exception\NodeExistsException - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeException + * @throws NodeExistsException + * @throws NodeTypeNotFoundException */ public function documentNodeIsSortedByTriggeringContentNodeAndDocumentIsMovedAfterwards() { $unaffectedNode = $this->createNode('unaffect-node', ['title' => 'an unaffected node'], 'Neos.ContentRepository.Testing:Document'); @@ -192,7 +212,7 @@ public function documentNodeIsSortedByTriggeringContentNodeAndDocumentIsMovedAft /** * @test - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException */ public function contentNodeIsSortedInDocument() { $triggerNodeType = $this->nodeTypeManager->getNodeType('PunktDe.Archivist.TriggerContentNodeToBeSortedInDocument'); @@ -211,7 +231,7 @@ public function contentNodeIsSortedInDocument() { * @param array $properties * @param string $triggerNodeType * @return \Neos\ContentRepository\Domain\Model\NodeInterface - * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException + * @throws NodeTypeNotFoundException */ protected function createNode($nodeName = 'trigger-node', array $properties = [], $triggerNodeType = 'PunktDe.Archivist.TriggerNode') {