Programmatically Create Menu Item in Joomla

此生再无相见时 提交于 2019-12-01 06:37:53
Matteus Hemström

Try using 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.

$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.

Additionally, the convenience method "save" can be used to bind, check and store the 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;
}
Ankit Suthar

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 = 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();
Sanjay Singh

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!