Control output of object relations

Author Message

Stephen Martin

Monday 03 March 2008 3:35:53 pm

New user question. I've created a class called "animal" and am using a object relations to store images associated with the animal.

Using the code below I can output the photos, however, I'd like to output just the first photo instead of all of them. I've searched the forum carefully but cannot find a proper solution. Please advise. Thanks!

		{def $nodes=fetch( 'content', 'list',
        hash( 'parent_node_id', 59,
              'depth', 1 ) )}
 		{foreach $nodes as $node}
			<div id="animal-listing">
				<div id="animal-head">
					<h1>{$node.name|wash} {attribute_view_gui attribute=$node.object.data_map.genestar_rating}/{attribute_view_gui attribute=$node.object.data_map.genestar_rating_max} <img style="vertical-align:middle" src={'genestar-logo.gif'|ezimage()} alt="GeneStar Logo" /></h1>
					<div id="price">{attribute_view_gui attribute=$node.object.data_map.price}</div> {* price *}
				</div> {* animal-head *}
				<div id="animal-photo">
					{attribute_view_gui attribute=$node.object.data_map.photography}
				</div> {* animal-photo *}
			</div> {* animal-listing *}
		{/foreach}

Felix Laate

Tuesday 04 March 2008 12:37:50 am

Hi Stephen,

if you want he first image (dependning on the sorting):

{def $nodes=fetch( 'content', 'list',
                              hash( 'parent_node_id', 59,
                             'depth', 1,
                              'limit',1 ) )}

Or do you have other criteria?

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Stephen Martin

Tuesday 04 March 2008 8:39:38 am

Felix,

Thanks for your answer. It wasn't quite was I was looking for. Adding the limit does return one record, however, I'm looking to return the first image of an object relation. So, if I put the limit of 1 in there, I get just one record returned whereas I'd like all records (animals) displayed with the first related photo.

$node.object.data_map.photography is an object relations that contains multiple photos. I need a way to get just the first image out of it.

Hopefully that is clearer.

Thanks!

Xavier Dutoit

Wednesday 05 March 2008 4:41:19 am

{$node.object.data_map.photography|attribute(show,2)}

Will help you finding what you want

http://www.sydesy.com

Stephen Martin

Thursday 06 March 2008 9:11:47 am

Xavier,

Thanks for the reply. That tip helped me figure it out. Don't know if this is the most efficient solution, but this is the code:

{foreach $node.data_map.photography.content.relation_list as $relation}
     {def $obj=fetch('content', 'object',
			hash('object_id', $relation.contentobject_id))}
			{attribute_view_gui attribute=$obj.data_map.image image_class='medium'}		
	{undef $obj}
	{break}
{/foreach}

André R.

Thursday 06 March 2008 9:56:04 am

That will do fine, but just so you know it this will contain objects in the trash as well.
In newer eZ Publish versions there is attribute on the relation called ''in_trash" or something.
On older versions (and new ones) it is visible on the main_node_id of the object you fetch (it will be false / null / empty).

One alternative to looping over the relations list is to use the fetch('content', 'relation') (see doc).

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.