Programmatically Create Menu Item in Joomla

后端 未结 4 1794
慢半拍i
慢半拍i 2021-01-13 22:23

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

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 22:34

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

提交回复
热议问题