Question

I'm developing a module for prestashop 1.5.3. I need to create a custom admin tab during the module installation. I make the install like this

public function install()
{
    if( (parent::install() == false)||(!$this->_createTab())  )
        return false;
    return true;
}

And the _createTab method is:

private function _createTab()
{
    $tab = new Tab();
    $tab->id_parent = 7; // Modules tab
    $tab->class_name='AdminWarranty';
    $tab->module='fruitwarranty';
    $tab->name[(int)(Configuration::get('PS_LANG_DEFAULT'))] = $this->l('Warranty');
    $tab->active=1;
        if(!$tab->save()) return false;
return true;
}

And nothing happens.. What am I doing wrong.. and where to find good prestashop developer reference.?

Was it helpful?

Solution

To create a custom tab for a module during installation you can use the following code.

Note: I am considering a test module called News.

private function _createTab()
{
    /* define data array for the tab  */
    $data = array(
                  'id_tab' => '', 
                  'id_parent' => 7, 
                  'class_name' => 'AdminNews', 
                  'module' => 'news', 
                  'position' => 1, 'active' => 1 
                 );

    /* Insert the data to the tab table*/
    $res = Db::getInstance()->insert('tab', $data);

    //Get last insert id from db which will be the new tab id
    $id_tab = Db::getInstance()->Insert_ID();

   //Define tab multi language data
    $data_lang = array(
                     'id_tab' => $id_tab, 
                     'id_lang' => Configuration::get('PS_LANG_DEFAULT'),
                     'name' => 'News'
                     );

    // Now insert the tab lang data
    $res &= Db::getInstance()->insert('tab_lang', $data_lang);

    return true;

} /* End of createTab*/

I hope the above code will help Thanks

OTHER TIPS

Well, I'm myself developing a PrestaShop module so in case someone lands here, the proper way.

For root tabs:

$rootTab = new Tab();
$rootTab->active = 1;
$rootTab->class_name = 'YourAdminControllerName';
$rootTab->name = array();
foreach (Language::getLanguages(true) as $lang) {
    $rootTab->name[$lang['id_lang']] = $this->l("Root tab");
}
$rootTab->id_parent = 0; // No parent
$rootTab->module = $this->name;
$rootTab->add();

Note for version 1.5: When creating a root tab, the system will look for a YourAdminControllerName.gif in your module's folder as the tab icon. Also please note that root tabs don't work as links, despite them requiring a class_name.

For non-root tabs:

$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'YourAdminControllerName';
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
    $tab->name[$lang['id_lang']] = $this->l("Non-root tab");
}
$tab->id_parent = $rootTab->id; // Set the root tab we just created as parent
$tab->module = $this->name;
$tab->add();

If you want to set an existing tab as parent, you can use the getIdFromClassName function. For example, in your case:

$tab->id_parent = (int)Tab::getIdFromClassName('AdminModules');

The add() function returns false if it fails, so you can use it in the if() as you were trying to do with the save() function.

Sadly PrestaShop is by far the worst documented CMS system I've had to work with, and the only way to really code for it is reading code, so I hope it helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top