Skip to content

Commit

Permalink
Merge pull request #21 from dlubitz/Neos-4.3-and-5.x
Browse files Browse the repository at this point in the history
Fusion based 404 handling
  • Loading branch information
aertmann authored Jan 16, 2020
2 parents 1f933fe + 57f4b77 commit 1ab1dbe
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 217 deletions.
100 changes: 100 additions & 0 deletions Classes/Fusion/Eel/Helper/ContextHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace MOC\NotFound\Fusion\Eel\Helper;

use Neos\Flow\Annotations as Flow;
use Neos\Eel\ProtectedContextAwareInterface;

class ContextHelper implements ProtectedContextAwareInterface
{
/**
* @Flow\InjectConfiguration(path="contentDimensions", package="Neos.ContentRepository")
* @var array
*/
protected $contentDimensionsConfiguration;

/**
* Returns a context array with matched dimension values per dimension for given request uri path. If nothing
* matches, it returns a context array with default dimension values per dimension.
*
* @param $requestUriPath
* @return array
*/
public function ofRequestUriPath($requestUriPath)
{
// No dimensions configured, context is empty
if (count($this->contentDimensionsConfiguration) === 0) {
return [];
}

$uriSegments = $this->getUriSegments($requestUriPath);
$dimensionValues = $this->getDimensionValuesForUriSegments($uriSegments);
if (empty($dimensionValues)) {
$dimensionValues = $this->getDefaultDimensionValues();
}

$targetDimensionValues = array_map(function ($dimensionValues) {
return reset($dimensionValues); // Default target dimension value is first dimension value
}, $dimensionValues);


return [
'dimensions' => $dimensionValues,
'targetDimensions' => $targetDimensionValues
];
}

/**
* @param array $uriSegments
* @return array
*/
protected function getDimensionValuesForUriSegments($uriSegments)
{
if (count($uriSegments) !== count($this->contentDimensionsConfiguration)) {
return [];
}

$index = 0;
$dimensionValues = [];
foreach ($this->contentDimensionsConfiguration as $dimensionName => $dimensionConfiguration) {
$uriSegment = $uriSegments[$index++];
foreach ($dimensionConfiguration['presets'] as $preset) {
if ($uriSegment === $preset['uriSegment']) {
$dimensionValues[$dimensionName] = $preset['values'];
continue 2;
}
}
}

if (count($uriSegments) !== count($dimensionValues)) {
return [];
}

return $dimensionValues;
}

/**
* Returns default dimension values per dimension.
*
* @return array
*/
protected function getDefaultDimensionValues()
{
$dimensionValues = [];
foreach ($this->contentDimensionsConfiguration as $dimensionName => $dimensionConfiguration) {
$dimensionValues[$dimensionName] = [$dimensionConfiguration['default']];
}
return $dimensionValues;
}

protected function getUriSegments($requestUriPath)
{
$pathParts = explode('/', trim($requestUriPath, '/'), 2);
return explode('_', $pathParts[0]);
}

public function allowsCallOfMethod($methodName)
{
return true;
}
}
192 changes: 0 additions & 192 deletions Classes/ViewHelpers/RequestViewHelper.php

This file was deleted.

21 changes: 10 additions & 11 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
Neos:
Flow:
error:
exceptionHandler:
renderingGroups:
notFoundExceptions:
matchingStatusCodes: [ 404, 410 ]
options:
viewClassName: Neos\FluidAdaptor\View\TemplateView
templatePathAndFilename: 'resource://MOC.NotFound/Private/Templates/404.html'
variables:
path: '404.html' # skip suffix if unset
Fusion:
defaultContext:
NotFound.Context: MOC\NotFound\Fusion\Eel\Helper\ContextHelper
Neos:
fusion:
autoInclude:
MOC.NotFound: true
MOC:
NotFound:
uriPathSegment: 404
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@ Introduction

Neos CMS package that loads a normal editable page for displaying a 404 error.

Compatible with Neos 2.x-4.x
Compatible with Neos 4.3 - 5.x

Supports multiple content dimensions with URI segments and empty segments for default dimensions.

Installation
------------
```composer require "moc/notfound:^3.0"```
```composer require "moc/notfound:^4.0"```

Create a page with the URI segment "404" in the root of your site. If using content dimensions with URI segments,
ensure a page exists in all contexts or through fallbacks.

Alternatively set the following configuration in ``Settings.yaml``:

```yaml
Neos:
Flow:
error:
exceptionHandler:
renderingGroups:
notFoundExceptions:
options:
variables:
path: '404.html' # skip suffix if unset
MOC:
NotFound:
uriPathSegment: 404
```
Note: If you override the configuration in a package's configuration instead of globally, you need to ensure that package is loaded after this package. To do so, add ``"moc/notfound": "*"`` to the package's ``require`` section in it's ``composer.json``.
22 changes: 22 additions & 0 deletions Resources/Private/Fusion/Root.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
prototype(MOC.NotFound:NotFoundDocument) < prototype(Neos.Fusion:Value) {
uriPathSegment = ${Configuration.setting('MOC.NotFound.uriPathSegment')}
context = ${NotFound.Context.ofRequestUriPath(this.requestUriPath)}

value = ${q(site).context(this.context).children('[instanceof Neos.Neos:Document]').filter('[uriPathSegment="' + this.uriPathSegment + '"]').get(0)}
}

error {
@context.notfoundDocument = MOC.NotFound:NotFoundDocument {
requestUriPath = ${request.httpRequest.uri.path}
}

4xx {
@position = 'start'
condition = ${statusCode >= 400 && statusCode < 500 && notfoundDocument}
renderer = Neos.Fusion:Renderer {
@context.node = ${notfoundDocument}
@context.documentNode = ${notfoundDocument}
renderPath = '/root'
}
}
}
2 changes: 0 additions & 2 deletions Resources/Private/Templates/404.html

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"neos/neos": ">3.3"
"neos/neos": ">=4.3"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 1ab1dbe

Please sign in to comment.