-
-
Save gerhard-boden/e4a3883f6d38095744aaaed7c7f6b19e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GesagtGetan\PathOverride\Routing; | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\TYPO3CR\Domain\Model\NodeInterface; | |
/** | |
* A route part handler for finding nodes that use the `uriPathSegmentOverride` field | |
*/ | |
class PathOverrideRoutePartHandler extends \TYPO3\Neos\Routing\FrontendNodeRoutePartHandler | |
{ | |
/** | |
* @Flow\Inject | |
* @var \Doctrine\Common\Persistence\ObjectManager | |
*/ | |
protected $entityManager; | |
/** | |
* Find a node path using the `uriPathSegmentOverride` or fall back to the standard behaviour. | |
* | |
* @param NodeInterface $siteNode The site node, used as a starting point while traversing the tree | |
* @param string $relativeRequestPath The request path, relative to the site's root path | |
* @throws \TYPO3\Neos\Routing\Exception\NoSuchNodeException | |
* @return string | |
*/ | |
protected function getRelativeNodePathByUriPathSegmentProperties(NodeInterface $siteNode, $relativeRequestPath) | |
{ | |
$nodePath = $this->getNodeNameForUriPathSegmentOverride($relativeRequestPath); | |
if ($nodePath !== false) { | |
return $nodePath; | |
} else { | |
return parent::getRelativeNodePathByUriPathSegmentProperties($siteNode, $relativeRequestPath); | |
} | |
} | |
/** | |
* Renders a request path based on the 'uriPathSegmentOverride' or falls back to the standard behaviour if none is given. | |
* | |
* @param NodeInterface $siteNode Top level node, corresponds to the top level of the request path | |
* @param NodeInterface $node The node where the generated path should lead to | |
* @return string A relative request path | |
* @throws Exception\MissingNodePropertyException if the given node doesn't have a "uriPathSegment" property set | |
*/ | |
protected function getRequestPathByNode(NodeInterface $siteNode, NodeInterface $node) | |
{ | |
$overridePathSegment = $node->getProperty('uriPathSegmentOverride'); | |
if ($overridePathSegment !== null && $overridePathSegment !== '') { | |
return $overridePathSegment; | |
} else { | |
return parent::getRequestPathByNode($siteNode, $node); | |
} | |
} | |
/** | |
* Query the database for node with a specific value set for `uriPathSegmentOverride`. | |
* | |
* @param $relativeRequestPath | |
* @return string|bool | |
*/ | |
private function getNodeNameForUriPathSegmentOverride($relativeRequestPath) | |
{ | |
$queryBuilder = $this->entityManager->createQueryBuilder(); | |
$queryBuilder->select('n') | |
->distinct() | |
->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n') | |
->where('n.workspace = :workspace') | |
->setParameter('workspace', 'live') | |
->andWhere('n.properties LIKE :uriPathSegmentOverride') | |
->setParameter('uriPathSegmentOverride', | |
'%"uriPathSegmentOverride"%' . str_replace('/', '\\\\\\/', ltrim($relativeRequestPath, '/')) . '%'); | |
$query = $queryBuilder->getQuery(); | |
$nodes = $query->getResult(); | |
if (empty($nodes)) { | |
return false; | |
} | |
return $nodes[0]->getPath(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment