本文介绍了在 Joomla 中以编程方式创建菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 joomla 2.5 中创建了一个组件,用于创建一篇新文章并将该文章添加到菜单项中.

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.

我有以下代码:

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

错误似乎与设置 parent_id 和级别有关.在调试库/joomla/database/tablenested.php 时将 parent_id 和级别设置为 0.这导致我的管理员页面出现以下错误:

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:

警告:str_repeat() [function.str-repeat]:/Applications/MAMP/htdocs/joomla_2_5/administrator/components/com_menus/views/items/tmpl/default 中的第二个参数必须大于或等于 0.php 第 129 行

推荐答案

尝试使用 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 does that you don't.

这篇关于在 Joomla 中以编程方式创建菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:54