'SECURITY TOKEN in first Prestashop 1.7 module
I have tryed now for a few day´s to create a module. Its going to be a easy one, that add some variable to different table, and show them in a list when you select from a dropdown. Im only still in admin, the front do i create later,
I follow a tutorials that was easy to follow. First copy the sql to the db.
CREATE TABLE pasta (
`id` INT NOT NULL AUTO_INCREMENT,
`sku` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`description` TEXT,
`id_pasta_category` INT NOT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
Then i copy the ObjectModel class /override/classes/fc_pasta/Pasta.php
<?php
class Pasta extends ObjectModel {
public $id; // fields are mandatory for create/update
public $sku;
public $name;
public $created;
public $category;
public $id_pasta_category;
public static $definition = [
'table' => 'pasta',
'primary' => 'id',
'fields' => [
'sku' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
'name' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
'description' => ['type' => self::TYPE_HTML, 'validate' => 'isAnything',],
'created' => ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
'id_pasta_category' => ['type'=>self::TYPE_INT, 'validate'=>'isUnsignedInt','required'=>true,],
],
];
}
And after that i copy the module /modules/fc_pasta/fc_pasta.php
<?php
if (!defined('_PS_VERSION_')) {exit;}
class Fc_Pasta extends Module {
public function __construct() {
$this->name = 'fc_pasta'; // must match folder & file name
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Florian Courgey';
$this->bootstrap = true; // use Bootstrap CSS
parent::__construct();
$this->displayName = $this->l('PrestaShop Module by FC');
$this->description = $this->l('Improve your store by [...]');
$this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
// install Tab to register AdminController in the database
$tab = new Tab();
$tab->class_name = 'AdminPasta';
$tab->module = $this->name;
$tab->id_parent = (int)Tab::getIdFromClassName('DEFAULT');
$tab->icon = 'settings_applications';
$languages = Language::getLanguages();
foreach ($languages as $lang) {
$tab->name[$lang['id_lang']] = $this->l('FC Pasta Admin controller');
}
$tab->save();
}
}
And after that i create the AdminPastaController
<?php
require_once _PS_ROOT_DIR_.'/override/classes/fc_pasta/Pasta.php';
class AdminPastaController extends ModuleAdminController {
public function __construct(){
parent::__construct();
// Base
$this->bootstrap = true; // use Bootstrap CSS
$this->table = 'pasta'; // SQL table name, will be prefixed with _DB_PREFIX_
$this->identifier = 'id'; // SQL column to be used as primary key
$this->className = 'Pasta'; // PHP class name
$this->allow_export = true; // allow export in CSV, XLS..
// List records
$this->_defaultOrderBy = 'a.sku'; // the table alias is always `a`
$this->_defaultOrderWay = 'ASC';
$this->_select = 'a.name as `pastaName`, cl.name as `categoryName`';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'category` cat ON (cat.id_category=a.id_pasta_category)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cat.id_category=cl.id_category and cat.id_shop_default=cl.id_shop)';
$this->fields_list = [
'id' => ['title' => 'ID','class' => 'fixed-width-xs'],
'sku' => ['title' => 'SKU'],
'pastaName' => ['title' => 'Name', 'filter_key'=>'a!name'], // filter_key mandatory because "name" is ambiguous for SQL
'categoryName' => ['title' => 'Category', 'filter_key'=>'cl!name'], // filter_key mandatory because JOIN
'created' => ['title' => 'Created','type'=>'datetime'],
];
// Read & update record
$this->addRowAction('details');
$this->addRowAction('edit');
$categories = Category::getCategories($this->context->language->id, $active=true, $order=false); // [0=>[id_category=>X,name=>Y]..]
$categories = [['id'=>1, 'display'=> 'abc'], ['id'=>2, 'display'=>'def']];
$this->fields_form = [
'legend' => [
'title' => 'Pasta',
'icon' => 'icon-list-ul'
],
'input' => [
['type'=>'html','html_content'=>'<div class="alert alert-info">Put here any info content</div>'],
['name'=>'id_xxx','label'=>'XXX','type'=>'select',
'options'=>[ 'query'=>$categories,
'id'=>'id', // use the key id as the <option> value
'name'=> 'display', // use the key display as the <option> title
]
],
['name'=>'name','type'=>'text','label'=>'Name','required'=>true],
['name'=>'description','type'=>'textarea','label'=>'Description',],
['name'=>'created','type'=>'datetime','label'=>'Created',],
['name'=>'id_pasta_category','label'=>'Category','type'=>'select','required'=>true,'class'=>'select2',
'options'=>[ 'query'=>$categories,
'id'=>'id_category', // use the key "id_category" as the <option> value
'name'=> 'name', // use the key "name" as the <option> title
]],
],
'submit' => [
'title' => $this->trans('Save', [], 'Admin.Actions'),
]
];
}
protected function getFromClause() {
return str_replace(_DB_PREFIX_, '', parent::getFromClause());
}
}
Everything almost ok, but everytime i update the page it create a new menu all the time: FC Pasta Admin controller, instead of 1 i have 25 now.
That I can´t find why.
And one more thing, everything i do in the module i get INVALID SECURITY TOKEN
Im really new to create modules to PS 1.7, but i really whant to try.
Solution 1:[1]
You are creating and saving a new backoffice tab in the module constructor, so a new tab will be created everytime the module object is instantiated.
You'll need to move the tab creation login in the install() method of the module, so that your tab will be created only once during the very first module installation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user3256843 |
