Create menu item in Joomla and rebuild Automatically
Table of Content
Introduction
If we want to create menu item from your custom component in Joomla as there is no documentation or method in Joomla API, so we have to create it using the database. But when we do it directly we are not able to make complete entries for it as we can’t populate values for lft and rgt fields by using query and logic, and these values are also very important in Joomla for Ordering.
The process to create menu item from a custom component is to first create a menu type entry and also a corresponding entry in the Assets then we can create the menu items as per Joomla Standards.
The process to create menu item from a custom component is to first create a menu type entry and also a corresponding entry in the Assets then we can create the menu items as per Joomla Standards.
Implementation
To Create a proper menu item you need to follow the steps:
**Please note the methods called below are not in any class in this case if you add these methods to a class please use proper instance to call them
In your code add
/**
* Webkul Software.
*
* @category Webkul
* @author Webkul
* @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
if (createMenutype('custom', 'Custom') === true) {
createMenuitem(
'custom', 'My Custom MenuItem',
'index.php?option=com_custom&view=custom',
array( 'component_id' => (int)$component_id)
);
}
// with this code you are done.
Method’s Defination
/**
* Webkul Software.
*
* @category Webkul
* @author Webkul
* @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*
* CreateMenuitem function
*
* @param [type] $menuType var
* @param [type] $title var
* @param [type] $url var
* @param array $properties var
*
* @return void
*/
function createMenuitem(
$menuType, $title, $url, $properties = array()
) {
$alias = trim(
preg_replace(
'/_+/', '-', preg_replace(
'/\W+/', '', str_replace(
' ', '_', str_replace('_', '', trim(strtolower($title)))
)
)
)
);
$table = JTable::getInstance('Menu');
while ( $table->load(array('alias' => $alias))) {
$matches = null;
if (preg_match('#-(\d+)$#', $alias, $matches)) {
$alias = preg_replace(
'#-(\d+)$#', '-' . ($matches[1] + 1) . '', $alias
);
} else {
$alias .= '-2';
}
}
$menu = JTable::getInstance('menu');
$menu->menutype = $menuType;
$menu->title = $title;
$menu->alias = $alias;
$menu->link = $url;
$menu->type = 'component';
$menu->language = '*';
$menu->published = 1;
if ($properties) {
$menu->bind($properties);
}
$menu->check();
if (!$menu->store()) {
return false;
} else {
if (!$menu->parent_id) {
$menu->parent_id = 1;
}
if (! $menu->level) {
$menu->level = 1;
}
if (!$menu->store()) {
return false;
}
}
return true;
}
/**
* Webkul Software.
*
* @category Webkul
* @author Webkul
* @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*
* CreateMenutype function
*
* @param [type] $type var
* @param [type] $title var
* @param [type] $description var
* @param array $properties var
*
* @return void
*/
function createMenutype(
$type, $title, $description = null, $properties = array()
) {
$menuType = JTable::getInstance('MenuType');
if (!$menuType->load(array('menutype' => $type))) {
$menuType->menutype = $type;
$menuType->title = $title;
$menuType->description = $description;
if ($properties) {
$menuType->bind($properties);
}
$menuType->check();
if (!$menuType->store()) {
return false;
}
} else {
return null;
}
return true;
}
Conclusion
When creating the menu item in Joomla using with this all entries will be filled as per Joomla.
The corresponding entry for the menutype will be created in the #__assets with the lft and rgt, also you do not need to worry about the lft and rgt value in the #__menuitem as they will be filled when the JTable store() method is called, so you will not require to rebuild the menu items.
If you find this helpful please like and Share, so others can also find this solution.
If you have any query please feel free to comment below.
View Comments (8)
Comment or Ask a Question
Quick Links