Skip to content

Instantly share code, notes, and snippets.

@phptek
Last active August 29, 2015 14:23
Show Gist options
  • Save phptek/c784e2f74e1f05ec5cb1 to your computer and use it in GitHub Desktop.
Save phptek/c784e2f74e1f05ec5cb1 to your computer and use it in GitHub Desktop.
/**
*
* Alternative and (will be) much faster menu generator to {@link ContentController::getMenu()}.
*
* - Uses raw DataQuery, skipping most of the ORM's heavy lifting
* - Doesn't require repeated calls to {@link Hierarchy::Children()}
* - Still works with unmodified template logic that calls $Menu(1).
SEE: Hierarchy::loadDescendantIDListInto()
*
* @param int $level Menu level to return.
* @param array $filter
* @return ArrayList
* @todo Re-engineer ORM call as a SQLQuery because it's faster
* @todo Weird shit going on when using Property names in templates within the loop = slows it right down
* @todo Add userland YML to allow SS to use getMenu() or getPerfMenu()
*/
public function getPerfMenu($level = 1, $filter = array()) {
// Get me all the pages - replace with non ORM query
$filterOn = array("ShowInMenus" => 1);
if($filter) {
$filterOn = array_merge($filterOn, $filter);
}
$nodes = SiteTree::get()->filter($filterOn);
$list = ArrayList::create($nodes->toArray());
$tree = array();
foreach($list as $node) {
$childNodes = array();
if($list->find('ParentID', $node->ParentID)) {
$childNodes[][$node->ID] = $node;
}
// Apply child-nodes in logic, rather than in-template: Avoids unecessary ORM calls
$node->LeafNodes = ArrayList::create($childNodes);
$tree[$level][$node->ID] = $node;
}
$items = ArrayList::create($tree[$level]);
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment