Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Create menu item in Joomla and rebuild Automatically

    Introduction

    If we want to create menu item from your custom component in Joomla as there is no documentation or method in Joomla API, so we have to create it using the database. But when we do it directly we are not able to make complete entries for it as we can’t populate values for lft and rgt fields by using query and logic, and these values are also very important in Joomla for Ordering.
    The process to create menu item from a custom component is to first create a menu type entry and also a corresponding entry in the Assets then we can create the menu items as per Joomla Standards.

    Implementation

    To Create a proper menu item you need to follow the steps:

    **Please note the methods called below are not in any class in this case if you add these methods to a class please use proper instance to call them

    In your code add

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    if (createMenutype('custom', 'Custom') === true) {
        createMenuitem(
            'custom', 'My Custom MenuItem',
            'index.php?option=com_custom&view=custom',
            array( 'component_id' => (int)$component_id)
        );
    }
    // with this code you are done.

    Method’s Defination

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     *
     * CreateMenuitem function
     *
     * @param [type] $menuType   var
     * @param [type] $title      var
     * @param [type] $url        var
     * @param array  $properties var
     * 
     * @return void
     */
    function createMenuitem(
        $menuType, $title, $url, $properties = array()
    ) {
        $alias = trim(
            preg_replace(
                '/_+/', '-', preg_replace(
                    '/\W+/', '', str_replace(
                        ' ', '_', str_replace('_', '', trim(strtolower($title)))
                    )
                )
            )
        );
        $table = JTable::getInstance('Menu');
        while ( $table->load(array('alias' => $alias))) {
            $matches = null;
            if (preg_match('#-(\d+)$#', $alias, $matches)) {
                $alias = preg_replace(
                    '#-(\d+)$#', '-' . ($matches[1] + 1) . '', $alias
                );
            } else {
                $alias .= '-2';
            }
        }
    
        $menu = JTable::getInstance('menu');
        $menu->menutype    = $menuType;
        $menu->title    = $title;
        $menu->alias    = $alias;
        $menu->link     = $url;
        $menu->type     = 'component';
        $menu->language    = '*';
        $menu->published   = 1;
    
        if ($properties) {
            $menu->bind($properties);
        }
    
        $menu->check();
        if (!$menu->store()) {
            return false;
        } else {
            if (!$menu->parent_id) {
                $menu->parent_id = 1;
            }
            if (! $menu->level) {
                $menu->level  = 1;
            }
    
            if (!$menu->store()) {
                return false;
            }
        }
        return true;
    }
    
    
    
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     *
     * CreateMenutype function
     *
     * @param [type] $type        var
     * @param [type] $title       var
     * @param [type] $description var
     * @param array  $properties  var
     * 
     * @return void
     */
    function createMenutype(
        $type, $title, $description = null, $properties = array()
    ) {
        $menuType = JTable::getInstance('MenuType');
        if (!$menuType->load(array('menutype' => $type))) {
            $menuType->menutype  = $type;
            $menuType->title  = $title;
            $menuType->description = $description;
    
            if ($properties) {
                $menuType->bind($properties);
            }
            $menuType->check();
            if (!$menuType->store()) {
                return false;
            }
        } else {
            return null;
        }
        return true;
    }

    Conclusion

    When creating the menu item in Joomla using with this all entries will be filled as per Joomla.

    The corresponding entry for the menutype will be created in the #__assets with the lft and rgt, also you do not need to worry about the lft and rgt value in the #__menuitem as they will be filled when the JTable store() method is called, so you will not require to rebuild the menu items.

    If you find this helpful please like and Share, so others can also find this solution.

    If you have any query please feel free to comment below.

     

    Supported Framework Version - Joomla 3.X

    . . .

    Comments (6)

    Add Your Comment

  • Mairon
    how do I import the files needed to effectively perform the creation of a menu item in joomla, I’m having trouble in that part, thanks!
    • Pratyush Painuly
      There are no such files required that you need to import for this, if you are trying to create menu-items for an extension at the time of installation, then you can add it to your script file and perform this action by implementing method like “postflight” so the menu-items are created only after the installation.

      You can take help from for script file of extension: https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_an_install-uninstall-update_script_file

      If this is not the case please explain so we can guide you in a better way.

      • Mairon
        I’m trying to create just a menu item, in the existing menu in joomla using a php form, it works as follows, there is a form in php for it to automatically create a menu item every time a product is inserted into the site by any seller.
        • Pratyush Painuly
          For this you will need to implement only createMenuitem() in a PHP file(helper will be a good option here), then on successful product creation you can call method with product data as:

          $classObj->createMenuitem(MENU_TYPE, PRODUCT_NAME,
          'index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=PRODUCT_ID',array( 'component_id' => (int)$virtuemart_component_id));

          You will be able to create the menu-item for product using this but need to supply all the required data like this.
          Hope this helps, if you like this blog please like and share.

          • Mairon Piber
            Hello, I still can not create a menu item automatically, every time I make the call to the file containing the createItemmenu function, the joomla returns me an error, is there a specific location to save the php file with the function or something that I need to go in the header? I tried in many different ways and still did not get results, I also tried to put the function in the same file as I call it and still did not succeed.
            I’m using joomla 3.9.8
          • Pratyush Painuly
            Hello, I need to know more details of this process so I can understand your issue in a better way.
            The information on the error you received and how are you calling the function is the important part without which I will not be able you provide you much help.
            Please send your query along with these details on support@webkul.com or raise a ticket on our support, sharing these details in public comments is not advised.
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close

    Table of Content

    Hide Index