Last active
September 17, 2021 09:44
-
-
Save jobee/005801fa62e37d3a0104d50a3b3b73c7 to your computer and use it in GitHub Desktop.
Neos CMS - Intercept UI reloads while editing document node properties (which are set to reloadIfChanged:true) outside of the current document scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Your\Package\Aspect; | |
use Neos\ContentRepository\Domain\Model\NodeInterface; | |
use Neos\Eel\FlowQuery\FlowQuery; | |
use Neos\Flow\Annotations as Flow; | |
use Neos\Flow\AOP\JoinPointInterface; | |
use Neos\Flow\Property\PropertyMapper; | |
use Neos\Flow\Session\SessionInterface; | |
/** | |
* @Flow\Aspect | |
*/ | |
class InterceptReloadDocumentAspect { | |
/** | |
* @Flow\Inject | |
* @var SessionInterface | |
*/ | |
protected $session; | |
/** | |
* @Flow\Inject | |
* @var PropertyMapper | |
*/ | |
protected $propertyMapper; | |
/** | |
* Intercept UI reloads while editing document node properties (which are set to reloadIfChanged:true) outside of the current document scope | |
* | |
* @Flow\Around("method(Neos\Neos\Ui\Domain\Model\Changes\Property->reloadDocument())") | |
*/ | |
public function interceptReloadDocument(JoinPointInterface $joinPoint): void | |
{ | |
/** @var NodeInterface $node */ | |
$node = $joinPoint->getMethodArgument('node'); | |
// Get document node that is currently visible / active in the nodetree | |
$skipReload = false; | |
$activeNode = null; | |
$workspaceName = $node->getWorkspace()->getName(); | |
if ($this->session->isStarted() && $this->session->hasKey('lastVisitedNode')) { | |
try { | |
$lastVisitedNode = $this->propertyMapper->convert($this->session->getData('lastVisitedNode'), NodeInterface::class); | |
$q = new FlowQuery([$lastVisitedNode]); | |
$activeNode = $q->context(['workspaceName' => $workspaceName])->get(0); | |
} catch (\Exception $exception) { | |
// TODO: log exception | |
} | |
} | |
if ($activeNode instanceof NodeInterface && $node != $activeNode) { | |
if ($activeNode->getNodeType()->isOfType('Your.Package:Mixin.OutOfScopeEditing.ReloadThisDocument')) { | |
$node = $activeNode; | |
} | |
if ($activeNode->getNodeType()->isOfType('Your.Packaget:Mixin.OutOfScopeEditing.SkipReload')) { | |
$skipReload = true; | |
} | |
} | |
if ($skipReload != true) { | |
$joinPoint->setMethodArgument('node', $node); | |
$joinPoint->getAdviceChain()->proceed($joinPoint); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Your.Package:Mixin.OutOfScopeEditing.ReloadThisDocument': | |
abstract: true | |
'Your.Package:Mixin.OutOfScopeEditing.SkipReload': | |
abstract: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment