Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Last active September 2, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwaidelich/6fe84d3b60ae28c4e1c128c5b8fbdf6b to your computer and use it in GitHub Desktop.
Save bwaidelich/6fe84d3b60ae28c4e1c128c5b8fbdf6b to your computer and use it in GitHub Desktop.
Neos CMS: Get (relative) Node Url upon publishing
<?php
namespace Some\Package;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\HttpRequestHandlerInterface;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\Routing\UriBuilder;
/**
* @Flow\Scope("singleton")
*/
final class CacheFlusher
{
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;
/**
* @var NodeInterface[]
*/
private $pendingNodes = [];
public function afterNodePublishing(NodeInterface $node, Workspace $targetWorkspace): void
{
if (!$targetWorkspace->isPublicWorkspace()) {
return;
}
/** @noinspection PhpUnhandledExceptionInspection */
$q = new FlowQuery([$node]);
$pageNode = $q->closest('[instanceof Neos.Neos:Document]')->context(['workspaceName' => 'live'])->get(0);
$this->pendingNodes[$pageNode->getIdentifier()] = $pageNode;
}
public function flush(): void
{
$requestHandler = $this->bootstrap->getActiveRequestHandler();
if (!$requestHandler instanceof HttpRequestHandlerInterface) {
return;
}
$actionRequest = new ActionRequest($requestHandler->getHttpRequest());
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($actionRequest);
foreach ($this->pendingNodes as $node) {
$relativeNodeUrl = $uriBuilder->uriFor('show', ['node' => $node], 'Frontend\Node', 'Neos.Neos');
// TODO ..something with $relativeNodeUrl
}
}
}
<?php
namespace Some\Package;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Package\Package as BasePackage;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
use Neos\Neos\Service\PublishingService;
class Package extends BasePackage
{
public function boot(Bootstrap $bootstrap): void
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(PublishingService::class, 'nodePublished', CacheFlusher::class, 'afterNodePublishing', false);
$dispatcher->connect(PersistenceManager::class, 'allObjectsPersisted', CacheFlusher::class, 'flush', false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment