Create Menu Item Dynamically

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Locked
jonBuckner1
Joomla! Explorer
Joomla! Explorer
Posts: 428
Joined: Mon Nov 12, 2012 9:08 am

Create Menu Item Dynamically

Post by jonBuckner1 » Mon Dec 14, 2015 3:58 pm

Hi folks

I have rewritten this post as I have the answer....

Question
How do you dynamically create a menu item and have the lft and rgt values set correctly?

Answer
Read jamesgarrett's post on StackExchange, Programmatically Add Menu Item in Component:
http://joomla.stackexchange.com/questio ... -component
Thank you VERY much James.

Code: Select all

$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;
}

ORIGINAL POST:
I am creating menu items dynamically using seblod and a field called aftersore...

Can someone assist with how to assign correct 'lft' and 'rgt' values?

Thank you VERY much for any help you can provide


Code: Select all

menuItem = new stdClass();

// $menuItem->id = ''; // created with Seblod
$menuItem->menutype = 'my-menu-type';
$menuItem->title = 'My Dynamic Title';
$menuItem->alias = 'my_dynamic_title';
$menuItem->note = '';
$menuItem->path = 'my_dynamic_title';
$menuItem->link = 'index.php?option=com_content&view=categories&id=' . $dynamicCatId;
$menuItem->type = 'component';
$menuItem->published = '1';
$menuItem->parent_id = '1';
$menuItem->level = '1';
$menuItem->component_id = '22';
$menuItem->checked_out = '0';
$menuItem->checked_out_time = '';
$menuItem->browserNav = '0';
$menuItem->access = '1';
$menuItem->img = '';
$menuItem->template_style_id = '0';
$menuItem->params = '';
$menuItem->lft = '';
$menuItem->rgt = '';
$menuItem->home = '0';
$menuItem->language = '*';
$menuItem->client_id = '0';

// Insert the object into the menu table.
$result = JFactory::getDbo()->insertObject('#__menu', $menuItem);

jonBuckner1
Joomla! Explorer
Joomla! Explorer
Posts: 428
Joined: Mon Nov 12, 2012 9:08 am

Re: Create Menu Item Dynamically

Post by jonBuckner1 » Wed Feb 03, 2016 2:52 pm

Hi

As an additional...
How to create multiple menu items at once
(this is my solution based on other posts and stuff ie http://p-o.co.uk/tech-articles/automati ... d%20others).

Create an array for each menu item that you need, and add the columns that you need but ensure that columns 'lft' and 'rgt' are NOT represented ie

Code: Select all

$menuItem1 = array(
                // 'id' = ''; // Automatic
                'menutype' => $menuType1, // Required
                'title' => $menuTitle1, // Required
                'alias' => etc...
                // 'lft' => , 
                // 'rgt' => ,
                'home' => 0,
                'language' => '*', 
                'client_id' => 1 
              );

$menuItem1 = array(
                // 'id' = ''; // Automatic
                'menutype' => $menuType2, // Required
                'title' => $menuTitle2, // Required
                'alias' => etc...
                // 'lft' => , 
                // 'rgt' => ,
                'home' => 0,
                'language' => '*', 
                'client_id' => 1 
              );
When you have created all of your menu item arrays add a for loop. In this for loop add a switch statement

Code: Select all


$menuItemQty = 4; // make this value whatever you need it to be

for ($i = 0; $i < $menuItemQty; $i++)
{
    $menuTable = JTableNested::getInstance('Menu');

    // assign the menulink array
    switch ($i) 
    {
        case 0:
            $menuData =  $menuData0;
            break;

        case 1:
            $menuData =  $menuData1;
            break;

        case 2:
            $menuData =  $menuData2;
            break;

        case 3:
            $menuData =  $menuData3;
            break;
    };
    // END switch

$parentId = 1; //  whatever you need it to be
    $menuTable->setLocation($parentId, 'last-child');
    
    if (!$menuTable->save($menuData))
    {
        $this->setError($menuTable->getError());
        return false;
    }
    // Menu created




I have slightly adjusted the code that I used to make it more readable as an example.
It works and it took me a loooooong time to get there
Hope i helps someone out there


Jon


Locked

Return to “Joomla! 3.x Coding”