Skip to content

Instantly share code, notes, and snippets.

@aertmann
Created March 6, 2014 11:49
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 aertmann/9388012 to your computer and use it in GitHub Desktop.
Save aertmann/9388012 to your computer and use it in GitHub Desktop.
Find operation with recursive filtering support and identifier support. Fallback of https://review.typo3.org/#/q/status:open+topic:find-operation,n,z for Neos 1.0. Usage: ${q(site).find('[instanceof TYPO3.Neos.NodeTypes:Text]')} // ${q(site).find('#60216562-0ad9-86ff-0b32-7b7072bcb6b2')}
<?php
namespace Moc\Net\TypoScript\FlowQueryOperations;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Eel\FlowQuery\FlowQuery;
use TYPO3\TYPO3CR\Domain\Repository\NodeDataRepository;
/**
* Override the built in Neos PropertyOperation so the operation can take a default value
*/
class FindOperation extends \TYPO3\Neos\TypoScript\FlowQueryOperations\FindOperation {
/**
* {@inheritdoc}
*
* @var integer
*/
static protected $priority = 200;
/**
* @Flow\Inject
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* @Flow\Inject
* @var \TYPO3\TYPO3CR\Domain\Factory\NodeFactory
*/
protected $nodeFactory;
/**
* {@inheritdoc}
* @TODO This will become obsolete with the next official Neos release
*
* @param FlowQuery $flowQuery the FlowQuery object
* @param array $arguments the arguments for this operation
* @return void
*/
public function evaluate(FlowQuery $flowQuery, array $arguments) {
$context = $flowQuery->getContext();
if (!isset($context[0])) {
return NULL;
}
/** @var \TYPO3\TYPO3CR\Domain\Model\NodeInterface $contextNode */
$contextNode = $context[0];
$selectorAndFilter = $arguments[0];
$parsedFilter = \TYPO3\Eel\FlowQuery\FizzleParser::parseFilterGroup($selectorAndFilter);
if ($selectorAndFilter[0] === '#') {
$context = $contextNode->getContext();
$nodeData = $this->nodeDataRepository->findOneByIdentifier(substr($selectorAndFilter, 1), $context->getWorkspace());
if ($nodeData !== NULL) {
$node = $this->nodeFactory->createFromNodeData($nodeData, $context);
}
$result = array($node);
} elseif (isset($parsedFilter['Filters'][0]['AttributeFilters']) && $parsedFilter['Filters'][0]['AttributeFilters'][0]['Operator'] === 'instanceof') {
$nodeTypeFilter = $parsedFilter['Filters'][0]['AttributeFilters'][0]['Operand'];
$context = $contextNode->getContext();
$nodeDataElements = $this->nodeDataRepository->findByParentAndNodeType($contextNode->getPath(), $nodeTypeFilter, $context->getWorkspace(), NULL, NULL, ($context->isRemovedContentShown() ? NULL : FALSE), TRUE);
$result = array();
foreach ($nodeDataElements as $nodeData) {
$node =$this->nodeFactory->createFromNodeData($nodeData, $context);
if ($node !== NULL) {
$result[] = $node;
}
}
} else {
$result = array($contextNode->getNode($selectorAndFilter));
}
$flowQuery->setContext($result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment