6

I am trying to create a component where the user can add a menu item to an existing Menu after creating the article (or selecting from a list of article). The closest I could come across to an example of sort was the answer linked below but it was for 2.x and looks the way to go. But one thing I noticed was there was nothing to show that it added any data to the #__assets table.

Am I missing something here but I remember that when a menu item is created when I add one from the backend, it also inserts a record into the assets table. So if I wanted to insert a new menu item, would I have to insert the relevant data to the #_assets table separately?

https://stackoverflow.com/questions/12651075/programatically-create-menu-item-in-joomla?lq=1

3 Answers 3

6

firstly - menu items are not put into the assets table, so this question is about a non-existent problem. The link provided in the question gives the correct answer for joom 2.5 and 3.x. but for reference here is an example in the prepareTable of a model.

$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;
}
1
  • This works well. Just be mindful of the menutype and parent_ids if you are going to add this to a loop.
    – Terry Carter
    Aug 29, 2018 at 18:40
4

Not really a solution, but an idea:

I recently learned about Kazaam!, a plugin that creates a menu based on your category- and article structure.

As the website states, All code is open source, and you are completely free to make changes.. Looking at the source code, you should be able to see how it works, and maybe use parts of the code for your own project.

1
  • Thanks for the reply. I will give the plugin a go although a lot of people seem to be having problems with it and will mark it as an answer if I can work with it.
    – sifu
    Nov 13, 2014 at 17:01
0

The easiest and most readable code that worked for me is here => https://webkul.com/blog/menu-item-joomla-rebuild-automatically/

1
  • I don't like the double preg_ calls in the while() loop. I think a single preg_replace_callback() call is cleaner. 3v4l.org/5IBXU Dec 8, 2021 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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