Skip to content

Instantly share code, notes, and snippets.

@dfritschy
Created May 28, 2014 12:02
Show Gist options
  • Save dfritschy/4b511c033480df27a870 to your computer and use it in GitHub Desktop.
Save dfritschy/4b511c033480df27a870 to your computer and use it in GitHub Desktop.
A sample frontpage controller which picks a random banner image from a list of related objects
public function indexAction( $locationId = 2 )
{
$response = new Response;
$response = $this->cacheResponse( $response, $locationId, 10 );
/** @var $repository \eZ\Publish\API\Repository\Repository */
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$location = $locationService->loadLocation( $locationId );
$content = $contentService->loadContent( $location->contentId );
// get relations of the current version
$versionInfo = $contentService->loadVersionInfo( $content->contentInfo );
$relations = $contentService->loadRelations( $versionInfo );
$related = null;
/** @var $random \eZ\Publish\API\Repository\Values\Content\Relation */
if ( count( $relations ) )
{
$random = $relations[array_rand( $relations )];
$related = $contentService->loadContent( $random->destinationContentInfo->id );
}
return $this->render(
"WebmanufakturSiteFrbBundle:full:frb_folder_section.html.twig",
array(
'content' => $content,
'related' => $related
),
$response
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment