Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a StructureResolver Extension for Localization #101

Open
wants to merge 2 commits into
base: 0.10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Content/StructureResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sulu\Bundle\HeadlessBundle\Content;

use Sulu\Bundle\HeadlessBundle\Content\StructureResolver\StructureResolverExtensionInterface;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\PageBundle\Document\BasePageDocument;
use Sulu\Bundle\PageBundle\Preview\PageRouteDefaultsProvider;
Expand Down Expand Up @@ -50,16 +51,23 @@ class StructureResolver implements StructureResolverInterface
*/
private $referenceStorePool;

/**
* @var StructureResolverExtensionInterface[]
*/
private iterable $structureResolverExtensions;

public function __construct(
ContentResolverInterface $contentResolver,
StructureManagerInterface $structureManager,
DocumentInspector $documentInspector,
ReferenceStorePoolInterface $referenceStorePool
ReferenceStorePoolInterface $referenceStorePool,
iterable $structureResolverExtensions = []
) {
$this->contentResolver = $contentResolver;
$this->structureManager = $structureManager;
$this->documentInspector = $documentInspector;
$this->referenceStorePool = $referenceStorePool;
$this->structureResolverExtensions = $structureResolverExtensions;
}

/**
Expand Down Expand Up @@ -95,6 +103,10 @@ public function resolve(
$data['view'][$property->getName()] = $contentView->getView();
}

foreach ($this->structureResolverExtensions as $structureResolverExtension) {
$data[$structureResolverExtension->getKey()] = $structureResolverExtension->resolve($requestedStructure, $locale);
}

return $data;
}

Expand Down
80 changes: 80 additions & 0 deletions Content/StructureResolver/LocalizationResolverExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Content\StructureResolver;

use Sulu\Component\Content\Compat\Structure\PageBridge;
use Sulu\Component\Content\Compat\StructureInterface;
use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;

class LocalizationResolverExtension implements StructureResolverExtensionInterface
{
private RequestAnalyzerInterface $requestAnalyzer;
private WebspaceManagerInterface $webspaceManager;

public function __construct(
RequestAnalyzerInterface $requestAnalyzer,
WebspaceManagerInterface $webspaceManager
)
{
$this->requestAnalyzer = $requestAnalyzer;
$this->webspaceManager = $webspaceManager;
}

public function getKey(): string
{
return 'localizations';
}

public function resolve(StructureInterface $requestedStructure, string $locale): mixed
{
$webspace = $this->requestAnalyzer->getWebspace();

if (null !== ($portal = $this->requestAnalyzer->getPortal())) {
$allLocalizations = $portal->getLocalizations();
} else {
$allLocalizations = $webspace->getLocalizations();
}

$pageUrls = [];
if ($requestedStructure instanceof PageBridge) {
$pageUrls = $requestedStructure->getUrls();
}

$localizations = [];

foreach ($allLocalizations as $localization) {
$locale = $localization->getLocale();

$alternate = true;
if (\array_key_exists($locale, $pageUrls)) {
$url = $this->webspaceManager->findUrlByResourceLocator($pageUrls[$locale], null, $locale);
if ($url === null) {
$url = $pageUrls[$locale];
}
} else {
$alternate = false;
$url = $this->webspaceManager->findUrlByResourceLocator('/', null, $locale);
}

$localizations[$locale] = [
'locale' => $locale,
'url' => $url,
'country' => $localization->getCountry(),
'alternate' => $alternate,
];
}
return $localizations;
}
}
26 changes: 26 additions & 0 deletions Content/StructureResolver/StructureResolverExtensionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Content\StructureResolver;

use Sulu\Component\Content\Compat\StructureInterface;

interface StructureResolverExtensionInterface
{
public function getKey(): string;

public function resolve(
StructureInterface $requestedStructure,
string $locale
): mixed;
}
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ export default HeadlessTemplatePage;
Finally, you can build your frontend application by executing `npm install` and `npm run build` in the `assets/headless`
directory.

### Extending the StructureResolver
To add more data to the json that is returned for a site, you can extend the StructureResolver by registering an service with the tag `sulu_headless.structure_resolver_extension`.

```
use Sulu\Bundle\HeadlessBundle\Content\StructureResolver\StructureResolverExtensionInterface;

class MyStructureExtension implements StructureResolverExtensionInterface
{
public function getKey(): string
{
return 'myKey';
}

public function resolve(
StructureInterface $requestedStructure,
string $locale
): mixed {
return [
'foo' => 'bar',
'fruits' => Fruits::getArray($locale),
'webspaceKey' => $requestedStructure->getWebspaceKey()
];
}
}
```

## ❤️&nbsp; Support and Contributions

Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
<argument type="service" id="sulu.content.structure_manager"/>
<argument type="service" id="sulu_document_manager.document_inspector"/>
<argument type="service" id="sulu_website.reference_store_pool"/>
<argument type="tagged_iterator" tag="sulu_headless.structure_resolver_extension"/>
</service>
<service id="Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface" alias="sulu_headless.structure_resolver"/>
<service id="Sulu\Bundle\HeadlessBundle\Content\StructureResolver\LocalizationResolverExtension" autowire="true">
<tag name="sulu_headless.structure_resolver_extension" />
</service>

<service
id="sulu_headless.content_resolver"
Expand Down