PrestaShop has done much work to ensure 1.4 version modules backward compatibility with 1.5 version. For example, module configuration is saved automatically based on a selected store. However, if your module contains a Prestashop tab, get ready for trouble.
The thing is, any class, responsible for tab display in a back-office, extended the AdminTab class before. But in PrestaShop 1.5 the approach has changed. There should be an inheritance from the AdminController class. On the other hand, it allowed reaching the separation of logic and presentation. We don’t see echo in the tab class at last! All we need to do now is to use $this->fields_list variable in a constructor to pass an array of grid field values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$this->fields_list = array( 'id_belvg_ddmenu_block' => array( 'title' => $this->l('ID'), 'align' => 'center', 'width' => 30 ), 'title' => array( 'title' => $this->l('Title'), 'width' => 600 ), 'position' => array( 'title' => $this->l('Position'), 'width' => 40, 'align' => 'center', 'position' => 'position', 'search' => FALSE, 'filter_key' => 'position' ), 'active' => array( 'title' => $this->l('Displayed'), 'width' => 40, 'active' => 'status', 'align' => 'center', 'type' => 'bool', 'orderby' => FALSE ), 'standart' => array( 'title' => $this->l('Standart'), 'width' => 40, 'activeVisu' => 'true', 'align' => 'center', 'type' => 'bool', 'orderby' => FALSE ) ); |
$this->fields_form variable is used to display fields in renderForm() method (which is responsible for fields output when editing/adding an object). A lot of default tabs have the «Shop association» parameter which allows associating some kind of data with a concrete store right while creating a new content (or editing an existing one). To output this parameter in your own module, use the piece of code below in the renderForm() method:
1 2 3 4 5 6 7 |
if (Shop::isFeatureActive()) { $this->fields_form['input'][] = array( 'type' => 'shop', 'label' => $this->l('Shop association:'), 'name' => 'checkBoxShopAsso', ); } // displays the «Shop association» parameter |
After the parameter is outputted, reload 2 methods from AdminController in your tab class. These methods are protected function updateAssoShop($id_object) and protected function getSelectedAssoShop($table). Their realization differs from a default one in the fact that we remove Shop::isTableAssociated($this->table) in both classes because Shop::$asso_tables array doesn’t contain our table with the «_shop» postfix. There are no default possibilities to change this array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
protected function updateAssoShop($id_object) { if (!Shop::isFeatureActive()) return; /* if (!Shop::isTableAssociated($this->table)) return; */ $assos_data = $this->getSelectedAssoShop($this->table, $id_object); // Get list of shop id we want to exclude from asso deletion $exclude_ids = $assos_data; foreach (Db::getInstance()->executeS('SELECT id_shop FROM '._DB_PREFIX_.'shop') as $row) if (!$this->context->employee->hasAuthOnShop($row['id_shop'])) $exclude_ids[] = $row['id_shop']; Db::getInstance()->delete($this->table.'_shop', '`'.$this->identifier.'` = '.(int)$id_object.($exclude_ids ? ' AND id_shop NOT IN ('.implode(', ', $exclude_ids).')' : '')); $insert = array(); foreach ($assos_data as $id_shop) $insert[] = array( $this->identifier => $id_object, 'id_shop' => (int)$id_shop, ); return Db::getInstance()->insert($this->table.'_shop', $insert, false, true, Db::INSERT_IGNORE); } protected function getSelectedAssoShop($table) { if (!Shop::isFeatureActive() /* || !Shop::isTableAssociated($table) */) return array(); $shops = Shop::getShops(true, null, true); if (count($shops) == 1 && isset($shops[0])) return array($shops[0], 'shop'); $assos = array(); if (Tools::isSubmit('checkBoxShopAsso_'.$table)) foreach (Tools::getValue('checkBoxShopAsso_'.$table) as $id_shop => $value) $assos[] = (int)$id_shop; else if (Shop::getTotalShops(false) == 1) $assos[] = (int)Shop::getContextShopID(); return $assos; } |
The example given uses a separate table to connect stores and corresponding data for multistore possibilities realization