1

I'm trying to create a menu item in joomla 3.x using php

I've been trying to get this code to work:

$menuItem = array(
    'menutype' => 'client-pages',
    'title' => "new Menu",
    'type' => 'component',
    'component_id' => 22,
    'link' => 'index.php?option=com_content&view=article&id='.$articleid,
    '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());
    echo "exception thrown";
    return false;
}

echo "<pre>";
print_r($menuTable);
echo "</pre>";

It doesn't throw errors and the print_r menu table makes it look like it's going to create the menu item

Joomla\CMS\Table\Menu Object
(
    [parent_id] => 5536
    [level] => 1
    [lft] => 327
    [rgt] => 328
    [alias] => new-menu
    [_location:protected] => last-child
    [_location_id:protected] => 
    [_cache:protected] => Array
        (
            [rebuild.sql] => 
SELECT id, alias
FROM #__menu
WHERE parent_id = %d
ORDER BY parent_id, lft
        ) ....

However no menu item is created - can someone suggest what I am doing wrong?

3

For starters, perhaps just an oversight on bringing your code to the JSE platform here, but...

$menuTable = JTable::getInstance('Menu', 'JTable', array());

You are passing in an empty array.

$newMenu = new stdClass();
$newMenu->title="blah";
$newMenu->type="component";
etc

Then you can do:

$menuTable        = JTable::getInstance('menu', 'JTable');
$menuData         = (array)$newMenu;

$menuTable->setLocation($parent_id, 'last-child');
$menuTable->save($menuData);

You are missing the following required properties in your $menuItem array.

  • alias
  • path
  • template_style_id
  • params

I'd also suggest doing this in a try/catch, which will be easier to diagnose.

try
{
    if ($menuTable->save($menuItem))
    {
        echo "Success";
    }
    else
    {
        echo "Failed";
    }
}
catch (Exception $e)
{
    throw new Exception('You might want to substitute this for another function, but $e will be your error exception', 500);
}
return $menuTable->id;
| improve this answer | |
  • thanks for the suggestion. I couldn't get it to work - I don't think I understand how $menuData = (array)$jMenus; fits into the code – user1616338 Jun 22 at 11:42
  • Apologies, updated code, it should read $menuData = (array)$newMenu – Grant G Jun 22 at 11:47
  • thanks got it to work - – user1616338 Jun 22 at 14:22
  • I don't know that the empty $config array as the 3rd param of getInstance() is a problem. docs.joomla.org/Using_the_JTable_class , api.joomla.org/cms-3/classes/… and save() will accept an object or an array as the first param: api.joomla.org/cms-3/classes/… It is important that we do not misinform the OP and researchers. Can you show me the docs page that shows the required data for the $menuData @Grant? – mickmackusa Jun 22 at 22:58
  • @mickmackusa both the $prefix and $config are optional as per menu.php model. The reason I know about those missing properties is through my own trial and error. The code in my OP is the code I am currently using - never did find the requirements per se, but I do suspect the lack of alias and params was the root cause of the issue here as these are always set in both the model and menu helpers. – Grant G Jun 23 at 0:28

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.