Created
September 17, 2013 14:42
-
-
Save dfritschy/6595267 to your computer and use it in GitHub Desktop.
eZ Publish 5: Is this a viable pattern for handling sub-items of a content object, for example children of a folder?
This file contains 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
{% extends ... %} | |
{% block content %} | |
{# Render the full view of the content object #} | |
{% if content.field['title'] is defined %} | |
<h1>{{ ez_render_field( content, 'title') }}</h1> | |
{% else %} | |
<h1>{{ ez_render_field(content, 'name') }}</h1> | |
{% endif %} | |
{% if content.fields['short_description'] is defined %} | |
<div class="attribute-short_description"> | |
{{ ez_render_field( content, 'short_description') }} | |
</div> | |
{% endif %} | |
{% if content.fields['description'] is defined %} | |
<div class="attribute-description"> | |
{{ ez_render_field( content, 'description') }} | |
</div> | |
{% endif %} | |
{# Render sub-items using a sub-request #} | |
{{ render( controller( "MyBundle:Default:subItems", {'locationId': location.id }), {'strategy': 'esi'} ) }} | |
{% endblock %} |
This file contains 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
{# | |
Render a content object in line view | |
#} | |
<div class="class-{{ content.contentInfo.contentTypeId }}"> | |
<h2><a href="{{ path( 'ez_urlalias', {'locationId': content.contentInfo.mainLocationId} ) }}">{{ ez_render_field( content, 'title') }}</a></h2> | |
{% if content.fields['short_description'] is defined %} | |
<div class="attribute-short_description"> | |
{{ ez_render_field( content, 'short_description') }} | |
</div> | |
{% endif %} | |
</div> |
This file contains 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
/** | |
* Renders sub-items of the current location | |
* | |
* TODO: add content type filtering, and possibly more | |
* TODO: add template to be used for rendering | |
* | |
* @param int $locationId | |
* @return \Symfony\Component\HttpFoundation\Response | |
*/ | |
public function subItemsAction( $locationId ) | |
{ | |
$response = new Response; | |
$response = $this->cacheResponse( $response, $locationId ); | |
$locationList = $this->fetchLocationListIncludingContentTypes( $locationId, array() ); | |
$contentList = array(); | |
foreach ( $locationList as $location ) | |
{ | |
$contentList[] = $this->getRepository()->getContentService()->loadContent( $location->contentId ); | |
} | |
return $this->render( | |
"WebmanufakturSiteDruckformBundle::sub_items.html.twig", | |
array( | |
"locationList" => $locationList, | |
"contentList" => $contentList | |
), | |
$response | |
); | |
} | |
/** | |
* Returns list of locations under given parent location, optionally pnly for given content types | |
* @param $locationId | |
* @param array $includeContentTypes | |
* @return array | |
*/ | |
private function fetchLocationListIncludingContentTypes( $locationId, array $includeContentTypes = array() ) | |
{ | |
$includeCriterion = array(); | |
if ( !empty( $includeContentTypes ) ) | |
{ | |
foreach ( $includeContentTypes as $contentTypeIdentifier ) | |
{ | |
$contentType = $this->getRepository()->getContentTypeService()->loadContentTypeByIdentifier( $contentTypeIdentifier ); | |
$includeCriterion[] = new Criterion\ContentTypeId( $contentType->id); | |
} | |
} | |
$criteria = array( | |
new Criterion\ParentLocationId( $locationId ), | |
new Criterion\Visibility( Criterion\Visibility::VISIBLE ) | |
); | |
if ( !empty( $includeCriterion ) ) | |
$criteria[] = new Criterion\LogicalAnd( $includeCriterion ); | |
$query = new Query( | |
array( | |
'criterion' => new Criterion\LogicalAnd( | |
$criteria | |
), | |
'sortClauses' => array( | |
new SortClause\LocationPriority( Query::SORT_ASC ) | |
) | |
) | |
); | |
$searchResult = $this->getRepository()->getSearchService()->findContent( $query ); | |
$locationList = array(); | |
if ( $searchResult instanceof SearchResult ) | |
{ | |
foreach ( $searchResult->searchHits as $searchHit ) | |
{ | |
$locationList[] = $this->getRepository()->getLocationService()->loadLocation( $searchHit->valueObject->contentInfo->mainLocationId ); | |
} | |
} | |
return $locationList; | |
} |
This file contains 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
{# | |
Process a list of children content objects and render them using line template | |
TODO: select default line view template based on content class | |
#} | |
<div class="content-view-children"> | |
{% for content in contentList %} | |
{% include "WebmanufakturSiteDruckformBundle::line.html.twig" %} | |
{% endfor %} | |
</div> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment