Prestashop 的back office tab 筆記 - part 2

上一份筆記寫下了db object 後, 這一篇的筆記就來掛上back-office 的頁面作出設定吧...

好首先就建立一個最基本的admin page, :
<?php
    class AdminTest extends AdminTab {
        public function __construct() {
            $this->table = 'test';
            $this->className = 'Test';
            $this->lang = true;
            $this->edit = true;  
            $this->delete = true;
            $this->fieldsDisplay = array(
              'id_test' => array(
                'title' => $this->l('ID'),
                'align' => 'center',
                'width' => 25),
              'name' => array(
                'title' => $this->l('Name'),
                'width' => 200),
              'description' => array(
                    'title' => $this->l('description'),
                    'width' => 510)          
            );

            $this->identifier = 'id_test';

            parent::__construct();
        }
  }
?> 

以上的code 掛上back-office後能出現以下的頁面
頁面
adminpage 也和db object class相類似, 也是需要去extends 一個基本的class - AdminTab, 之後就可以overwrite 它的 __construct來為版面作出設定..

$this->table = 'test';
$this->className = 'test';
$this->lang = true;

這裏的table是指你要為DB裏那一個table作出設定,所以就於入上次所建立的`test`, 同樣地是不用加上prefix的, 而className 則是這個table所對應的db object class, 所以填上上次所建立的Test, 由於我們當中有Field 是支援多語言的, 所以這個lang就要設定成true了..
注意: 如果沒有了_lang 的table, 但把lang設定為true 是會出現bad sql query 的錯誤!

我們可以設定不同的action, 只要把對應的變數設定係true 便可了

設定好需要顯示的fields
$this->fieldsDisplay = array(
  'id_test' => array(
    'title' => $this->l('ID'),
    'align' => 'center',
    'width' => 25),
  'name' => array(
    'title' => $this->l('Name'),
    'width' => 200),
  'description' => array(
    'title' => $this->l('description'),
    'width' => 510)           
); 

設定後的頁面
再set 番個table key 入去個identifier變數裏. $this->identifier = 'id_test';
最後因為你overwrite 了__construct , 所以當然不要忘記call 番parent 呢...
parent::__construct();

忘記了call 番parent 的__construct 可是會出現error 的呢..
這樣就可以有最基本的admin page了, 不過這時按下add new/edit 只會出現白頁,如何為頁面加入簡單的form 就留待下一篇筆記先再寫了

0 回應:

Post a Comment