Share » Forums » Setup & design » Filter if Attribute has content or not

Filter if Attribute has content or not

Filter if Attribute has content or not

Thursday 06 January 2005 7:38:22 am - 6 replies

Modified on Thursday 06 January 2005 7:39:29 am by Nicolas Heiringhoff

Author Message

Roy Bøhmer

Thursday 06 January 2005 11:14:37 am

Hi Nico!
Only certain types of dataypes can be filtered in in a fetch using attribute-filter. See http://ez.no/ez_publish/documentation/reference/data_fetching/content/list

I really don't know if this will work, but if the attribute is of datatype ezstring (which it's probably not) you could try

'attribute_filter', array( array( 'your_class/aktuelles', 
                                               '>', 
                                               '0' ) )

It's good practice to also filter on class when filtering on attribute.

If this is not working, my only solution would be to fetch all nodes and make a counter in the loop and test the aktuelles-attribute for content.

{let counter=0}
{section loop=$nodes}
{section show=and($counter|lt(5), count_chars(trim($:item.data_map.aktuelles.data_text))|gt(0))}
  {set counter=$counter+1}
  <a href={concat("/content/view/full/",$:item.node_id)|ezurl}>{$:item.data_map.aktuelles.data_text}</a><br /><br />
 {/section}
{/section}
{/let}

This is a ugly hack and will continue to iterate the $node-loop even when 5 items has been written.

Hope someone comes up with a better solution.

Roy

Paul Borgermans

Thursday 06 January 2005 11:40:46 am

To determine if an attribute has content, you can also use the property <b>has_content</b>, like in the following example

...
{section show=and($counter|lt(5), $:item.data_map.aktuelles.has_content)}
...

If wether or not displaying the node is regardless of the attribute has content, you can save a long loop. If you really need to filter, I would suggest some heuristics like imposing a limit in the fetch of 20 or so. But looping is speedier now than in the old days of 3.0 and 3.1 :-)

hth

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Kristian Hole

Thursday 06 January 2005 12:12:11 pm

Hi!

I have a small note about your syntax. I would do something like this:

{let nodes=fetch( 'content', 'list', hash( 'parent_node_id', 2,
                                           'limit', 5, 
                                           'sort_by', array( 'published', false() )) ) }

{section var=node loop=$nodes}
{section show=$node.object.data_map.aktuelles.has_content}
<a href={$node.url_alias|ezurl}>{$node.object.data_map.aktuelles.content}</a><br /><br />
{/section}
{/section}

Notice the use of var in the section. This is much nicer, because namespaces can get very confusing if you have several loops inside eachother.

Also the $node.url_alias makes the nice urls, and not the system urls ("/my_funkystuff" as opposed to "content/view/213")

Kristian

http://ez.no/ez_publish/documenta...tricks/show_which_templates_are_used
http://ez.no/doc/ez_publish/techn...te_operators/miscellaneous/attribute

Nicolas Heiringhoff

Friday 07 January 2005 1:47:03 am

Thanx to everybody for your kind help!

The code form Kristian works fine. I have just a small problem with the sorting.

All my objects are in folders like "a", "b", "c" (and so on) in the folder with id 108 so I have modified the "list" to "tree". I have to set the limit to at least "70" to get 5 objects displayed.
Is this maybe because of the subfolders?
And unfortunately the displayed objects are not sorted by the date of publishing (the least published should be on top)

This is my code now


{let nodes=fetch( 'content', 'tree', hash( 'parent_node_id', 2,
                                           'limit', 500, 
                                           'sort_by', array( 'published', false() )) ) }

{section var=node loop=$nodes}
{section show=$node.object.data_map.aktuelles.has_content}
<a href={$node.url_alias|ezurl}>{$node.object.data_map.aktuelles.content}</a><br /><br />
{/section}
{/section}

Thank you,
Nico

http://www.heiringhoff.de

Kristian Hole

Friday 07 January 2005 3:49:19 am

Yeah. The 'list' (and node 2 (the root node)) was just there because i used that in my test. Sorry about that.

By the way should fetch only the classes that has the "aktuelles" attribute. Like this:

{let nodes=fetch( 'content', 'tree', hash( 'parent_node_id', 108,
                                           'limit', 10, 
                                           'class_filter_type',  'include',
                                           'class_filter_array', array( 'YOURCLASSHERE' ) ),
                                           'sort_by', array( 'published', false() )) ) }

{section var=node loop=$nodes}
{section show=$node.object.data_map.aktuelles.has_content}
<a href={$node.url_alias|ezurl}>{$node.object.data_map.aktuelles.content}</a><br /><br />
{/section}
{/section}

Where YOURCLASS here is the name of the class (eg article).

Kristian

http://ez.no/ez_publish/documenta...tricks/show_which_templates_are_used
http://ez.no/doc/ez_publish/techn...te_operators/miscellaneous/attribute

snorri s

Monday 03 January 2011 5:27:47 am

Hi,

it still IS an ugly hack. Looping through the result set and filtering by has_content is only appropriate if you have a small result set. If you have many content objects to fetch filtered on having contents in an specific attribute and/or want to use the google paginator you NEED to have an attribute_filter for <attribute> >0 or <attribute> !=''
How is it done? Still no better ideas around?

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu