0

I've created a Joomla 3 component that will add some menu items programmatically. I looked for how to do this and I found this old question / answer: https://stackoverflow.com/questions/12651075/programmatically-create-menu-item-in-joomla

I suggest using this code:

// add the article to a menu item
$ 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 date.
if (! $ menuTable-> check ())
{
    $ this-> setError ($ menuTable-> getError ());
    return false;
}

// Store the data.
if (! $ menuTable-> store ())
{
    $ this-> setError ($ menuTable-> getError ());
    return false;
}

It works, but it has the same problem shown in the question that I related. Showing error:

Warning: str_repeat () [function.str-repeat]: The second argument must be greater than or equal to 0 in .....

Can someone help me with the accepted solution? I can not understand.

Try using:

JTableNested :: setLocation ($ referenceId, $ position = 'after'):
$ table-> setLocation ($ parent_id, 'last child');

I also think you need to rebuild the path:

// Rebuild the tree path.
if (! $ table-> rebuildPath ($ table-> id)) {
    $ this-> setError ($ table-> getError ());
    returns false;
}

Could someone explain me where to use this solution?

  • The spacing, IMO, is nasty in the original code. – mickmackusa Apr 12 at 23:23
  • The conditions in the first snippet can be combined into a single if condition. There is no str_replace() can in your posted codes. Please show us the relevant code of you can. – mickmackusa Apr 13 at 0:29
  • I think this answer is clear enough: stackoverflow.com/a/36481454/10015973 – Mohamed Abdelaziz Apr 13 at 13:00
  • I agree with Mohamed. The accepted answer is not always the best answer. – mickmackusa Apr 13 at 22:01

Your Answer

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

Browse other questions tagged or ask your own question.