Skip to content

Commit

Permalink
BUGFIX: use correct content context in Flow queries, add new feature …
Browse files Browse the repository at this point in the history
…to prefix labels by their node context (hidden, not in menus, not live, removed)
  • Loading branch information
jobee committed Oct 18, 2021
1 parent 7db3db4 commit b4dbba9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
67 changes: 47 additions & 20 deletions Classes/DataSource/NodeDataDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@
use Neos\Media\Domain\Model\ImageInterface;
use Neos\Media\Domain\Model\ThumbnailConfiguration;
use Neos\Media\Domain\Service\AssetService;
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 {

class NodeDataDataSource extends AbstractDataSource
{
/**
* @var string
*/
static protected $identifier = 'tms-select-nodedata';

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

/**
* @Flow\Inject
* @var AssetService
Expand All @@ -39,15 +31,13 @@ class NodeDataDataSource extends AbstractDataSource {
*/
public function getData(NodeInterface $node = NULL, array $arguments = [])
{
/** @var ContentContext $contentContext */
$contentContext = $this->contentContextFactory->create(array($node));

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

$q = new FlowQuery(array($rootNode));
$q = $q->context(['invisibleContentShown' => true, 'removedContentShown' => true, 'inaccessibleContentShown' => true]);
$nodes = array();

if (!isset($arguments['nodeType']) && !isset($arguments['nodeTypes']))
Expand All @@ -57,6 +47,9 @@ public function getData(NodeInterface $node = NULL, array $arguments = [])
if (isset($arguments['nodeTypes']))
$nodeTypes = $arguments['nodeTypes'];

$setLabelPrefixByNodeContext = false;
if (isset($arguments['setLabelPrefixByNodeContext']) && $arguments['setLabelPrefixByNodeContext'] == true)
$setLabelPrefixByNodeContext = true;
$labelPropertyName = null;
if (isset($arguments['labelPropertyName']))
$labelPropertyName = $arguments['labelPropertyName'];
Expand All @@ -66,10 +59,10 @@ public function getData(NodeInterface $node = NULL, array $arguments = [])

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

return $nodes;
Expand All @@ -80,13 +73,15 @@ public function getData(NodeInterface $node = NULL, array $arguments = [])
* @param array $nodeTypes
* @param string|null $labelPropertyName
* @param string|null $previewPropertyName
* @param boolean $labelPrefixNodeContext
* @param string|null $groupBy
*
* @return array
*/
protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropertyName = null, $previewPropertyName = null, $groupBy = null)
protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropertyName = null, $previewPropertyName = null, $setLabelPrefixByNodeContext = false, $groupBy = null)
{
$q = new FlowQuery(array($parentNode));
$q = $q->context(['invisibleContentShown' => true, 'removedContentShown' => true, 'inaccessibleContentShown' => true]);
$nodes = array();

$filter = [];
Expand All @@ -107,14 +102,46 @@ protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropert
continue;
$preview = $thumbnail['src'];
}

$label = $labelPropertyName ? $node->getProperty($labelPropertyName) : $node->getLabel();
$groupLabel = $parentNode->getLabel();

if ($setLabelPrefixByNodeContext) {
$label = $this->getLabelPrefixByNodeContext($node, $label);
$groupLabel = $this->getLabelPrefixByNodeContext($parentNode, $groupLabel);
}

$nodes[] = array(
'value' => $node->getIdentifier(),
'label' => $labelPropertyName ? $node->getProperty($labelPropertyName) : $node->getLabel(),
'group' => ($groupBy !== null ? $parentNode->getLabel() : null),
'label' => $label,
'group' => ($groupBy !== null ? $groupLabel : null),
'preview' => $preview
);
}
}
return $nodes;
}

/**
* @param NodeInterface $node
* @param string $label
*
* @return string
*/
protected function getLabelPrefixByNodeContext(NodeInterface $node, string $label)
{
if ($node->isHidden())
$label = '[HIDDEN] ' . $label;
if ($node->isRemoved())
$label = '[REMOVED] ' . $label;
if ($node->isHiddenInIndex())
$label = '[NOT IN MENUS] ' . $label;

$q = new FlowQuery(array($node));
$nodeInLiveWorkspace = $q->context(['workspaceName' => 'live'])->get(0);
if (!$nodeInLiveWorkspace instanceof NodeInterface)
$label = '[NOT LIVE] ' . $label;

return $label;
}
}
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ composer require tms/select
groupBy: 'Your.Package:GroupType'
startingPoint: '/start/here/instead/of/rootnode'
labelPropertyName: 'title'
setLabelPrefixByNodeContext: true
previewPropertyName: 'thumbnailImage' # works with Neos 7.2+
```
### Optional parameters
| Parameter name | Description |
|-----------------------|---|
| `labelPropertyName` | Choose your specific **text property name** - if not set, the nodes label will be used. |
| `previewPropertyName` | Choose your specific **image property name** to display a custom preview icon as mentioned in the [Neos 7.2 release notes](https://www.neos.io/blog/neos-flow-72-released.html#neos-7-1-features). |
| Parameter name | Description |
|-------------------------------|---|
| `labelPropertyName` | Choose your specific **text property name** - if not set, the nodes label will be used. |
| `setLabelPrefixByNodeContext` | If set to `true`, labels get prefixed by `[HIDDEN] ...`, `[NOT IN MENUS] ...`, `[NOT LIVE] ...` and `[REMOVED] ...` depending on the node context. |
| `previewPropertyName` | Choose your specific **image property name** to display a custom preview icon as mentioned in the [Neos 7.2 release notes](https://www.neos.io/blog/neos-flow-72-released.html#neos-7-1-features). |

## Acknowledgments
Development sponsored by [tms.development - Online Marketing and Neos CMS Agency](https://www.tms-development.de/)

0 comments on commit b4dbba9

Please sign in to comment.