Programmatically create a menu item in Joomla

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

Creating an article works fine, but I'm having trouble creating a menu item.

I have the following code:

//add the article to a menu item $menuTable = JTable::getInstance('Menu', 'JTable', array()); $menuData = array( 'menutype' => 'client-pages', 'title' => $data[name], 'type' => 'component', 'component_id' => 22, 'link' => 'index.php?option=com_content&view=article&id='.$resultID, 'language' => '*', 'published' => 1, 'parent_id' => '1', 'level' => 1, ); // Bind data if (!$menuTable->bind($menuData)) { $this->setError($menuTable->getError()); return false; } // Check the data. if (!$menuTable->check()) { $this->setError($menuTable->getError()); return false; } // Store the data. if (!$menuTable->store()) { $this->setError($menuTable->getError()); return false; } 

It seems that the error is related to setting parent_id and level. When debugging libraries /joomla/database/tablenested.php sets parent_id and level to 0. This caused the following error on my admin page:

Warning: str_repeat () [function.str-repeat]: The second argument must 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

+4
source share
4 answers

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

 $table->setLocation($parent_id, 'last-child'); 

I also believe that you need to rebuild the path:

 // Rebuild the tree path. if (!$table->rebuildPath($table->id)) { $this->setError($table->getError()); return false; } 

If this still does not work, try to find out that MenusModelItem :: save does it wrong.

+4
source
 $table->setLocation($parent_id, 'last-child'); 

- this is all that is necessary to ensure the correct creation of left / right values ​​for a new menu item. There is no need to rebuild the path, as it is now being processed by the JTableMenu storage method.

In addition, the "save" method of a convenient method can be used to bind, check, and save a menu item:

 $menuItem = array( 'menutype' => 'client-pages', 'title' => $data[name], 'type' => 'component', 'component_id' => 22, 'link' => 'index.php?option=com_content&view=article&id='.$resultID, 'language' => '*', 'published' => 1, 'parent_id' => $parent_id, 'level' => 1, ); $menuTable = JTable::getInstance('Menu', 'JTable', array()); $menuTable->setLocation($parent_id, 'last-child'); if (!$menuTable->save($menuItem)) { throw new Exception($menuTable->getError()); return false; } 
+2
source

This code worked for me

  JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_menus/tables/'); $menuTable =& JTable::getInstance('menu', 'menusTable'); $menuData = array( 'menutype' => 'client-pages', 'title' => 'mytrialmenu', 'type' => 'component', 'component_id' => 22, 'link' => 'index.php?option=index.php? option='com_content&view=article&id='.$resultID, 'language' => '*', 'published' => 1, 'parent_id' => 'choose some parent', 'level' => 1, ); // Bind data if (!$row->bind($menuData)) { $this->setError($menuTable->getError()); return false; } // Check the data. if (!$row->check()) { $this->setError($menuTable->getError()); return false; } // Store the data. if (!$row->store()) { $this->setError($menuTable->getError()); return false; } 

I think the reason in menusTable extends the JnestedTable, which is required to manipulate the lft and rgt fields in the menu table

0
source

Somehow $menutable does not update the parent_id and level in the database table, so you need to manually update these two fields at the request of joomla.

 $menuTable = JTable::getInstance('Menu', 'JTable', array()); $menuData = array( 'menutype' => 'client-pages', 'title' => $data[name], 'type' => 'component', 'component_id' => 22, 'link' => 'index.php?option=com_content&view=article&id='.$resultID, 'language' => '*', 'published' => 1, 'parent_id' => '1', 'level' => 1, ); // Bind data if (!$menuTable->bind($menuData)) { $this->setError($menuTable->getError()); return false; } // Check the data. if (!$menuTable->check()) { $this->setError($menuTable->getError()); return false; } // Store the data. if (!$menuTable->store()) { $this->setError($menuTable->getError()); return false; } $db = $this->getDbo(); $qry = "UPDATE `#__menu` SET `parent_id` = 1 , `level` = 1 WHERE `id` = ".$menuTable->id; $db->setQuery($qry); $db->query(); 
0
source

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


All Articles