prepareDropdownString (function)

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

Purpose

The prepareDropdownString function in UserSpice is a custom function used to generate a string of HTML code for a dropdown menu item with its children items.

Location

users/helpers/menus.php

Parameters

# Parameter Data Type Required Description
1 $menuItem array Yes The menu item array containing information about the dropdown menu.
2 $user_id integer Yes The ID of the user.

Returns

Data Type Description of Returned Data
string The function returns a string that represents the generated HTML code for the dropdown menu item and its children.

Further Documentation:

$menuItem = array(
'icon_class' => 'fa fa-cogs',
'label' => 'Settings',
'children' => array(
array(
'id' => 1,
'logged_in' => 0,
// Other child item properties
),
array(
'id' => 2,
'logged_in' => 1,
// Other child item properties
),
// Other child items
)
);
$user_id = 123;

// Generate the dropdown menu string
$dropdownString = prepareDropdownString($menuItem, $user_id);
echo $dropdownString;



In the example above, the prepareDropdownString function is called with the $menuItem array and the $user_id variable as arguments. The $menuItem array contains information about the dropdown menu item, including its icon class, label, and children items.

The function iterates over the children items of the dropdown menu and checks the logged-in status and permission groups for each child item. If the child item is not restricted to logged-in users or if the user has the necessary permission groups, it calls the prepareItemString function to generate the HTML code for the child item. The generated HTML code is appended to the $itemString variable.

Finally, the function wraps the generated HTML code in
  • and
      tags to create the dropdown menu structure. The resulting string is returned by the function.

      In the example, the $dropdownString variable stores the generated HTML code for the dropdown menu. You can output this string in your HTML document to display the dropdown menu with its children items.