Reading list Switch to dark mode

    Create Module’s Admin Controllers without creating Tab in prestashop

    Updated 18 January 2017

    
    

    In Prestashop while creating a module mostly we need to create Admin Controllers. In the module, to make an admin controller work we must create an entry of that admin controller’s class in _DB_PREFIX_.’`tab`’ table. And generally, we make all these entries at the time of module installation.

    So if you are creating your admin controllers in your module you can create it with two cases –

    • You want to create a tab for your admin controller.
    • You want to create your admin controller without creating a tab for it. For example your want a controller which opens on click of a link and many other cases may be there.

    Lets understand the process of both case with examples-

    Lets we have created a function with name inatallTab() which makes entries in the ‘tab’ table for our module’s admin controllers.

    // Lets you want to create a child tab under 'Shipping' Tab. As we know Shipping Tab's class name is 'AdminParentShipping'
    $this->installTab('AdminMyControllerName', 'My Tab Name', 'AdminParentShipping');
    
    // Lets you want to create a parent tab. Then call the installTab() like below example-
    $this->installTab('AdminMyControllerName', 'My Parent Tab Name');

    CASE-1 : Admin Controller  with Tab

    Searching for an experienced
    Prestashop Company ?
    Find out More
    /*
    * 2007-2016 PrestaShop
    *
    * NOTICE OF LICENSE
    *
    * This source file is subject to the Open Software License (OSL 3.0)
    * that is bundled with this package in the file LICENSE.txt.
    * It is also available through the world-wide-web at this URL:
    * http://opensource.org/licenses/osl-3.0.php
    * If you did not receive a copy of the license and are unable to
    * obtain it through the world-wide-web, please send an email
    * to [email protected] so we can send you a copy immediately.
    *
    * DISCLAIMER
    *
    * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
    * versions in the future. If you wish to customize PrestaShop for your
    * needs please refer to http://www.prestashop.com for more information.
    *
    *  @author PrestaShop SA <[email protected]>
    *  @copyright  2007-2016 PrestaShop SA
    *  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
    *  International Registered Trademark & Property of PrestaShop SA
    */
    public function installTab($yourControllerClassName, $yourTabName, $tabParentControllerName = false)
    {
        $tab = new Tab();
        $tab->active = 1;
        $tab->class_name = $yourControllerClassName; 
        // e.g. $yourControllerClassName = 'AdminMyControllerName'
        // Here $yourControllerClassName is the name of your controller's Class
    
        $tab->name = array();
    
        foreach (Language::getLanguages(true) as $lang) {
    
            $tab->name[$lang['id_lang']] = $yourTabName;
            // e.g. $yourTabName = 'My Tab Name'
            // Here $yourTabName is the name of your Tab
        }
    
        if ($tab_parent_controller_name) {
    
            $tab->id_parent = (int) Tab::getIdFromClassName($tabParentControllerName);
            // e.g. $tabParentControllerName = 'AdminParentAdminControllerName'
            // Here $tabParentControllerName  is the name of the controller under which Admin Controller's tab you want to put your controller's Tab
    
        } else {
            
            // If you want to make your controller's Tab as parent Tab in this case send id_parent as 0
            $tab->id_parent = 0;
        }
    
        // $this->name is the name of your module to which your admin controller belongs.
        // As we generally create it in module's installation So you can get module's name by $this->name in module's main file
    
        $tab->module = $this->name;
        // e.g. $this->name = 'MyModuleName'
    
        $tab->add();
        // make an entry of your tab in the _DB_PREFIX_.'tab' table.
    }
     Let’s have a look how your Child or Parent tabs are created in your backoffice with the help of the below screenshot.

    CASE-2 : Admin Controller  without creating Tab

    If you want to create your admin controller without creating a tab in your module then you have to make a slight change in the above code which create entries of the your admin controller in the _DB_PREFIX_.’tab’ table .

    We just need to passs -1 for the field id_parent in the code. Lets have a look on the code for this case.

    /*
    * 2007-2016 PrestaShop
    *
    * NOTICE OF LICENSE
    *
    * This source file is subject to the Open Software License (OSL 3.0)
    * that is bundled with this package in the file LICENSE.txt.
    * It is also available through the world-wide-web at this URL:
    * http://opensource.org/licenses/osl-3.0.php
    * If you did not receive a copy of the license and are unable to
    * obtain it through the world-wide-web, please send an email
    * to [email protected] so we can send you a copy immediately.
    *
    * DISCLAIMER
    *
    * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
    * versions in the future. If you wish to customize PrestaShop for your
    * needs please refer to http://www.prestashop.com for more information.
    *
    *  @author PrestaShop SA <[email protected]>
    *  @copyright  2007-2016 PrestaShop SA
    *  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
    *  International Registered Trademark & Property of PrestaShop SA
    */
    $tab = new Tab();
    $tab->active = 1;
    $tab->class_name = $class_name;
    $tab->name = array();
    
    foreach (Language::getLanguages(true) as $lang) {
        $tab->name[$lang['id_lang']] = $tab_name;
    }
    
    //If you don't want to create a tab for your admin controller then Pass id_parent value as -1.
    $tab->id_parent = -1;
    
    $tab->module = $this->name;
    
    return $tab->add();

    So as you have seen if we set the value if  id_parent in the _DB_PREFIX_.’tab’ table as -1 .It will not create any tab for your admin controller and your admin controller will work normally.

    After the above process, you just need to create your admin controller’s class in your module and write code for the functions you need from your admin controller.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home