Skip to content

Instantly share code, notes, and snippets.

@jobee
Last active March 27, 2018 06:56
Show Gist options
  • Save jobee/ed61f3c495a217b7650d787fcad3ffc0 to your computer and use it in GitHub Desktop.
Save jobee/ed61f3c495a217b7650d787fcad3ffc0 to your computer and use it in GitHub Desktop.
Neos Dimension Resolver by Host
<?php
namespace TMS\HostDimensionResolver\Http\ContentDimensionDetection;
use Flowpack\Neos\DimensionResolver\Http\ContentDimensionDetection\ContentDimensionPresetDetectorInterface;
use Neos\Flow\Http;
/**
* Host based dimension preset detector
*/
final class HostDimensionPresetDetector implements ContentDimensionPresetDetectorInterface
{
/**
* @var array
*/
protected $defaultOptions = [];
/**
* @param string $dimensionName
* @param array $presets
* @param Http\Component\ComponentContext $componentContext
* @param array|null $overrideOptions
* @return array|null
*/
public function detectPreset(string $dimensionName, array $presets, Http\Component\ComponentContext $componentContext, array $overrideOptions = null)
{
$host = $componentContext->getHttpRequest()->getUri()->getHost();
foreach ($presets as $preset) {
if ($host === $preset['resolutionValue']) {
return $preset;
}
}
return null;
}
}
<?php
namespace TMS\HostDimensionResolver\Http\ContentDimensionLinking;
use Flowpack\Neos\DimensionResolver\Http\ContentDimensionLinking\ContentDimensionPresetLinkProcessorInterface;
use Neos\Flow\Mvc\Routing;
/**
* Host based dimension preset detector
*/
final class HostDimensionPresetLinkProcessor implements ContentDimensionPresetLinkProcessorInterface
{
/**
* @param Routing\Dto\UriConstraints $uriConstraints
* @param string $dimensionName
* @param array $presetConfiguration
* @param array $preset
* @param array|null $overrideOptions
* @return Routing\Dto\UriConstraints
*/
public function processUriConstraints(
Routing\Dto\UriConstraints $uriConstraints,
string $dimensionName,
array $presetConfiguration,
array $preset,
array $overrideOptions = null
): Routing\Dto\UriConstraints {
$host = $preset['resolutionValue'];
// TODO: without an additional `withPathPrefix` routes are resolved with `pathPrefix = //` and thus internal links stop working
$uriSegment = $preset['uriSegment'];
return $uriConstraints->withHost($host)->withPathPrefix($uriSegment);
}
}
Neos:
ContentRepository:
contentDimensions:
language:
label: 'Language (Region)'
icon: 'icon-language'
default: 'en'
defaultPreset: 'en'
# Add host dimension resolver
detectionComponent:
implementationClassName: 'TMS\HostDimensionResolver\Http\ContentDimensionDetection\HostDimensionPresetDetector'
linkProcessorComponent:
implementationClassName: 'TMS\HostDimensionResolver\Http\ContentDimensionLinking\HostDimensionPresetLinkProcessor'
presets:
'en':
label: 'English'
values: ['en']
uriSegment: 'en'
resolutionValue: 'example.com'
'en_US':
label: 'English (United States)'
values: ['en_US', 'en']
uriSegment: 'en-us'
resolutionValue: 'example.us'
'de':
label: 'Deutsch'
values: ['de']
uriSegment: 'de'
resolutionValue: 'example.de'
'es':
label: 'Español'
values: ['es']
uriSegment: 'es'
resolutionValue: 'example.es'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment