Dynamic Menu Item

I have a navbar menu item containing an external URL.
The external URL address needs to change based on who is logged in.I tried this Menu_Rendering code, but it renders prefixes the URL with “Pages/.” which makes the link fail.

function Menu_Rendering($menu)
// Change menu items here
{
	$docserver = ExecuteScalar("SELECT Value from Settings where V_Setting='".getSchoolID()."_DocumentServer'");
	if ($menu->Id == "navbar") { // Sidebar menu or change from "menu" to "navbar" for top menu
        $menu->addMenuItem(10000, "MyName", "Document server", ".$docserver.", -1, "", IsLoggedIn());
    }
}

Why does this function assume the URL is relative?
How can i stop it from doing that?

If your URLs are absolute, make sure your URL starts with “/” or “http(s)://”.

my url begins with https:// and the variable outputs the URL correctly if I echo it.I used example 1 from the manual as a basis for my functionAs a side note, there is no explanation in the manual as to what exactly each of the objects in the function actually do. Those in bold below i still don’t understand:
$menu->addMenuItem(10000, “MyName”, “MyMenuText”, “MyPage”,** -1**, “”, IsLoggedIn());

philmills wrote:

$menu->addMenuItem(10000, “MyName”, “Document server”, “.$docserver.”, -1, “”, IsLoggedIn());

But your URLs starts with “.”. Are you trying to do some (unnecessary) string concatenation?

I found the error.The $docserver variable needed to be double-quoted… “”.$docserver.“” not “.$docserver.”
oopsI’d still like to know especially what “MyName” in the manual example does - as far as I can see, it does nothing at all.Also how can I add a fontawesome icon to the menu item?

The menu item name is used to identify the menu item in server event. (The menu item ID may change if you move the menu item, while the menu item Text varies in multi-language project.)To add a menu icon you may simply enter the font awesome class names in Icon Class Name column of the Menu Editor.

I know how to add icons using the menu editor, all my other menu items have them already.
But this menu item is generated in the Menu_Rendering function not in the editor, and I need to add an icon for it. How?

The method signature is:
public function addMenuItem($id, $name, $text, $url, $parentId = -1, $src = “”, $allowed = true, $isHeader = false, $isCustomUrl = false, $icon = “”, $label = “”, $isNavbarItem = false)

Thanks! i got it working now :slight_smile:

Is there any way to set target=“_blank” for the menu item so it opens in a new window?

https://discourse.hkvstore.com/t/how-to-open-a-page-in-new-tab-from-menu-item-v2021/4052/1