Skip to content

Commit

Permalink
Merge pull request #5 from punktDe/feature/publish-hierarchy-nodes
Browse files Browse the repository at this point in the history
FEATURE: Automatically publish hierarchy nodes
  • Loading branch information
daniellienert authored Oct 18, 2018
2 parents 3e59367 + d1a127e commit 33e9ab7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Classes/Archivist.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function organizeNode(NodeInterface $triggeringNode, array $sortingInstru
return;
}
}

if (isset($sortingInstructions['affectedNode'])) {
$affectedNode = $this->eelEvaluationService->evaluate($sortingInstructions['affectedNode'], ['node' => $triggeringNode]);

Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Classes/NodeSignalInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class NodeSignalInterceptor
* @param NodeInterface $node
* @throws Exception\ArchivistConfigurationException
* @throws NodeTypeNotFoundException
* @throws \Neos\Eel\Exception
*/
public function nodeAdded(NodeInterface $node)
{
Expand All @@ -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)
{
Expand Down
37 changes: 34 additions & 3 deletions Classes/Service/HierarchyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,12 @@ class HierarchyService
*/
protected $nodeDataRepository;

/**
* @Flow\Inject
* @var PublishingServiceInterface
*/
protected $publishingService;

/**
* @Flow\Inject
* @var SystemLoggerInterface
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions Configuration/Testing/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
-
Expand Down
52 changes: 36 additions & 16 deletions Tests/Functional/ArchivistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +44,7 @@ public function nodeStructureIsAvailable()

/**
* @test
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws NodeTypeNotFoundException
*/
public function simpleCreateNode()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -91,7 +108,7 @@ public function hierarchyIsNotCreatedTwice()

/**
* @test
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws NodeTypeNotFoundException
*/
public function hierarchyNodesAreSortedCorrectlyWithSimpleProperty()
{
Expand All @@ -105,7 +122,8 @@ public function hierarchyNodesAreSortedCorrectlyWithSimpleProperty()

/**
* @test
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws NodeTypeNotFoundException
* @throws Exception
*/
public function hierarchyNodesAreSortedCorrectlyWithEelExpression()
{
Expand All @@ -119,7 +137,8 @@ public function hierarchyNodesAreSortedCorrectlyWithEelExpression()

/**
* @test
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws NodeTypeNotFoundException
* @throws Exception
*/
public function createdNodesAreSortedCorrectly()
{
Expand All @@ -135,7 +154,8 @@ public function createdNodesAreSortedCorrectly()

/**
* @test
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws NodeTypeNotFoundException
* @throws Exception
*/
public function nodesAreSortedIfHierarchyAlreadyExist()
{
Expand All @@ -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()
{
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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')
{
Expand Down

0 comments on commit 33e9ab7

Please sign in to comment.