Prestashop: admin tab with custom data table

Trabla: Prestashop: admin tab with custom data table


Prestashop eCommerce admin tab with custom data table


Solving Overview:

1. Create table ps_super_hero in database

2. Insert tab 'Super Hero' into table ps_tab and ps_tab_lang

3. Create file SuperHero.php  with class SuperHero - model of table ps_super_hero.
Copy file to ...\prestashop\override\classes\  folder

4. Create file AdminSuperHeroController.php with class AdminSuperHero.
Copy file to ...\prestashop\override\controllers\admin\ folder



Solving Details:

1. Create table ps_super_hero in database

CREATE TABLE `ps_super_hero` (
  `id_super_hero` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL UNIQUE COMMENT 'Super Hero Name',
  `motto` varchar(255) NOT NULL UNIQUE COMMENT 'Super Hero Motto',
  PRIMARY KEY (`id_super_hero`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


2. Insert tab 'Super Hero' into table ps_tab and ps_tab_lang

INSERT INTO ps_tab(id_tab,id_parent, class_name,module,position,active,hide_host_mode)
VALUES (2000,0,'AdminSuperHero','',100, 1, 0) ;

INSERT INTO ps_tab_lang( id_tab, id_lang, name) VALUES (2000,1,'Super Heroes');



3. Create file SuperHero.php  with class SuperHero - model of table ps_super_hero.
Copy file to ...\prestashop\override\classes\  folder

 <?php

class SuperHero extends ObjectModel{

  
    public $id_super_hero;
    public $name;
    public $motto;


    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'super_hero',
        'primary' => 'id_super_hero',
        'multilang' => false,
        'fields' => array(
          
            'name' =>         array('type' => self::TYPE_STRING, 'size' => 255),
            'motto' =>         array('type' => self::TYPE_STRING, 'size' => 255),

        ),
    );
}

?>


4. Create file AdminSuperHeroController.php with class AdminSuperHero.
Copy file to ...\prestashop\override\controllers\admin\ folder

<?php

class AdminSuperHeroController extends AdminControllerCore{

    protected $position_identifier = 'id_super_hero';

    public function __construct(){
   

        $this->bootstrap = true;
        $this->className = 'SuperHero';
        $this->table = 'super_hero';
        $this->controller_name = "AdminSuperHero";
       
        $this->addRowAction('edit');
        $this->addRowAction('delete');

        $this->_defaultOrderBy = 'name';
       
        $this->context = Context::getContext();

        $this->fields_list = array(
            'id_super_hero' => array(
                'title' => $this->l('ID'),
                'align' => 'center',
                'class' => 'fixed-width-xs'
            ),
            'name' => array(
                'title' => $this->l('Name')
            ),
            'motto' => array(
                'title' => $this->l('Motto')
            ),
           
        );

        parent::__construct();   

    }

    public function renderForm()
    {
        $this->fields_form = array(
            'legend' => array(
                'title' => $this->l('Super Heroes'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Name'),
                    'name' => 'name',
                    'required' => true
                   
                    ),

                array(
                    'type' => 'text',
                    'label' => $this->l('Motto'),
                    'name' => 'motto',
                    'required' => true
                   
                    ),   

               

            )
           
        );

       
        $this->fields_form['submit'] = array(
            'title' => $this->l('Save'),
        );

       
        return parent::renderForm();
    }

   
}
?>


No comments:

Post a Comment