Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Add 1.0 version of the NodeTypeFinder. #1

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Classes/Command/NodeTypeFinderCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace Netlogix\NodeTypeFinder\Command;

use GuzzleHttp\Psr7\ServerRequest;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\ActionResponse;
use Neos\Flow\Mvc\Controller\Arguments;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\Mvc\Routing\UriBuilder;
use Netlogix\NodeTypeFinder\Service\NodeTypeFinderService;

/**
* @Flow\Scope("singleton")
*/
class NodeTypeFinderCommandController extends CommandController
{
/**
* @Flow\Inject
* @var NodeTypeFinderService
*/
protected $nodeTypeFinderService;

/**
* List the URLs of all pages where a node of the specified type occurs.
*
* @param string $nodeTypeName
* @throws \Neos\Eel\Exception
* @throws \Neos\Flow\Http\Exception
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
* @throws \Neos\Flow\Property\Exception
* @throws \Neos\Flow\Security\Exception
* @throws \Neos\Neos\Exception
*/
public function listNodeTypeOccurrencesCommand(string $nodeTypeName): void
{
$this->output->outputTable($this->nodeTypeFinderService->findNodeTypeOccurrences(
$nodeTypeName,
self::buildControllerContext()
), ['Occurrence on page:']);
}

private static function buildControllerContext(): ControllerContext
{
$_SERVER['FLOW_REWRITEURLS'] = '1';
$httpRequest = ServerRequest::fromGlobals();
$request = ActionRequest::fromHttpRequest($httpRequest);
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($request);

return new ControllerContext(
$request,
new ActionResponse(),
new Arguments([]),
$uriBuilder
);
}
}
62 changes: 62 additions & 0 deletions Classes/Controller/NodeTypeFinderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace Netlogix\NodeTypeFinder\Controller;

use Neos\Flow\Annotations as Flow;
use Neos\Fusion\View\FusionView;
use Neos\Flow\Mvc\View\ViewInterface;
use Neos\Neos\Controller\Module\AbstractModuleController;
use Netlogix\NodeTypeFinder\Service\NodeTypeFinderService;

class NodeTypeFinderController extends AbstractModuleController
{
/**
* @var FusionView
*/
protected $defaultViewObjectName = FusionView::class;

/**
* @Flow\Inject
* @var NodeTypeFinderService
*/
protected $nodeTypeFinderService;

protected function initializeView(ViewInterface $view)
{
parent::initializeView($view);
$view->setFusionPathPattern('resource://Netlogix.NodeTypeFinder/Private/Fusion/Backend');
}

/**
* @param string $searchTerm
*
* @throws \Neos\Eel\Exception
* @throws \Neos\Flow\Http\Exception
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
* @throws \Neos\Flow\Property\Exception
* @throws \Neos\Flow\Security\Exception
* @throws \Neos\Neos\Exception
*/
public function indexAction(?string $searchTerm = null): void
{
$this->view->assign('searchTerm', $searchTerm);

if (!empty($searchTerm)) {
$this->view->assign('occurrences', iterator_to_array($this->search($searchTerm)));
}
}

private function search(string $searchTerm): \Generator
{
$occurrences = $this->nodeTypeFinderService->findNodeTypeOccurrences(
$searchTerm,
$this->controllerContext
);

foreach ($occurrences as $occurrence) {
yield ['url' => $occurrence['url'], 'label' => $occurrence['label']];
}
}
}
84 changes: 84 additions & 0 deletions Classes/Service/NodeTypeFinderService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);

namespace Netlogix\NodeTypeFinder\Service;

use Neos\Flow\Annotations as Flow;
use GuzzleHttp\Psr7\Uri;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Neos\Service\LinkingService;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;

