Last active
August 3, 2016 11:52
-
-
Save QuingKhaos/efd75f0e04a43421f0cf94c2c2e2d74f to your computer and use it in GitHub Desktop.
Resolving the parent and next sibling page of a Sulu content page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<template xmlns="http://schemas.sulu.io/template/template" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd"> | |
<key>default</key> | |
<view>ClientWebsiteBundle:templates:default</view> | |
<controller>ClientWebsiteBundle:Website:index</controller> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<template xmlns="http://schemas.sulu.io/template/template" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd"> | |
<key>news</key> | |
<view>ClientWebsiteBundle:templates:news</view> | |
<controller>ClientWebsiteBundle:Website:news</controller> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Client\Bundle\WebsiteBundle\Controller; | |
use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController as BaseWebsiteController; | |
use Sulu\Component\Content\Compat\Structure\PageBridge; | |
use Sulu\Component\Content\Compat\StructureInterface; | |
use Sulu\Component\Util\SortUtils; | |
use Symfony\Component\HttpFoundation\Response; | |
class WebsiteController extends BaseWebsiteController | |
{ | |
/** | |
* Loads the content from the request (filled by the route provider) and | |
* creates a response with this content and the appropriate cache headers. | |
* | |
* @param PageBridge $structure | |
* @param bool $preview | |
* @param bool $partial | |
* | |
* @return Response | |
*/ | |
public function indexAction(PageBridge $structure, $preview = false, $partial = false) | |
{ | |
/** @var StructureInterface[] $siblings */ | |
$siblings = $structure->getParent()->getChildren(); | |
$next = $this->resolveNextSibling($structure->getUuid(), $siblings); | |
$response = $this->renderStructure( | |
$structure, | |
[ | |
'parent' => $this->getParentItemData($structure), | |
'next' => $next ? $this->structureToItemData($next) : null, | |
], | |
$preview, | |
$partial | |
); | |
return $response; | |
} | |
/** | |
* Loads the content from the request (filled by the route provider) and | |
* creates a response with this content and the appropriate cache headers. | |
* | |
* @param PageBridge $structure | |
* @param bool $preview | |
* @param bool $partial | |
* | |
* @return Response | |
*/ | |
public function newsAction(PageBridge $structure, $preview = false, $partial = false) | |
{ | |
/** @var StructureInterface[] $siblings */ | |
$siblings = $structure->getParent()->getChildren(); | |
$siblings = SortUtils::multisort($siblings, 'publish_date', 'DESC'); | |
$next = $this->resolveNextSibling($structure->getUuid(), $siblings); | |
$nextData = null; | |
if ($next) { | |
$nextData = $this->structureToItemData($next); | |
$nextData['publish_date'] = $next['content']['publish_date']; | |
} | |
$response = $this->renderStructure( | |
$structure, | |
[ | |
'parent' => $this->getParentItemData($structure), | |
'next' => $nextData, | |
], | |
$preview, | |
$partial | |
); | |
return $response; | |
} | |
/** | |
* @param string $uuid The current structure UUID | |
* @param StructureInterface[] $siblings | |
* | |
* @return null|StructureInterface[] | |
*/ | |
protected function resolveNextSibling($uuid, $siblings) | |
{ | |
$foundCurrent = false; | |
$next = null; | |
foreach ($siblings as $sibling) { | |
if ($sibling->getUuid() === $uuid) { | |
$foundCurrent = true; | |
} elseif (!$sibling->getPublishedState()) { | |
continue; | |
} elseif ($foundCurrent) { | |
$next = $this->get('sulu_website.resolver.structure')->resolve($sibling); | |
break; | |
} | |
} | |
return $next; | |
} | |
/** | |
* @param PageBridge $structure | |
* | |
* @return array | |
*/ | |
protected function getParentItemData(PageBridge $structure) | |
{ | |
$parentStructure = $structure->getParent(); | |
$parentNode = $this->get('sulu_website.resolver.structure')->resolve($parentStructure); | |
if ('overview_news' === $parentNode['template']) { | |
$newsParent = $this->get('sulu_website.resolver.structure')->resolve($parentStructure->getParent()); | |
if ($newsParent['content']['year'] === $parentNode['content']['year']) { | |
$parentNode = $newsParent; | |
} | |
} | |
return $this->structureToItemData($parentNode); | |
} | |
private function structureToItemData($structure) | |
{ | |
return [ | |
'url' => $structure['content']['url'], | |
'title' => $structure['content']['title'], | |
'ext_title' => $structure['extension']['excerpt']['title'], | |
'subtitle' => $structure['content']['subtitle'], | |
'image' => $structure['content']['header'], | |
'ext_image' => $structure['extension']['excerpt']['images'], | |
'teaser' => !empty($structure['content']['teaser']) ? $structure['content']['teaser'] : '', | |
'ext_teaser' => $structure['extension']['excerpt']['description'], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment