Skip to content
Advertisement

Programmatically Create Menu Item in Joomla

I have created a component in joomla 2.5 that creates a new article and adds that article to a menu item.

Creating the article is working fine, but I am having some trouble with creating the menu item.

I have the following code:

JavaScript

The error seems to be with setting the parent_id and level. On debugging libraries/joomla/database/tablenested.php sets the parent_id and level to 0. This caused the following error on my administrator page:

Warning: str_repeat() [function.str-repeat]: Second argument has to be greater than or equal to 0 in /Applications/MAMP/htdocs/joomla_2_5/administrator/components/com_menus/views/items/tmpl/default.php on line 129

Advertisement

Answer

Try using JTableNested::setLocation($referenceId, $position = 'after'):

JavaScript

I also think that you need to rebuild the path:

JavaScript

If it still doesn’t work, try to find out what MenusModelItem::save does that you don’t.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement