0

I am trying to add menu items in Magento using an observer and I have managed that but now I need to sort the order of the menu items by an attribute inside of each menuelement object (the attribute is position so I want to sort it so position 1 comes first, 2 comes second etc.). I have tried usort but can't get it to work

public function addItemsToTopmenuItems($observer)
{
    $menu = $observer->getMenu();
    $tree = $menu->getTree();

    $collection = Mage::getModel('sean_menucreator/menuelement')->getCollection();
    $menuelementNodeId = 'menuelement'; 

    foreach($collection as $menuitem) {
        if($menuitem->getLevel()=='1'){
            $node = new Varien_Data_Tree_Node(array(
                'name'   => $menuitem->getName(),
                'id' => $menuelementNodeId.'_'.$menuitem->getEntity_id(),
                'url'    => $menuitem->getUrl(), // point somewhere
            ), 'id', $tree, $menu);
            $menu->addChild($node);

            $parent_id = $menuitem->getEntity_id();

            $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
            foreach ($collection as $menuitem){
                if(isset($parent_id) && $menuitem->getParent_id() == $parent_id) {
                    $tree = $node->getTree();
                    $data = array(
                        'name' => $menuitem->getName(),
                        'id' => $menuelementNodeId.'_'.$menuitem->getEntity_id(),
                        'url' => $menuitem->getUrl(),
                        'is_active' => ($action == 'sean_menucreator_menuelement_index' || $action == 'sean_menucreator_menuelement_view')
                    );

                    $menuelementNode = new Varien_Data_Tree_Node($data, 'id', $tree, $node);
                    $node->addChild($menuelementNode);

                }
            }
        }
    }
    return $this;
}

As you can see $collection is a collection of menuelements and each menuelement is an object - is the way to do this somehow sort the initial collection before I do my foreach's? therefore initially the collection will all be in order so it will add them in order?

0

please try ksort

for example

<?php
$cats = Mage::getModel('catalog/category')->load(319)->getChildren();
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
       $category = Mage::getModel('catalog/category')->load($catId); 
       $categories[$category->getName()] = $category->getUrl();
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
    <li>
    <a href="<?php echo $url; ?>"><?php echo $name; ?></a>
    </li>
<?php endforeach; ?>
</ul>

I hope this will help you.

3
  • This wouldn't work, ksort sorts based on keys, not values and the key will always be 'position' and do remember I'm not sorting an array, I'm sorting an object based on its attributes
    – seanyt123
    Jan 5 '16 at 9:48
  • which attribute you want to sort on? Jan 5 '16 at 9:48
  • position, every $menuitem needs to be added to the menu via the Varien_Data_Tree_Node logic in order of it's 'position'
    – seanyt123
    Jan 5 '16 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.