Skip to content

Instantly share code, notes, and snippets.

@Sebobo
Last active October 2, 2015 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebobo/4bd90b64573c2098fc5b to your computer and use it in GitHub Desktop.
Save Sebobo/4bd90b64573c2098fc5b to your computer and use it in GitHub Desktop.
Route part handler allowing to ignore nodes
<?php
namespace Foo\Bar\Routing;
/* *
* This script belongs to the TYPO3 Flow package "Foo.Bar". *
* */
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Security\Authorization\AccessDecisionManagerInterface;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\Neos\Routing\Exception as Exception;
/**
* A route part handler for finding nodes specifically in the website's frontend.
*/
class FrontendNodeRoutePartHandler extends \TYPO3\Neos\Routing\FrontendNodeRoutePartHandler
{
/**
* @Flow\Inject
* @var AccessDecisionManagerInterface
*/
protected $accessDecisionManager;
/**
* @param string $requestPath
* @return string
*/
protected function getWorkspaceName($requestPath)
{
$contextPathParts = array();
if ($requestPath !== '' && strpos($requestPath, '@') !== false) {
preg_match(NodeInterface::MATCH_PATTERN_CONTEXTPATH, $requestPath, $contextPathParts);
}
return isset($contextPathParts['WorkspaceName']) && $contextPathParts['WorkspaceName'] !== '' ? $contextPathParts['WorkspaceName'] : 'live';
}
/**
* @inheritdoc
*/
protected function getRelativeNodePathByUriPathSegmentProperties(NodeInterface $siteNode, $relativeRequestPath)
{
/** @var NodeInterface $node */
$node = $siteNode;
$relativeNodePathSegments = array();
$workspaceName = $this->getWorkspaceName($relativeRequestPath);
foreach (explode('/', $relativeRequestPath) as $pathSegment) {
$node = $this->findNextNodeWithPathSegmentRecursively($node, $pathSegment, $relativeNodePathSegments,
$workspaceName);
if ($node === null) {
return false;
}
}
return implode('/', $relativeNodePathSegments);
}
/**
* @param NodeInterface $node
* @param string $pathSegment
* @param array $relativeNodePathSegments
* @param string $workspaceName
* @return NodeInterface
*/
protected function findNextNodeWithPathSegmentRecursively(
NodeInterface $node,
$pathSegment,
&$relativeNodePathSegments,
$workspaceName
) {
/** @var NodeInterface $node */
foreach ($node->getChildNodes('TYPO3.Neos:Document') as $node) {
if ($workspaceName == 'live' && $node->hasProperty('hideSegmentInUriPath') && $node->getProperty('hideSegmentInUriPath') == true) {
$foundNode = $this->findNextNodeWithPathSegmentRecursively($node, $pathSegment,
$relativeNodePathSegments, $workspaceName);
if ($foundNode !== null) {
array_unshift($relativeNodePathSegments, $node->getName());
return $foundNode;
}
}
if ($node->getProperty('uriPathSegment') === $pathSegment) {
$relativeNodePathSegments[] = $node->getName();
return $node;
}
}
return null;
}
/**
* @inheritdoc
*/
protected function getRequestPathByNode(NodeInterface $siteNode, NodeInterface $node)
{
if ($siteNode === $node) {
return '';
}
$startingNode = $node;
$workspaceName = $node->getContext()->getWorkspaceName();
$requestPathSegments = array();
while ($siteNode !== $node && $node instanceof NodeInterface) {
if (!$node->hasProperty('uriPathSegment')) {
throw new Exception\MissingNodePropertyException(sprintf('Missing "uriPathSegment" property for node "%s". Nodes can be migrated with the "flow node:repair" command.',
$node->getPath()), 1415020326);
}
if ($workspaceName !== 'live' || $startingNode === $node || !$node->hasProperty('hideSegmentInUriPath') || !$node->getProperty('hideSegmentInUriPath') == true) {
$pathSegment = $node->getProperty('uriPathSegment');
$requestPathSegments[] = $pathSegment;
}
$node = $node->getParent();
}
return implode('/', array_reverse($requestPathSegments));
}
}
'TYPO3.Neos:Document':
properties:
hideSegmentInUriPath:
type: boolean
ui:
label: 'Hide page url segment in URLs'
TYPO3\Neos\Routing\FrontendNodeRoutePartHandlerInterface:
className: Foo\Bar\Routing\FrontendNodeRoutePartHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment