eZ Platform Discussions

Render relation objetcs on template twig


#1

Hi, i am new in ezpublish5 it’s possible to explain me how to display the content of relation objects in the parent template.

Here my controller

public function getRelatedArticleAction($id, $show) { $repository=$this->getRepository(); $searchService = $repository->getSearchService(); $result=array(); $query = new LocationQuery(); $criterionAnd = new Criterion\LogicalAnd( array() ); $criterionAnd->criteria[] = new Criterion\Visibility( Criterion\Visibility::VISIBLE ); $criterionAnd->criteria[] = new Criterion\ContentTypeIdentifier( "articlet" ); $criterionAnd->criteria[]= new Criterion\FieldRelation('article_video',Operator::CONTAINS,array($id));
	$query->sortClauses = array(new SortClause\DatePublished( Query::SORT_DESC ));
	$result=$searchService->findContent( $query )->searchHits;


	//var_dump($result); die();
	return $this->render("MyWebSiteBundle:render:view_related_article.html.twig", array('result'=>$result,'show'=>$show));
}

#2

Rendering the field with {{ ez_render_field( content, ‘article_video’ ) }} would be easiest. This would show line views of the related content.

If you wish to show another view of the related content you would pass to the parameters an alternate template to use. https://ez-systems-developer-documentation.readthedocs-hosted.com/en/master/guide/content_rendering/#prototype-and-arguments_9


#3

Please do you have a complete example of the related content via parametrers.
I newer dit that. I am new to ezpublish5


#4

You can use the ez_content:viewAction Controller and it will take care to load the relation object content.
Add in Twig:
{{ render(controller("ez_content:viewAction", {"contentId": content.getFieldValue('article_video'), "viewType": "line"})) }}

Add in ezplatform.yml


#...
system:
    default:
        content_view:
            line:
                article_video:
                    template: 'line/article_video.html.twig'
                    match:
                        Identifier\ContentType: [YOUR-VIDEO-CONTENTTYPE-IDENTIFIER]

Create then the line folder in Resources/views and add your template there. You could may be use it in your custom bundle, you should then add the bundle name to the template. you can check symfony doc on this.
In your template you should have access to all FieldType values. See {{ dump(content) }} or {{ content.getFieldValue('FIELDTYPE-IDENTIFIER')}}