Skip to content

Commit

Permalink
TASK: add additional data source parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jobee committed Aug 31, 2020
1 parent 4774dea commit 583cae7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
46 changes: 38 additions & 8 deletions Classes/DataSource/NodeDataDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
namespace Tms\Select\DataSource;

use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Neos\Domain\Service\ContentContext;
use Neos\Neos\Domain\Service\ContentContextFactory;
use Neos\Neos\Service\DataSource\AbstractDataSource;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;

class NodeDataDataSource extends AbstractDataSource {

Expand All @@ -12,6 +15,12 @@ class NodeDataDataSource extends AbstractDataSource {
*/
static protected $identifier = 'tms-select-nodedata';

/**
* @Flow\Inject
* @var ContentContextFactory
*/
protected $contentContextFactory;

/**
* Get data
*
Expand All @@ -21,41 +30,62 @@ class NodeDataDataSource extends AbstractDataSource {
*/
public function getData(NodeInterface $node = NULL, array $arguments)
{
$rootNode = $node->getContext()->getCurrentSiteNode();
/** @var ContentContext $contentContext */
$contentContext = $this->contentContextFactory->create(array($node));

if (isset($arguments['startingPoint']))
$rootNode = $contentContext->getNode($arguments['startingPoint']);
else
$rootNode = $contentContext->getRootNode();

$q = new FlowQuery(array($rootNode));
$nodes = array();

if (!isset($arguments['nodeType']))
if (!isset($arguments['nodeType']) && !isset($arguments['nodeTypes']))
return array();
if (isset($arguments['nodeType']))
$nodeTypes = array($arguments['nodeType']);
if (isset($arguments['nodeTypes']))
$nodeTypes = $arguments['nodeTypes'];

$labelPropertyName = null;
if (isset($arguments['labelPropertyName']))
$labelPropertyName = $arguments['labelPropertyName'];

if (isset($arguments['groupBy'])) {
foreach ($q->find('[instanceof ' . $arguments['groupBy'] . ']')->get() as $parentNode) {
$nodes = array_merge($nodes, $this->getNodes($parentNode, $arguments['nodeType'], $arguments['groupBy']));
$nodes = array_merge($nodes, $this->getNodes($parentNode, $nodeTypes, $labelPropertyName, $arguments['groupBy']));
}
} else {
$nodes = $this->getNodes($rootNode, $arguments['nodeType']);
$nodes = $this->getNodes($rootNode, $nodeTypes, $labelPropertyName);
}

return $nodes;
}

/**
* @param NodeInterface $parentNode
* @param string $nodeType
* @param array $nodeTypes
* @param string|null $labelPropertyName
* @param string|null $groupBy
*
* @return array
*/
protected function getNodes(NodeInterface $parentNode, $nodeType, $groupBy = null)
protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropertyName = null, $groupBy = null)
{
$q = new FlowQuery(array($parentNode));
$nodes = array();

foreach ($q->find('[instanceof ' . $nodeType . ']')->get() as $node) {
$filter = [];
foreach ($nodeTypes as $nodeType)
$filter[] = '[instanceof ' . $nodeType . ']';
$filterString = implode(',', $filter);

foreach ($q->find($filterString)->get() as $node) {
if ($node instanceof NodeInterface) {
$nodes[] = array(
'value' => $node->getIdentifier(),
'label' => $node->getLabel(),
'label' => $labelPropertyName ? $node->getProperty($labelPropertyName) : $node->getLabel(),
'group' => ($groupBy !== null ? $parentNode->getLabel() : null)
);
}
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ composer require tms/select
editorOptions:
dataSourceIdentifier: 'tms-select-nodedata'
dataSourceAdditionalData:
nodeType: 'Your.Package:TypeThatShouldBeReferenced'
nodeTypes: ['Your.Package:TypeThatShouldBeReferenced']
# Optional parameters
groupBy: 'Your.Package:GroupType'
startingPoint: '/start/here/instead/of/rootnode'
labelPropertyName: 'title'
```
## Acknowledgments
Expand Down

0 comments on commit 583cae7

Please sign in to comment.