i am using block layout to create some custom fields for backend pages.
$layout->add_fields(
array(
'whatsnew'=>array(
'type'=>'template',
'display_label'=>FALSE,
'label'=>'Sections',
'add_extra'=>FALSE,
'repeatable'=>TRUE,
'min'=>1,
'title_field'=>'block',
'fields'=>array(
'section'=>array('type'=>'section', 'value'=>'Section {num}'),
'block'=>array('type'=>'block','block_name'=>'whatsnew_article_block','display_label'=>FALSE, 'context' => 'whatsnew[0][block]',),
),
),
)
);
$block = new Fuel_block_layout('whatsnew_article_block');
$block->set_label('Whatsnew');
$block->add_field('link', array('label'=>'Link','ignore_representative' => FALSE, 'filter'=>'^en/whats-new/(.*)+$'));
$block->add_field('weight', array('default'=>'0'));
$config['blocks']['whatsnew_article_block'] = $block;
the codes working fine. However when editing the page, i wish the blocks to display according to descending weight. i have built a function
function cmp($a,$b)
{
if($a['block']['weight']==$b['block']['weight']) return 0;
return $a['block']['weight']>$b['block']['weight']?1:-1;
}
usort($whatsnew,"cmp");
however, i dont know how to slot this function in.
Comments
$block->add_field('weight', array('default'=>'0', 'pre_process' => 'cmp'));
One tricky thing with that is that they will be potentially move location on the screen based on their weight value which may cause some confusion right?
function cmp($a,$b)
{
if($a['block']['weight']==$b['block']['weight']) return 0;
return $a['block']['weight']>$b['block']['weight']?1:-1;
}
usort($whatsnew,"cmp");
but backend, i cant. The reason to display them in weight descending is to let user see the order of blocks just like what they will see in the front end when the list growing longer.
function cmp($a,$b)
{
if($a['block']['weight']==$b['block']['weight']) return 0;
return $a['block']['weight']>$b['block']['weight']?-1:1;
}
function sortit($whatsnew){
usort($whatsnew,"cmp");
return $whatsnew;
}
then do a pre_process=>'sortit' on template field. Again, admire the whole flexibility of this CMS
However one question would be if there are two parameter for a pre_process function. how to write it in block attribute?
$block->add_field('weight', array('default'=>'0', 'pre_process' => 'cmp(a,b)')); ??
i doubt this works though.
$block->add_field('weight', array('default'=>'0', 'pre_process' => create_function("$a, $b", "return cmp($a, $b);";