Skip to content

Instantly share code, notes, and snippets.

@wachterjohannes
Last active October 20, 2017 10:05
Show Gist options
  • Save wachterjohannes/2bdef03d009d695144858417d3032095 to your computer and use it in GitHub Desktop.
Save wachterjohannes/2bdef03d009d695144858417d3032095 to your computer and use it in GitHub Desktop.
Document to twig structure
class DefaultController extends WebsiteController
{
public function indexAction(StructureInterface $structure, $preview = false, $partial = false)
{
$document = $this->loadDocument('/test', $structure->getLanguageCode(), $structure->getWebspaceKey());
return $this->renderStructure(
$structure,
[
'page' => $this->resolve($this->documentToStructure($document)),
],
$preview,
$partial
);
}
protected function loadDocument($url, $locale, $webspaceKey)
{
$resourceLocatorStrategyPool = $this->get('sulu.content.resource_locator.strategy_pool');
$resourceLocatorStrategy = $resourceLocatorStrategyPool->getStrategyByWebspaceKey($webspaceKey);
$document = $this->get('sulu_document_manager.document_manager')->find(
$resourceLocatorStrategy->loadByResourceLocator(
rtrim($resourceLocator, '/'),
$webspaceKey,
$locale
),
$locale,
[
'load_ghost_content' => false,
]
);
}
protected function documentToStructure(BasePageDocument $document)
{
$inspector = $this->get('sulu_document_manager.document_inspector');
$structure = $inspector->getStructureMetadata($document);
$documentAlias = $inspector->getMetadata($document)->getAlias();
$structureManager = $this->get('sulu.content.structure_manager');
$structureBridge = $structureManager->wrapStructure($documentAlias, $structure);
$structureBridge->setDocument($document);
return $structureBridge;
}
protected function resolve(StructureInterface $structure)
{
return $this->get('sulu_website.resolver.structure')->resolve($structure);
}
}
@ducho
Copy link

ducho commented Oct 20, 2017

Thanks… I hope that last question… How I can pass StructureInterface $structure to API node?

     * @Rest\Get("/pages")
     *
     * @ApiDoc(
     *   section="Pages",
     *   resource = true,
     *   statusCodes = {
     *     204 = "Returned when successful",
     *     404 = "Returned when not found"
     *   }
     * )
     *
     * @param StructureInterface $structue
     * @param ParamFetcher $paramFetcher
     * @param Request $request
     *
     * @Annotations\QueryParam(name="route", description="Page route for content load.")
     *
     * @throws NotFoundHttpException when does not exist
     *
     * @return Response|array
     */
    public function getPageAction( StructureInterface $structue, ParamFetcher $paramFetcher, Request $request)
    {

        $resourceLocator = $paramFetcher->get('route');

        $resourceLocatorStrategyPool = $this->get('sulu.content.resource_locator.strategy_pool');
        $resourceLocatorStrategy = $resourceLocatorStrategyPool->getStrategyByWebspaceKey(self::WEBSPACE_KEY);

        $document = $this->get('sulu_document_manager.document_manager')->find(
            $resourceLocatorStrategy->loadByResourceLocator(
                rtrim($resourceLocator, '/'),
                self::WEBSPACE_KEY,
                $request->getLocale()
            ),
            $request->getLocale(),
            [
                'load_ghost_content' => false,
            ]
        );

        $response = $this->renderStructure(
            $structure,
            [
                'page' => $this->resolve($this->documentToStructure($document)),
            ],
            false,
            false
        );

       return $response;

    }```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment