Skip to content

Commit

Permalink
Merge pull request #11 from gerhard-boden/create-redirects-for-all-fa…
Browse files Browse the repository at this point in the history
…llback-dimensions

BUGFIX: Fix behaviour for fallback dimensions
  • Loading branch information
kdambekalns authored Dec 4, 2018
2 parents b7d74d2 + e62b07e commit 414f12a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
preset: psr2

finder:
path:
- "Classes"
- "Tests"
69 changes: 53 additions & 16 deletions Classes/Service/NodeRedirectService.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
use TYPO3\Flow\Persistence\PersistenceManagerInterface;
use TYPO3\Neos\Domain\Model\Domain;
use TYPO3\Neos\Domain\Service\ContentContext;
use TYPO3\TYPO3CR\Domain\Service\ContextFactoryInterface;
use TYPO3\Neos\Routing\Exception;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TYPO3CR\Domain\Model\Workspace;
use TYPO3\TYPO3CR\Domain\Repository\NodeDataRepository;
use TYPO3\TYPO3CR\Domain\Service\ContentDimensionCombinator;
use TYPO3\TYPO3CR\Domain\Service\ContextFactoryInterface;

/**
* Service that creates redirects for moved / deleted nodes.
Expand Down Expand Up @@ -85,6 +86,12 @@ class NodeRedirectService implements NodeRedirectServiceInterface
*/
protected $defaultStatusCode;

/**
* @Flow\Inject
* @var ContentDimensionCombinator
*/
protected $contentDimensionCombinator;

/**
* @Flow\InjectConfiguration(path="enableRemovedNodeRedirect", package="Neos.RedirectHandler.NeosAdapter")
* @var array
Expand All @@ -96,28 +103,39 @@ class NodeRedirectService implements NodeRedirectServiceInterface
*/
public function createRedirectsForPublishedNode(NodeInterface $node, Workspace $targetWorkspace)
{
try {
$this->executeRedirectsForPublishedNode($node, $targetWorkspace);
} catch (\Exception $exception) {
$this->systemLogger->log(sprintf('Can not create redirect for the node = %s in workspace = %s. See original exception: "%s"', $node->getContextPath(), $targetWorkspace->getName(), $exception->getMessage()), LOG_WARNING);
$nodeType = $node->getNodeType();
if ($targetWorkspace->getName() !== 'live' || !$nodeType->isOfType('TYPO3.Neos:Document')) {
return;
}
$this->createRedirectsForNodesInDimensions($node, $targetWorkspace);
}

/**
* Creates a redirect for the node if it is a 'TYPO3.Neos:Document' node and its URI has changed
* Cycle dimensions and create redirects if necessary.
*
* @param NodeInterface $node The node that is about to be published
* @param Workspace $targetWorkspace
* @return void
* @throws Exception
* @param $node
* @param $targetWorkspace
*/
protected function executeRedirectsForPublishedNode(NodeInterface $node, Workspace $targetWorkspace)
protected function createRedirectsForNodesInDimensions(NodeInterface $node, Workspace $targetWorkspace)
{
$nodeType = $node->getNodeType();
if ($targetWorkspace->getName() !== 'live' || !$nodeType->isOfType('TYPO3.Neos:Document')) {
return;
foreach ($this->contentDimensionCombinator->getAllAllowedCombinations() as $allowedCombination) {
$nodeInDimensions = $this->getNodeInDimensions($node, $allowedCombination);
if ($nodeInDimensions === null) {
continue;
}

$this->createRedirect($nodeInDimensions, $targetWorkspace);
}
}

/**
* Creates the actual redirect for the given node and possible children.
*
* @param NodeInterface $node
* @param Workspace $targetWorkspace
*/
protected function createRedirect(NodeInterface $node, Workspace $targetWorkspace)
{
$context = $this->contextFactory->create([
'workspaceName' => 'live',
'invisibleContentShown' => true,
Expand All @@ -126,7 +144,7 @@ protected function executeRedirectsForPublishedNode(NodeInterface $node, Workspa

$targetNode = $context->getNodeByIdentifier($node->getIdentifier());
if ($targetNode === null) {
// The page has been added
// The page has been added or is not available in live context for the given dimension
return;
}

Expand Down Expand Up @@ -162,11 +180,12 @@ protected function executeRedirectsForPublishedNode(NodeInterface $node, Workspa

$this->flushRoutingCacheForNode($targetNode);
$statusCode = (integer)$this->defaultStatusCode['redirect'];

$this->redirectStorage->addRedirect($targetNodeUriPath, $nodeUriPath, $statusCode, $hosts);

$q = new FlowQuery([$node]);
foreach ($q->children('[instanceof TYPO3.Neos:Document]') as $childrenNode) {
$this->executeRedirectsForPublishedNode($childrenNode, $targetWorkspace);
$this->createRedirect($childrenNode, $targetWorkspace);
}
}

Expand Down Expand Up @@ -240,4 +259,22 @@ protected function getUriBuilder()
}
return $this->uriBuilder;
}

/**
* Get the given node in the given dimensions.
* If it doesn't exist the method returns null.
*
* @param NodeInterface $node
* @param array $dimensions
* @return NodeInterface|null
*/
protected function getNodeInDimensions(NodeInterface $node, array $dimensions)
{
$context = $this->contextFactory->create([
'workspaceName' => $node->getWorkspace()->getName(),
'dimensions' => $dimensions,
'invisibleContentShown' => true,
]);
return $context->getNode($node->getPath());
}
}
9 changes: 2 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
"description": "Neos Redirect Handler",
"license": "GPL-3.0-or-later",
"require": {
"neos/redirecthandler": "~1.0",
"typo3/neos": "~2.2 || dev-master"
"neos/redirecthandler": "^1.0",
"typo3/neos": "^2.2"
},
"autoload": {
"psr-4": {
"Neos\\RedirectHandler\\NeosAdapter\\": "Classes"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}

0 comments on commit 414f12a

Please sign in to comment.