Skip to content

Commit

Permalink
FEATURE: add optional preview icon - works with Neos 7.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
jobee committed Oct 4, 2021
1 parent 98396a1 commit 7db3db4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
34 changes: 29 additions & 5 deletions Classes/DataSource/NodeDataDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace Tms\Select\DataSource;

use Neos\Eel\FlowQuery\FlowQuery;
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;
Expand All @@ -21,6 +24,12 @@ class NodeDataDataSource extends AbstractDataSource {
*/
protected $contentContextFactory;

/**
* @Flow\Inject
* @var AssetService
*/
protected $assetService;

/**
* Get data
*
Expand Down Expand Up @@ -51,13 +60,16 @@ public function getData(NodeInterface $node = NULL, array $arguments = [])
$labelPropertyName = null;
if (isset($arguments['labelPropertyName']))
$labelPropertyName = $arguments['labelPropertyName'];
$previewPropertyName = null;
if (isset($arguments['previewPropertyName']))
$previewPropertyName = $arguments['previewPropertyName'];

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

return $nodes;
Expand All @@ -67,11 +79,12 @@ public function getData(NodeInterface $node = NULL, array $arguments = [])
* @param NodeInterface $parentNode
* @param array $nodeTypes
* @param string|null $labelPropertyName
* @param string|null $previewPropertyName
* @param string|null $groupBy
*
* @return array
*/
protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropertyName = null, $groupBy = null)
protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropertyName = null, $previewPropertyName = null, $groupBy = null)
{
$q = new FlowQuery(array($parentNode));
$nodes = array();
Expand All @@ -83,14 +96,25 @@ protected function getNodes(NodeInterface $parentNode, $nodeTypes, $labelPropert

foreach ($q->find($filterString)->get() as $node) {
if ($node instanceof NodeInterface) {
$preview = null;
if ($previewPropertyName) {
$image = $node->getProperty($previewPropertyName);
if (!$image instanceof ImageInterface)
continue;
$thumbnailConfiguration = new ThumbnailConfiguration(null, 74, null, 56);
$thumbnail = $this->assetService->getThumbnailUriAndSizeForAsset($image, $thumbnailConfiguration);
if (!isset($thumbnail['src']))
continue;
$preview = $thumbnail['src'];
}
$nodes[] = array(
'value' => $node->getIdentifier(),
'label' => $labelPropertyName ? $node->getProperty($labelPropertyName) : $node->getLabel(),
'group' => ($groupBy !== null ? $parentNode->getLabel() : null)
'group' => ($groupBy !== null ? $parentNode->getLabel() : null),
'preview' => $preview
);
}
}

return $nodes;
}
}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ composer require tms/select
groupBy: 'Your.Package:GroupType'
startingPoint: '/start/here/instead/of/rootnode'
labelPropertyName: 'title'
previewPropertyName: 'thumbnailImage' # works with Neos 7.2+
```
## Acknowledgments
### 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). |

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

0 comments on commit 7db3db4

Please sign in to comment.