0

Im creating a component, where among other things, can create and update menus, and i have successfully used this answer to create a new menu item. Programmatically Add Menu Item in Component

$menuTable = JTableNested::getInstance('Menu');

// which menu you want to add to - 
$menutype = 'thisismymenusname';

// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'menutype' => $menutype,
  'title' => $table->alias,
  'alias' => $table->alias,
  'path' => $table->alias,
  'type' => 'heading',
  'component_id' => 0,                  
  'language' => '*',
   'published' => 1,
);

// this item is at the root so the parent id needs to be 1
$parent_id = 1;
$menuTable->setLocation($parent_id, 'last-child');

// save is the shortcut method for bind, check and store
if (!$menuTable->save($menuData))
{
  $this->setError($menuTable->getError());
  return false;
}

but im really unable to find the correct way to update a menu item, like the title and alias. but im unsuccessfully. I have tried using this with no success.

            $menu_title = $_POST['title'];
            $menu_alias = JFilterOutput::stringURLSafe($menu_title);

            $menuTable = JTableNested::getInstance('Menu');

            // this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
            $menuData = array(
                'title' => $menu_title,
                'alias' => $menu_alias,
            );

            // this item is at the root so the parent id needs to be 1
            $parent_id = ($_POST['parent'] != "" ? $_POST['parent'] : '1');
            $menuTable->setLocation($parent_id, 'last-child');

            // save is the shortcut method for bind, check and store
            if (!$menuTable->set($_POST['menuid'],$menuData)) {
                $this->setError($menuTable->getError());
                exit;
            }

can any help

1

If the menu item already exists then you don't need jtablenested at all - you should just update the fields directly in the database. What you do need is the menu item's id in Joomla's menu table. You should probably store it when you create the menu item the first time around as your menu item presumably corresponds to something in your custom component. Once you have that just do this:

$menuitem= new stdClass();
$menuitem->id = $the_id_of_the_menu_item_you_re_updating;
$menuitem->title = $title;
$menuitem->path = $path;
$menuitem->alias = $alias;
$result = JFactory::getDbo()->updateObject('#__menu', $menuitem, 'id'); 

You should probably do a quick check for duplicate aliases etc. The only reason you need to use jtablenested to make the menu item in the first place is because of the calculation of the right and left positioning which is a bit nuts.

  • Yes I got that working but the problem is when put it in as a submenu. I have to rebuild the ltf rgt, but that I don't the call for. – bymem Feb 15 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.