Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.