[英]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.我在 joomla 2.5 中创建了一个组件,用于创建一篇新文章并将该文章添加到菜单项中。
Creating the article is working fine, but I am having some trouble with creating the 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;
}
The error seems to be with setting the parent_id and level.错误似乎与设置 parent_id 和级别有关。 On debugging libraries/joomla/database/tablenested.php sets the parent_id and level to 0. This caused the following error on my administrator page:
在调试 libraries/joomla/database/tablenested.php 时,将 parent_id 和 level 设置为 0。这导致我的管理员页面出现以下错误:
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
警告:str_repeat() [function.str-repeat]:/Applications/MAMP/htdocs/joomla_2_5/administrator/components/com_menus/views/items/tmpl/default.php 中的第二个参数必须大于或等于 0第 129 行
Try using JTableNested::setLocation($referenceId, $position = 'after'):
尝试使用
JTableNested::setLocation($referenceId, $position = 'after'):
$table->setLocation($parent_id, 'last-child');
I also think that you need to rebuild the path:我也认为你需要重建路径:
// Rebuild the tree path.
if (!$table->rebuildPath($table->id)) {
$this->setError($table->getError());
return false;
}
If it still doesn't work, try to find out what MenusModelItem::save does that you don't.如果它仍然不起作用,请尝试找出
MenusModelItem::save做了哪些你没有做的事情。
$table->setLocation($parent_id, 'last-child');
is all that is needed to ensure that left/right values are created correctly for the new menu item.这是确保为新菜单项正确创建左/右值所需的全部内容。 There is no need to rebuild the path as this is now handled by JTableMenu's store method.
无需重建路径,因为这现在由 JTableMenu 的 store 方法处理。
Additionally, the convenience method "save" can be used to bind, check and store the menu item:此外,还可以使用便捷方法“save”来绑定、检查和存储菜单项:
$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;
}
Somehow $menutable
does not update parent_id
and level
in database table so you have to manually update those two fields by joomla query.不知何故
$menutable
不会更新数据库表中的parent_id
和level
,因此您必须通过 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();
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 is menusTable extends JnestedTable which is required for manipulating lft and rgt fields in the menu table我认为原因是 menusTable 扩展了 JnestedTable,这是操作菜单表中的 lft 和 rgt 字段所必需的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.