How do I sort a custom class' selection attribute?

Author Message

Mindshare Interactive Campaigns

Wednesday 13 September 2006 8:17:24 am

I have a custom class with a multiple selection attribute named <b>Country</b>. This attribute lists several countries listed in no particular order.

This code is being used to display it as a dropdown:

<select name="study_country">
   <option value="-99" selected>Please select a country...</option>
   <option disabled>&nbsp;</option>
   {def $countries = fetch('content', 'class_attribute', hash('attribute_id', 308))}
     {foreach $countries.content.options as $dropdown_item}
        <option>{$dropdown_item.name}</option>
     {/foreach}
 </select>

This displays the countries in whatever order they are listed in the class definition. I need these to be sorted alphabetically. <b>fetch('content', 'class_attribute')</b> does not seem to allow this.

How can I accomplish this?

http://www.mindshare.net

Claudia Kosny

Thursday 14 September 2006 11:19:34 am

Hello there

As far as I know there is no inbuilt way to do this. The easy way out is to fetch all the country names in an array, sort this array and then loop through this array. Unfortunately there is no array sort function built into EZ so either take the contribution
http://ez.no/community/contribs/template_plugins/arraysortoperator
or just write your own wrapper for the php sort function.

Greetings from Luxembourg

Claudia

Mindshare Interactive Campaigns

Friday 15 September 2006 12:12:52 pm

Thank you for the suggestion Claudia. I installed the extension and rewrote the code above like so:

 {def $country_array = array("foo")}
 {def $countries = fetch('content', 'class_attribute', hash('attribute_id', 308))}
 
 {foreach $countries.content.options as $dropdown_item}
   {set $country_array = $country_array|append($dropdown_item.name)} 	
 {/foreach}
 
 {set $country_array = $country_array|remove(0,1)}
 {def $sorted_country_array = $country_array|sort('string')}
 
 <select name="study_country">
   <option value="-99" selected>Please select a country...</option>
   <option disabled>&nbsp;</option>
   {foreach $sorted_country_array as $sorted_country}
     <option>{$sorted_country}</option>
   {/foreach}
 </select>

This bit of code presents the list of attributes in the same order; i.e. the arraysortoperator extension is doing nothing. I noticed that the extension's description page states that it works with version 3.6 and 3.7, but I am using 3.8.

Any idea what is going wrong?

http://www.mindshare.net

Claudia Kosny

Friday 15 September 2006 1:18:57 pm

Hi there

The sort operator of this extension works fine for me (EZ 3.8.3). Are you sure that the extension is loaded alright? If not you should see an message in the debug output that the operator sort is not defined. Another possible thhough unlikely problem might be interference from another extension which might already implement an sort operator wich behaves differently.

Greetings from Luxembourg

Claudia

Mindshare Interactive Campaigns

Wednesday 20 September 2006 7:23:45 am

Good thinking. I turned on the debug output and found this line:

Warning: eZTemplate  	Sep 20 2006 17:19:51
Operator 'sort' is not registered

This is confusing though because the extension is registered in the admin screen. I tried both registering it manually by updating the necessary .ini files as well as using the Setup -> Extension form.

Am I missing a step?

http://www.mindshare.net

Claudia Kosny

Wednesday 20 September 2006 7:42:11 am

Hi there

Maybe the directry structure if the extension is not correct - this happens quite often to me if I unzip into a directory with the name of the extension and the zipfile provides already such a directory. So check your extensions folder and open the folder for the array sort extension. There you should see the folders autoloads and maybe settings (I already deleted the extension again so I am not sure).

Claudia

Mindshare Interactive Campaigns

Wednesday 20 September 2006 10:36:26 am

Claudia, you're brilliant!

I never considered that the files were not unzipped correctly. I went into the extension directory and found this:

|-extension
|---arraysortoperator
|-----docs
|-------autoloads
|-------classes
|-------settings

The <b>docs</b> directory contained the other directories, which is clearly wrong. The correct directory structure should look like this:

|-extension
|---arraysortoperator
|-----docs
|-----autoloads
|-----classes
|-----settings

Now my countries are sorted correctly. Thank you!

http://www.mindshare.net

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

eZ debug

Timing: Jan 31 2025 01:26:43
Script start
Timing: Jan 31 2025 01:26:43
Module start 'layout'
Timing: Jan 31 2025 01:26:43
Module start 'content'
Timing: Jan 31 2025 01:26:43
Module end 'content'
Timing: Jan 31 2025 01:26:43
Script end

Main resources:

Total runtime0.6461 sec
Peak memory usage8,192.0000 KB
Database Queries69

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 588.2813151.2266
Module start 'layout' 0.00540.0039 739.5078220.7188
Module start 'content' 0.00930.6360 960.22664,354.4531
Module end 'content' 0.64530.0008 5,314.679715.8125
Script end 0.6461  5,330.4922 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00370.5717160.0002
Check MTime0.00130.2032160.0001
Mysql Total
Database connection0.00080.130610.0008
Mysqli_queries0.555085.9065690.0080
Looping result0.00060.0954670.0000
Template Total0.597592.520.2988
Template load0.00220.342720.0011
Template processing0.595392.135620.2976
Template load and register function0.00150.227310.0015
states
state_id_array0.00080.118110.0008
state_identifier_array0.00070.103720.0003
Override
Cache load0.00190.2993410.0000
Sytem overhead
Fetch class attribute can translate value0.00180.281020.0009
Fetch class attribute name0.00200.308870.0003
XML
Image XML parsing0.00030.039020.0001
class_abstraction
Instantiating content class attribute0.00000.002170.0000
General
dbfile0.00160.2405100.0002
String conversion0.00000.001140.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
13content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 28
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs