Building extensions - interact with the menu extension

The menu extension allow other extensions to add links to the menu and the link picker, with just a few lines of code. To use the functionality from the menu extension, import the following file as shown below.

SMExtensionManager::Import("SMMenu", "SMMenu.classes.php", true);
Add link to the link picker

When the user creates a new link in the menu extension, he or she may want to use the link picker to select a content page or an extension. The following few lines of code demonstrates how to add links to the link picker.

// The ready state is True when the link picker is being executed
if (SMMenuLinkList::GetInstance()->GetReadyState() === true)
{
    $menuLinkList = SMMenuLinkList::GetInstance();

    $menuLinkList->AddLink("My Test", "My extension", SMExtensionManager::GetExtensionUrl("MyExtension"));
    $menuLinkList->AddLink("My Test", "Google", "http://google.com");
}
Adding links to the link picker is possible during InitComplete and PreRender. The result from the code above will look something like the example image below.


/LinkPicker.png



Add link to the navigation menu

Adding links to the navigation menu is just as simple as adding links to the link picker. The following code snippet demonstrates this.

// Admin menu only exists if user is authorized (logged in)
if (SMAuthentication::Authorized() === true)
{
    $adminItem = SMMenuManager::GetInstance()->GetChild("SMMenuAdmin");
    $newChild = new SMMenuItem("UniqueId", "My extension", SMExtensionManager::GetExtensionUrl("MyTest"));
    $adminItem->AddChild($newChild);
}
It is possible to add links to the administration menu during PreTemplateUpdate only. Items may be added to the root of the menu during PreTemplateUpdate as well, as demonstrated in the following example.

$rootItem = SMMenuManager::GetInstance();
$newChild = new SMMenuItem("UniqueId", "My extension", SMExtensionManager::GetExtensionUrl("MyTest"));
$rootItem->AddChild($newChild)
Adding a permanent link to the menu

The following snippet is an example of how to create a permanent link in the navigation menu. The code should be executed during InitComplete or PreRender, in order for the new item to show up immediately after post back.

$rootItem = SMMenuManager::GetInstance();
$newChild = new SMMenuItem("UniqueId", "My extension", SMExtensionManager::GetExtensionUrl("MyTest"));
$newChild = $rootItem->AddChild($newChild);

$newChild->CommitPersistent();
SMMenu API

In the following section the API of the SMMenu extension is described.


SMMenuLinkList class

Function Return type
Description
::GetInstance() SMMenuLinkList
Get instance of SMMenuLinkList on which links may be added.
GetLinkCollection() Array Returns array used internally in the class to store link information. The array contains string arrays. Each of these arrays may be indexed using: category, title, url. Example: $array[0]["title"]
SetLinkCollection($arr:array)   Set array containing link information. The array must have the exact same layout as specified in the description for GetLinkCollection(). All string arrays must contain both category, title, and url.
AddLink($category:string, $title:string, $url:string   Create a new link with the specified title and URL. Multiple links created with the same category will be grouped together in the link picker. Links may be added during InitComplete and PreRender.
GetReadyState() Boolean Returns True when the link picker is being used, otherwise False. Use this function to determine whether to add links or not. The ready state is set during Init and will be available during InitComplete.
SetReadyState($value)
Used by the SMMenu extension controller to indicate that the link collection is going to be used. Specifying True will therefore result in link list being populated by all other extensions.


SMMenuManager class

Function Return type
Description
::GetInstance() SMMenuItem
Get instance of SMMenuItem (root item) on which links may be added.
AddChild($child:SMMenuItem)
SMMenuItem Add a new item to the root of the menu. This must be done during PreTemplateUpdate. Use returned SMMenuItem to perform further operations on the link, since it has been associated with its parent.
AddChild($child:SMMenuitem, $appendMode::SMMenuItemAppendMode) SMMenuItem See description for AddChild($child:SMMenuItem). Set prefered append mode specifying whether to add link to the beginning of the menu, or to the end. Notice that no matter what append mode is used, links added later, may change the position of your link. Use SMMenuItemAppendMode::$Beginning to add link to the beginning of the menu (usually in the top or left side of the menu), SMMenUItemAppendMode::$End (default) to add link to the end of the menu (usually in the bottom or right side of the menu).
RemoveChild($id:string) Boolean Removes a child with the given ID from the collection. Returns True on success, otherwise False.
RemoveChild($id:string, $commit:boolean) Boolean See RemoveChild(). Use this function to make the change permanent.
SetChildren($children:SMMenuItem[])   Set collection of menu items
GetChildren() SMMenuItem[] Get collection of menu items
GetChild($id:string) SMMenuItem Get a child from the collection of root items. Returns null if not found.
GetChild($id:string, $searchDeep:boolean) SMMenuItem See GetChild($id:string). Will search all nodes recursively.
MoveChildUp($id:string) Boolean Will move a child with the given ID up in the order of appearance. Returns True on success, otherwise False. Notice
MoveChildUp($id:string, $commit:boolean)
Boolean See MoveChildUp($id:string). Will make the change permanent.
MoveChildDown($id:string)
Boolean
Will move a child with the given ID down in the order of appearance. Returns True on success, otherwise False.
MoveChildUp($id:string, $commit:boolean) Boolean See MoveChildDown($id:string). Will make the change permanent.


SMMenuItem class

Function Return type
Description
__construct($id:string, $title:string, $url:string)

Create a new menu link. $Id must be a unique value.
AddChild($child:SMMenuItem)
SMMenuItem See description for AddChild($child:SMMenuItem) on SMMenuManager.
AddChild($child:SMMenuItem, SMMenuItemAppendMode)
SMMenuItem See description for AddChild($child:SMMenuItem, SMMenuItemAppendMode) on SMMenuManager.
RemoveChild($id:string) Boolean See description for RemoveChild($id:string) on SMMenuManager
RemoveChild($id:string, $commit:boolean) Boolean See description for RemoveChild($id:string, $commit:boolean) on SMMenuManager
SetChildren($children:SMMenuItem[])   See description for SetChildren($children:SMMenuItem[]) on SMMenuManager
GetChildren() SMMenuItem[] See description for getChildren() on SMMenuManager
GetChild($id:string) SMMenuItem See description for GetChild($id:string) on SMMenuManager
GetParentId() String Returns the ID of the parent node. An empty string is returned, if node is a root node.
GetId
String Returns the ID of the child.
SetTitle($value:string)

Set the title of the child.
GetTitle() String Get the title of the child.
SetUrl($url:string)   Set the URL of the child.
GetUrl() String Get the URL of the child.
SetOrder($order:string)   Set the order of the child in proportion to its siblings. The child with the lowest order number will have the top most position in the navigation menu.
GetOrder() String Get the order (position) of the child.
DeletePersistent() Boolean Permanently removes the item from the data source. Notice that children are preserved in the data source. Returns True on success, otherwise False.
DeletePersistent($deleteChildren:boolean) Boolean See description for DeletePersistent(). All children and their children are removed along with the given node.
CommitPersistent() Boolean
Make menu item permanent by committing it to the data source. If item is supposed to be a child of another node, make sure to add it to that node before committing it - see description for AddChild($child:SMMenuItem). Returns True on success, otherwise False.
CommitPersistent($commitChildren:boolean)
Boolean
See description for CommitPersistent(). If $commitChildren is True all children of the given node, and their children, will be committed.
::GetPersistent($id:string)
SMMenuItem
Load the item with the given ID from the data source. Null is returned if not found.
::GetPersistent($id:string, $getChildren:boolean) SMMenuItem
See description for GetPersistent($id:string). All children, and their children, are included.


SMMenuLoader class

Function Return type Description
::GetMenuItems SMMenuItem[] Get all root items including children, and their children, from the menu.