difficult listing reordering problem

edited September 2014 in Modules
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

  • edited 3:17PM
    Hmmm... that may be tough. It sounds like this would be something that needs to do with the saved values. You could try adding a pre_process or post_process value to your weight field like so (haven't tested this... and the saved value is JSON encoded):
    $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?
  • edited 3:17PM
    actually i tried to use pre_process, but actually it is not to use cmp to work, but usort. On front end, when i display these encoded json values i can just use the 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");

    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.
  • edited 3:17PM
    When you say you can't, do you mean that it's just not working or are you getting an error message? Does the pre_process function even run do you know?
  • edited 3:17PM
    success to make it happen.

    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
  • edited 3:17PM
    If you add a "print_r($whatsnew); exit();" in the sortit function does it stop execution and print out the contents of the array?
  • edited September 2014
    yes, above function is working after i wrap a sortit function around usort($whatsnew,"cmp") which will then able to automatically handle the passed-in json value from database. This problem is solved, thanks.

    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.
  • edited 3:17PM
    Try doing the following:
    $block->add_field('weight', array('default'=>'0', 'pre_process' => create_function("$a, $b", "return cmp($a, $b);";
Sign In or Register to comment.