Skip to content

Instantly share code, notes, and snippets.

@lorenzulrich
Last active January 23, 2018 18:25
Show Gist options
  • Save lorenzulrich/d9c99ff734e93d508bcb874b0011693e to your computer and use it in GitHub Desktop.
Save lorenzulrich/d9c99ff734e93d508bcb874b0011693e to your computer and use it in GitHub Desktop.
Neos CMS FlowQuery operation to filter a collection of Nodes having a reference to a given Node
<?php
namespace Visol\FoobarCom\FlowQuery\Operation;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Eel\FlowQuery\Operations\AbstractOperation;
class ContainsReferenceToNodeOperation extends AbstractOperation
{
/**
* {@inheritdoc}
*
* @var string
*/
static protected $shortName = 'containsReferenceToNode';
/**
* {@inheritdoc}
*
* @param FlowQuery $flowQuery the FlowQuery object
* @param array $arguments the arguments for this operation
* @return void
*/
public function evaluate(FlowQuery $flowQuery, array $arguments)
{
/** @var string $propertyName */
$propertyName = $arguments[0];
/** @var NodeInterface $node */
$nodeToCheckForReference = $arguments[1];
$context = $flowQuery->getContext();
foreach ($context as $key => $node) {
/** @var $node NodeInterface */
$references = $node->getProperty($propertyName);
$deleteFromContext = true;
if (is_array($references)) {
foreach ($references as $referencedNode) {
/** @var $referencedNode NodeInterface */
if ($referencedNode === $nodeToCheckForReference) {
// The node is references, so it stays in the context
$deleteFromContext = false;
break;
}
}
}
// Node is not referenced
if ($deleteFromContext) {
unset($context[$key]);
}
}
$flowQuery->setContext(array_values($context));
}
}
teachers = Neos.Fusion:Collection {
collection = ${q(site).find('[instanceof Visol.FoobarCom:Document.Teacher]').containsReferenceToNode('offers', node).get()}
itemName = 'teacher'
itemRenderer = Visol.FoobarCom:Component.Molecule.TeacherTeaser {
teacher = ${teacher}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment