4

I have been developing a component for Joomla 1.7.x, during development I need to add new component menu items to admin menu, it was easy by adding new rows to components table in DB in Joomla 1.5 times, but now it seems complicated to add a menu item by adding new row to menu table because of the database structure changes in Joomla 1.7

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

tHanks

Umut KIRGÖZ
  • 2,105
  • 3
  • 22
  • 29

4 Answers4

3

The easiest way I found:

$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;
}

To delete:

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

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

admit
  • 31
  • 3
0

Here's a few SQL queries I came up with that did the trick (only the relevant parts 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.

Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
0

Admit's answer is in need of an update for Joomla 3.x

I'm sure it's correct for older Joomla versions which is why I'm not editing 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;
}
Seoras
  • 1,286
  • 13
  • 21
0

Joomla 1.6+ menu items are stored under the #__menu table, with a special menu type called "main" for the admin menu.

Find your main component admin menu item's ID. You can add sub-items of that by declaring the parent_id column as the id of your main menu item, and setting the level column to 2.

The only other issue you are going to run into is the adoption of nested sets (lft and rgt columns). This is better way to handle parent-child relationships and the ordering of menu items. I'm unsure whether or not the parent_id or lft/rgt are used at this stage, but they are both filled in.

To add a new item, you would have to "shunt" all lft/rgt values by two for menu items with a value greater or equal to the position you wish to add your menu item. This should include the rgt of your parent item. If your parent item has no children, the lft for your new item will be the value of parent's left + 1. The value of the new item's rgt will be parent's lft + 2.

One thing to note with the lft and rgt is that the numbering applies to every menu item (front-end and back-end), so not doing it properly may have the potential to break your entire menu heirarchy. I think this is why the parent_id column is still used, and that there is an option in the admin area to "rebuild" the menu.

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

pharalia
  • 709
  • 4
  • 6