Skip to content

Commit

Permalink
FEATURE: Add editPreviewMode support for Neos 9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mficzel committed Mar 7, 2023
1 parent 6eb144b commit febb202
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
14 changes: 13 additions & 1 deletion Neos.Neos/Classes/Controller/Frontend/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\Projection\ContentGraph\NodePath;
use Neos\ContentRepository\Core\NodeType\NodeTypeConstraintParser;
use Neos\Neos\Domain\Model\EditPreviewMode;
use Neos\Neos\Domain\Repository\EditPreviewModeRepository;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
Expand Down Expand Up @@ -110,8 +112,15 @@ class NodeController extends ActionController
*/
protected $nodeSiteResolvingService;

/**
* @Flow\Inject
* @var EditPreviewModeRepository
*/
protected $editPreviewModeRepository;

/**
* @param string $node Legacy name for backwards compatibility of route components
* @param string|null $editPreviewMode Rendering mode like "rawContent" defaults to defaultEditPreviewMode from settings
* @throws NodeNotFoundException
* @throws \Neos\Flow\Mvc\Exception\StopActionException
* @throws \Neos\Flow\Mvc\Exception\UnsupportedRequestTypeException
Expand All @@ -122,7 +131,7 @@ 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): void
public function previewAction(string $node, string $editPreviewMode = null): void
{
$visibilityConstraints = VisibilityConstraints::frontend();
if ($this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.GeneralAccess')) {
Expand Down Expand Up @@ -166,6 +175,9 @@ public function previewAction(string $node): void
$this->view->assignMultiple([
'value' => $nodeInstance,
'site' => $site,
'editPreviewMode' => $editPreviewMode
? $this->editPreviewModeRepository->findByName($editPreviewMode)
: $this->editPreviewModeRepository->findDefault()
]);

if (!$nodeAddress->isInLiveWorkspace()) {
Expand Down
43 changes: 43 additions & 0 deletions Neos.Neos/Classes/Domain/Model/EditPreviewMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

declare(strict_types=1);

namespace Neos\Neos\Domain\Model;

final class EditPreviewMode
{
protected function __construct(
public readonly string $name,
public readonly string $title,
public readonly string $fusionPath,
public readonly bool $isEditMode,
public readonly bool $isPreviewMode
) {
}

/**
* @param string $name
* @param array{'title'?:string, 'fusionRenderingPath'?:string, 'isEditingMode'?:bool, 'isPreviewMode'?:bool} $configuration
* @return self
*/
public static function fromNameAndConfiguration(string $name, array $configuration): self
{
return new static(
$name,
$configuration['title'] ?? $name,
$configuration['fusionRenderingPath'] ?? '',
$configuration['isEditingMode'] ?? false,
$configuration['isPreviewMode'] ?? false
);
}
}
40 changes: 40 additions & 0 deletions Neos.Neos/Classes/Domain/Repository/EditPreviewModeRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

declare(strict_types=1);

namespace Neos\Neos\Domain\Repository;

use Neos\Neos\Domain\Model\EditPreviewMode;
use Neos\Flow\Annotations as Flow;

class EditPreviewModeRepository
{
#[Flow\InjectConfiguration(path:"userInterface.defaultEditPreviewMode")]
protected string $defaultEditPreviewMode;

/**
* @var array<string, array{'title'?:string, 'fusionRenderingPath'?:string, 'isEditingMode'?:bool, 'isPreviewMode'?:bool}>
*/
#[Flow\InjectConfiguration(path:"userInterface.editPreviewModes")]
protected array $editPreviewModeConfigurations;

public function findDefault(): EditPreviewMode
{
return EditPreviewMode::fromNameAndConfiguration($this->defaultEditPreviewMode, $this->editPreviewModeConfigurations[$this->defaultEditPreviewMode]);
}

public function findByName(string $name): EditPreviewMode
{
return EditPreviewMode::fromNameAndConfiguration($name, $this->editPreviewModeConfigurations[$name]);
}
}
12 changes: 10 additions & 2 deletions Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ public function uriFor(NodeAddress $nodeAddress): UriInterface
*/
public function previewUriFor(NodeAddress $nodeAddress): UriInterface
{
return new Uri($this->uriBuilder->uriFor(
$uri = new Uri($this->uriBuilder->uriFor(
'preview',
['node' => $nodeAddress->serializeForUri()],
[],
'Frontend\Node',
'Neos.Neos'
));

$queryParameters = ['node' => $nodeAddress->serializeForUri()];

if ($this->uriBuilder->getRequest()->hasArgument('editPreviewMode')) {
$queryParameters['editPreviewMode'] = $this->uriBuilder->getRequest()->getArgument('editPreviewMode');
}

return $uri->withQuery(http_build_query($queryParameters));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
prototype(Neos.Fusion:GlobalCacheIdentifiers) {
workspaceChain = ${Array.join(Array.keys(Neos.Caching.getWorkspaceChain(documentNode)), ',')}
[email protected] = ${!!documentNode}
editPreviewMode = ${editPreviewMode}
editPreviewMode = ${editPreviewMode.name}
}
5 changes: 2 additions & 3 deletions Neos.Neos/Resources/Private/Fusion/RootCase.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ root {

editPreviewMode {
@position = 'end 9996'
possibleEditPreviewModePath = ${documentNode.context.currentRenderingMode.fusionPath}
condition = ${Neos.Node.inBackend(documentNode) && this.possibleEditPreviewModePath != null && this.possibleEditPreviewModePath != ''}
renderPath = ${'/' + this.possibleEditPreviewModePath}
condition = ${Neos.Node.inBackend(documentNode) && editPreviewMode.fusionPath}
renderPath = ${'/' + editPreviewMode.fusionPath}
}

format {
Expand Down

0 comments on commit febb202

Please sign in to comment.