I needed to sort a summary list, in this case from the Uploads module, based on the content of a customfield. Problem is that the custom fields in Uploads, as in many other modules, are a sub-list within the main item/entry object. Sorting the custom fields had no impact on the main list.
Solution:
Code: Select all
{assign var=newarr value=''}
{foreach from=$items item='extra'}
{foreach from=$extra->fields item=fld}
{assign var='custom' value='custom_'|cat:$fld.name}
{append var='extra' value=$fld.value index=$custom}
{/foreach}
{append var='newarr' value=$extra}
{/foreach}
I then have a flat object/array and can sort on the 'custom_MyFieldName' key.
Next step was to sort the list. I tried using the @sort stuff but it was problemmatic. The following UDT called 'array_sort' solved it.
Code: Select all
$array = $params['array'];
$on = $params['on'];
$order = !empty($params['order']) ? strtolower($params['order']) : 'asc';
$smarty = cmsms()->GetSmarty();
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = trim($v2);
}
}
} else {
$sortable_array[$k] = trim($v);
}
}
switch ($order) {
case 'asc':
asort($sortable_array);
break;
case 'desc':
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
$smarty->assign('sorted', $new_array);
Code: Select all
{if count($newarr) gt 0}
{array_sort array=$newarr on='custom_MyFieldName' order='desc'}
{foreach from=$sorted item='entry'}
{if $entry.upload_id}
......
{/if}
{/foreach}
{/if}
The array_sort UDT outputs an array! Many summary lists, eg Uploads, by default output each item as an object. You need to ensure that your template is properly configured for arrays, eg change:
Code: Select all
{$entry->name}
Code: Select all
{$entry.name}
Have fun with it!
psy