Skip to content

Commit

Permalink
Merge pull request #4521 from neos/90/editPreviewModeSupport_v3_review
Browse files Browse the repository at this point in the history
90/edit preview mode support v3 review
  • Loading branch information
mficzel authored Sep 16, 2023
2 parents 6d00b3e + 5639b6b commit 3202c4e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
13 changes: 5 additions & 8 deletions Neos.Neos/Classes/Controller/Frontend/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Neos\Flow\Security\Context as SecurityContext;
use Neos\Flow\Session\SessionInterface;
use Neos\Flow\Utility\Now;
use Neos\Neos\Domain\Model\RenderingMode;
use Neos\Neos\Domain\Service\NodeSiteResolvingService;
use Neos\Neos\Domain\Service\RenderingModeService;
use Neos\Neos\FrontendRouting\Exception\InvalidShortcutException;
Expand Down Expand Up @@ -114,7 +115,6 @@ class NodeController extends ActionController

/**
* @param string $node Legacy name for backwards compatibility of route components
* @param string $renderingModeName Name of the user interface mode to use
* @throws NodeNotFoundException
* @throws \Neos\Flow\Mvc\Exception\StopActionException
* @throws \Neos\Flow\Mvc\Exception\UnsupportedRequestTypeException
Expand All @@ -125,13 +125,10 @@ class NodeController extends ActionController
* with unsafe requests from widgets or plugins that are rendered on the node
* - For those the CSRF token is validated on the sub-request, so it is safe to be skipped here
*/
public function previewAction(string $node, string $renderingModeName = null): void
public function previewAction(string $node): void
{
if (is_null($renderingModeName)) {
$renderingMode = $this->renderingModeService->findByCurrentUser();
} else {
$renderingMode = $this->renderingModeService->findByName($renderingModeName);
}
// @todo add $renderingModeName as parameter and append it for successive links again as get parameter to node uris
$renderingMode = $this->renderingModeService->findByCurrentUser();

$visibilityConstraints = VisibilityConstraints::frontend();
if ($this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.GeneralAccess')) {
Expand Down Expand Up @@ -247,7 +244,7 @@ public function showAction(string $node, bool $showInvisible = false): void
$this->handleShortcutNode($nodeAddress, $contentRepository);
}

$this->view->setOption('renderingModeName', 'frontend');
$this->view->setOption('renderingModeName', RenderingMode::FRONTEND);

$this->view->assignMultiple([
'value' => $nodeInstance,
Expand Down
17 changes: 13 additions & 4 deletions Neos.Neos/Classes/Domain/Model/RenderingMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

namespace Neos\Neos\Domain\Model;

use Neos\Utility\ObjectAccess;
use Neos\Neos\Domain\Exception;

/**
* Describes the mode in which the Neos interface is rendering currently,
* mainly distinguishing between edit and preview modes currently.
*/
class RenderingMode
{
public const FRONTEND = 'frontend';

/**
* @param array<string,mixed> $options
*/
Expand All @@ -36,13 +38,20 @@ public function __construct(
}

/**
* Creates an UserInterfaceMode object by configuration
* Creates a rendering mode from its configuration
*
* @param string $modeName
* @param array<string,mixed> $configuration
*/
public static function createFromConfiguration(string $modeName, array $configuration): RenderingMode
{
if ($modeName === RenderingMode::FRONTEND) {
throw new Exception(
'Cannot instantiate system rendering mode "frontend" from configuration.'
. ' Please use RenderingMode::createFrontend().',
1694802951840
);
}
$mode = new RenderingMode(
$modeName,
$configuration['isEditingMode'] ?? false,
Expand All @@ -55,12 +64,12 @@ public static function createFromConfiguration(string $modeName, array $configur
}

/**
* Creates the live User interface mode
* Creates the system integrated rendering mode 'frontend'
*/
public static function createFrontend(): RenderingMode
{
return new RenderingMode(
'frontend',
RenderingMode::FRONTEND,
false,
false,
'Frontend',
Expand Down
27 changes: 18 additions & 9 deletions Neos.Neos/Classes/Domain/Service/RenderingModeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class RenderingModeService
*/
protected $defaultEditPreviewMode;

/**
* @var array<string, RenderingMode>
*/
private array $instances = [];

/**
* Get the current rendering mode.
* Will return a live mode when not in backend.
Expand Down Expand Up @@ -85,16 +90,20 @@ public function findDefault(): RenderingMode
*/
public function findByName(string $modeName): RenderingMode
{
if ($modeName === 'frontend') {
return RenderingMode::createFrontend();
}
if (isset($this->editPreviewModes[$modeName])) {
return RenderingMode::createFromConfiguration($modeName, $this->editPreviewModes[$modeName]);
if ($instance = $this->instances[$modeName] ?? null) {
return $instance;
}
throw new Exception(
'The requested interface render mode "' . $modeName . '" is not configured.'
if ($modeName === RenderingMode::FRONTEND) {
$this->instances[$modeName] = RenderingMode::createFrontend();
} elseif (isset($this->editPreviewModes[$modeName])) {
$this->instances[$modeName] = RenderingMode::createFromConfiguration($modeName, $this->editPreviewModes[$modeName]);
} else {
throw new Exception(
'The requested rendering mode "' . $modeName . '" is not configured.'
. ' Please make sure it exists as key in the Settings path "Neos.Neos.Interface.editPreviewModes".',
1427715962
);
1427715962
);
}
return $this->instances[$modeName];
}
}
3 changes: 2 additions & 1 deletion Neos.Neos/Classes/View/FusionExceptionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Neos\Fusion\Core\Runtime as FusionRuntime;
use Neos\Fusion\Core\RuntimeFactory;
use Neos\Fusion\Exception\RuntimeException;
use Neos\Neos\Domain\Model\RenderingMode;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\FusionService;
use Neos\Neos\Domain\Service\SiteNodeUtility;
Expand Down Expand Up @@ -204,7 +205,7 @@ protected function getFusionRuntime(

$fusionGlobals = FusionGlobals::fromArray([
'request' => $this->controllerContext->getRequest(),
'renderingModeName' => 'frontend'
'renderingModeName' => RenderingMode::FRONTEND
]);
$this->fusionRuntime = $this->runtimeFactory->createFromConfiguration(
$fusionConfiguration,
Expand Down
3 changes: 2 additions & 1 deletion Neos.Neos/Classes/View/FusionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Neos\Fusion\Core\Runtime;
use Neos\Fusion\Core\RuntimeFactory;
use Neos\Fusion\Exception\RuntimeException;
use Neos\Neos\Domain\Model\RenderingMode;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\FusionService;
use Neos\Neos\Domain\Service\SiteNodeUtility;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function render(): string|ResponseInterface
'boolean'
],
'renderingModeName' => [
'frontend',
RenderingMode::FRONTEND,
'Name of the user interface mode to use',
'string'
]
Expand Down
3 changes: 2 additions & 1 deletion Neos.Neos/Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ Neos:

defaultEditPreviewMode: inPlace
editPreviewModes:
# the "frontend" renderingMode is hardcoded internally and cannot be modified
# the system integrated rendering mode "frontend" cannot be configured
# frontend: {}
inPlace:
isEditingMode: true
isPreviewMode: false
Expand Down

0 comments on commit 3202c4e

Please sign in to comment.