prepareMenuTree (function)

Last updated: Fri, May 26, 2023 9:55 am
Return to Knowledgebase

Purpose

The prepareMenuTree function in UserSpice is a custom function used to build a hierarchical menu tree structure based on the provided menu data.

Location

users/helpers/menus.php

Parameters

# Parameter Data Type Required Description
1 $menuResults array Yes The menu data in the form of an array. This array should contain the menu items with their parent-child relationships and display order.

Returns

Data Type Description of Returned Data
array The function returns an array representing the hierarchical menu tree structure.

Further Documentation:

$menuResults = [
[
'id' => 1,
'parent' => 0,
'display_order' => 1,
// Other menu item properties
],
[
'id' => 2,
'parent' => 0,
'display_order' => 2,
// Other menu item properties
],
// Other menu items
];

// Get the hierarchical menu tree structure
$menuTree = prepareMenuTree($menuResults);

print_r($menuTree);



In the example above, the prepareMenuTree function is called with the $menuResults array as an argument. The $menuResults array contains the menu data, where each menu item is represented by an associative array with properties such as id, parent, and display_order. The id property represents the unique identifier of the menu item, the parent property represents the ID of the parent menu item (or 0 for top-level items), and the display_order property specifies the order in which the menu items should be displayed.

The function processes the menu data and builds a hierarchical menu tree structure based on the provided relationships and display order. The resulting tree structure is returned as an array. You can then further manipulate or use this menu tree structure as needed in your application.

Note: The commented line $menuTree = $treeManager->slapTree($recordsTree, 1 ); suggests that there might be an additional step to indent the tree using the slapTree method of the treeManager class. However, it's commented out in the provided code snippet, so it's not active.