/**
* @Flow\Scope("singleton")
*/
class NodeTypeFinderService
{
/**
* @Flow\Inject
* @var LinkingService
*/
protected $linkingService;

/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;

/**
* @param string $nodeTypeName
* @param Uri $baseUri
* @return array
* @throws \Neos\Eel\Exception
* @throws \Neos\Flow\Http\Exception
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
* @throws \Neos\Flow\Property\Exception
* @throws \Neos\Flow\Security\Exception
* @throws \Neos\Neos\Exception
*/
public function findNodeTypeOccurrences(string $nodeTypeName, ControllerContext $controllerContext): array
{
$occurrences = [];

$context = $this->contextFactory->create(['workspaceName' => 'live']);

$nodes = (new FlowQuery([$context->getCurrentSiteNode()]))
->find('/')
->find('[instanceof '.$nodeTypeName.']')
->get();

foreach ($nodes as $node) {
if (!$node instanceof NodeInterface) {
continue;
}

$documentQuery = new FlowQuery([$node]);
$documentNode = $documentQuery->closest('[instanceof Neos.Neos:Document]')->get(0);

if (!$documentNode instanceof NodeInterface) {
continue;
}

$uri = $this->linkingService->createNodeUri(
$controllerContext,
$documentNode,
null,
null,
true
);

if (!array_key_exists($uri, $occurrences)) {
$occurrences[$uri] = [
'url' => str_replace('./', '', $uri),
'label' => $documentNode->getLabel()
];
}
}

return array_values($occurrences);
}
}
12 changes: 12 additions & 0 deletions Configuration/Policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
privilegeTargets:
'Neos\Neos\Security\Authorization\Privilege\ModulePrivilege':
'Netlogix.NodeTypeFinder:BackendModule':
matcher: 'administration/nodeTypeFinderModule'

roles:
'Neos.Neos:Administrator':
privileges:
-
privilegeTarget: 'Netlogix.NodeTypeFinder:BackendModule'
permission: GRANT

10 changes: 10 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Neos:
Neos:
modules:
administration:
submodules:
nodeTypeFinderModule:
label: 'NodeType Finder'
controller: 'Netlogix\NodeTypeFinder\Controller\NodeTypeFinderController'
description: 'Auflistung der Seiten URLs auf denen der gegebene NodeType vorkommt.'
icon: 'icon-search'
44 changes: 44 additions & 0 deletions Resources/Private/Fusion/Backend/NodeTypeFinder.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
include: resource://Neos.Fusion/Private/Fusion/Root.fusion
include: resource://Neos.Fusion.Form/Private/Fusion/Root.fusion

Netlogix.NodeTypeFinder.NodeTypeFinderController.index = Netlogix.NodeTypeFinder:BackendModule

prototype(Netlogix.NodeTypeFinder:BackendModule) < prototype(Neos.Fusion:Component) {

renderer = afx`
<Neos.Fusion.Form:Form form.data.searchTerm={searchTerm} form.target.action="index">
<div class="neos-row">
<Neos.Fusion.Form:Neos.BackendModule.FieldContainer attributes.class="neos-span4" field.name="searchTerm" >
<Neos.Fusion.Form:Input attributes.class="neos-span4" attributes.placeholder="Vendor.Package.NodeTypes:NodeType" />
</Neos.Fusion.Form:Neos.BackendModule.FieldContainer>
<Neos.Fusion.Form:Button attributes.class="neos-button neos-button-primary">
Search
</Neos.Fusion.Form:Button>
</div>
</Neos.Fusion.Form:Form>
<fieldset>
<legend>Occurrences of NodeType</legend>
</fieldset>
<table class="neos-table" @if.hasOccurences={occurrences}>
<Neos.Fusion:Loop items={occurrences} itemName="occurrence" @children="itemRenderer">
<tr>
<td>
<a href={occurrence.url}>
{occurrence.label}
</a>
</td>
</tr>
</Neos.Fusion:Loop>
</table>
<div class="neos-row" @if.noOccurences={!occurrences && searchTerm}>
<div class="neos-span12">
No occurrences of NodeType {searchTerm}.
</div>
</div>
<div class="neos-row" @if.noSearchTerm={!searchTerm}>
<div class="neos-span12">
Search for a NodeType to see a list of its occurrences.
</div>
</div>
`
}
1 change: 1 addition & 0 deletions Resources/Private/Fusion/Backend/Root.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: NodeTypeFinder.fusion
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "netlogix/nodetypefinder",
"description": "Neos Backend Module to search for occurrences of NodeTypes in the page tree.",
"type": "neos-plugin",
"license": "MIT",
"require": {
"php": "^7.4 || ^8.0",
"neos/neos": "^5.3 || ^7.0",
"guzzlehttp/psr7": "^1.8 || ^2.0"
},
"autoload": {
"psr-4": {
"Netlogix\\NodeTypeFinder\\": "Classes/"
}
},
"extra": {
"neos": {
"package-key": "Netlogix.NodeTypeFinder"
}
}
}