Programmatically Create Menu Item in Joomla

后端 未结 4 1831
慢半拍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条回答
  •  猫巷女王i
    2021-01-13 22:33

    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();
    

提交回复
热议问题