Skip to content

Instantly share code, notes, and snippets.

@mficzel
Last active April 10, 2020 09:31
Show Gist options
  • Save mficzel/30beba6919d18ee1780cb163e00ed1c1 to your computer and use it in GitHub Desktop.
Save mficzel/30beba6919d18ee1780cb163e00ed1c1 to your computer and use it in GitHub Desktop.
Filter list by reference intersection
videos = Neos.Fusion:Loop {
items = Vendor.Site:VideoNodesByTag {
tags =
}
renderer = afx`<div>{item.title}</div>`
}
prototype(Vendor.Site:VideoNodesByTag) < prototype(Neos.Fusion:Component) {
# the tags to search for
tags = null
# the video list, by default all videos in the site
allVideos = ${q(site).find('[instanceof Vendor.Site:Videos]').get()}
renderer = Neos.Fusion:Reduce {
items = ${props.allVideos}
carryName = 'carry'
itemName = 'item'
# by default the result list is empty array
initialValue = ${[]}
#
# if no intersection between the tags the carry is returned directly otherwise the item is pushed to the result
# this is based on the behavior of php that considers and empty array [] falsy and thus will
# use the second part of the ternary if no intersection is found
#
itemReducer = ${(CustomArray.intersect(props.tags, item.tags)) ? Array.push(carry, item) : carry}
}
}
<?php
namespace Vendor\Site\Eel
use Neos\Eel\ProtectedContextAwareInterface;
class CustomArrayHelper implements ProtectedContextAwareInterface
{
/**
* @param iterable $array Array of elements to test
* @param iterable $array Array of elements to test
* @return array the elements that are present in both array
*/
public function itersect(iterable $a, iterable $b): array
{
if ($a instanceof \Traversable) {
$a = iterator_to_array($array);
}
if ($b instanceof \Traversable) {
$b = iterator_to_array($array);
}
return array_intersect($a, $b);
}
/**
* All methods are considered safe
*
* @param string $methodName
* @return bool
*/
public function allowsCallOfMethod($methodName): bool
{
return true;
}
}
Neos:
Fusion:
defaultContext:
CustomArray: 'Vendor\Site\Eel\CustomArrayHelper'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment