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

5

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;
}
  • 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 '18 at 18:40
3

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.

  • 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 '14 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/

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.