How to add new admin menu items without reinstalling the component in joomla?

I am developing a component for Joomla 1.7.x, during development I need to add new menu items to the admin menu, it was easy to add new rows to the component table in DB in Joomla 1.5 times, but now it seems difficult to add a menu item by adding a new one row in menu table due to changes in database structure in Joomla 1.7

Is there an easy way to do this without reinstalling the component?

thanks

+4
source share
4 answers

The easiest way to find:

$table = JTable::getInstance('menu'); $data = array(); $data['menutype'] = 'main'; $data['client_id'] = 1; $data['title'] = 'ITEM TITLE'; $data['alias'] = 'com-component-name'; $data['link'] = 'index.php?option=com_component_name&view=default'; $data['type'] = 'component'; $data['published'] = '0'; $data['parent_id'] = '117'; // ID, under which you want to add an item $data['component_id'] = '10026'; // ID of the component $data['img'] = 'components/com_component_name/assets/images/icon.png'; $data['home'] = 0; if ( !$table->setLocation(117, 'last-child') // Parent ID, Position of an item || !$table->bind($data) || !$table->check() || !$table->store() ){ // Install failed, warn user and rollback changes JError::raiseWarning(1, $table->getError()); return false; } 

For removing:

 $table->delete(120); // item ID $table->rebuild(); 

Based on http://docs.joomla.org/Using_nested_sets#Adding_a_new_node

+3
source

Joomla 1.6+ menu items are stored in the #__menu table with a special type of menu called the “main” for the admin menu.

Find the identifier of the menu item of the main component. You can add sub-items to this by declaring the parent_id column as the identifier of your main menu item and setting the level column to 2.

The only problem you will run into is accepting nested sets (lft and rgt columns). This is the best way to handle parent-child relationships and organize menu items. I'm not sure if parent_id or lft / rgt is used at this point, but they are both full.

To add a new item, you will have to “bypass” all lft / rgt values ​​by two for menu items with a value greater than or equal to the position that you want to add to your menu item. This should include the rgt of your parent. If your parent element has no children, for your new element, the lft value will be the value of the parent left + 1. The value of the new rgt element will be the parent lft + 2.

One thing that should be noted with lft and rgt is that numbering applies to each menu item (front-end and back-end), so not doing it properly may have the potential to destroy your entire menu hierarchy. I think that’s why the parent_id column is still in use and that there is an “rebuild” menu option in the admin area.

http://en.wikipedia.org/wiki/Nested_set_model

0
source

Here are a few SQL queries that I came up with that did the trick (only the relevant parts are shown):

 SET @lastRgt := (SELECT rgt + 1 FROM #__menu WHERE alias="alias-of-preceding-menu-item"); UPDATE #__menu SET rgt=rgt+2 WHERE rgt > @lastRgt; UPDATE #__menu SET lft=lft+2 WHERE lft > @lastRgt; INSERT INTO #__menu (menutype, title, alias, path, link, type, published, parent_id, level, component_id, img, client_id, params, access, lft, rgt) VALUES(..., @lastRgt+1, @lastRgt+2); 

Worked for me on Joomla 2.5.

0
source

Valid answer needs to be updated for Joomla 3.x

I am sure that this is correct for older versions of Joomla, so I do not edit it.

This worked for me after further research and editing.

 $table = JTableNested::getInstance('Menu'); $data = array(); $data['menutype'] = 'main'; $data['client_id'] = 1; $data['title'] = 'ITEM TITLE'; $data['alias'] = 'com-component-name'; $data['link'] = 'index.php?option=com_component_name&view=default'; $data['type'] = 'component'; $data['published'] = '0'; $data['parent_id'] = '117'; // ID, under which you want to add an item $data['component_id'] = '10026'; // ID of the component $data['img'] = 'components/com_component_name/assets/images/icon.png'; $data['home'] = 0; $table->setLocation(117, 'last-child') // Parent ID, Position of an item if (!$table->bind($data) || !$table->check() || !$table->store()) { // Install failed, warn user and rollback changes JError::raiseWarning(1, $table->getError()); return false; } 
0
source

Source: https://habr.com/ru/post/1389353/


All Articles