user1393876
user1393876

Reputation: 53

Custom Prestashop Admin Module

I am developing a module for prestashop (basically, it's a very custom import of data and the only thing I need is to have a form and process data). I have created controller class derived from the ModuleAdminController but the problem is where should I put the tpl file containing the look of my custom form?

I realize that I can put tpl file to the templates but I want to keep all files within my module folder, is it possible (probably somewhere like "/views/templates/admin")?

Upvotes: 2

Views: 13862

Answers (5)

user1270589
user1270589

Reputation:

This is the most easy method to create a basic admin controller / action in Prestashop 1.6

Create basic configuration :

./config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<module>
    <name>foo</name>
    <displayName><![CDATA[Foo]]></displayName>
    <version><![CDATA[2.1.3]]></version>
    <description><![CDATA[Bar.]]></description>
    <author><![CDATA[your-name]]></author>
    <tab><![CDATA[administration]]></tab>
    <is_configurable>0</is_configurable>
    <need_instance>0</need_instance>
    <limited_countries></limited_countries>
</module>

./foo.php

if (!defined('_PS_VERSION_'))
    exit;

class BarcodeEasyPrint extends Module
{
    public function __construct()
    {
        $this->name = 'foo';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'your-name-here';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Foo');
        $this->description = $this->l('Bar.');

        if ((int)Tools::getValue('p'))
            $this->page = (int)Tools::getValue('p');
    }


}

You need to create the controller with base functions :

./controllers/admin/AdminFooController.php

class AdminFooController extends ModuleAdminController {


    public function __construct()   {
        $this->bootstrap = true;
        parent::__construct();
    }

    public function createTemplate($tpl_name) {
        if (file_exists($this->getTemplatePath() . $tpl_name) && $this->viewAccess())
                return $this->context->smarty->createTemplate($this-    >getTemplatePath() . $tpl_name, $this->context->smarty);
            return parent::createTemplate($tpl_name);
    }

    public function initContent(){
        parent::initContent();
        $tpl = $this->createTemplate('content.tpl')->fetch();

        /* DO STUFF HERE */
        $posts = array();

        $this->context->smarty->assign('posts', $posts);

    }
}

You can use boostrap directly in the template file :

./views/templates/admin/content.tpl

<div class="row">
    <div class="col-md-6">
    </div>

    <div class="col-md-6">
    </div>
</div>

Upvotes: 4

mattimatti
mattimatti

Reputation: 967

extending the answer from @altafhussain create a folder views/templates/admin in your module and place your customview.tpl Than append the free text block as below.

$this->fields_form = array(
    'legend' => array(
        'title' => $this->l('Legend')
    ),
    'input' => array(
          array(
                'type' => 'free',
                'label' => 'Whatever label text',
                'desc' => $this->display(__FILE__,'views/templates/admin/customview.tpl'),
                'name' => 'FREE_TEXT',
                'required' => false
        )

    ),
    'submit' => array(
        'title' => $this->l('Save'),
        'class' => 'button'
    )
);

return parent::renderForm();

}

Upvotes: 0

AlexDeb
AlexDeb

Reputation: 157

You need to use the helper form, here is the documentation for it, it is really easy to use ;) .

http://doc.prestashop.com/display/PS15/HelperForm

You also can find more information about how and where to use helper form, look for the function getContent() and displayForm().

http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module

Upvotes: 0

Altaf Hussain
Altaf Hussain

Reputation: 5202

If it is an admin module only, then you will have no need to create any views. Because Prestashop provides a nice structure for admin section which is easy to use and we dont need to use any views or .tpl files. For admin section, normally three types of views or .tpl files are required, one for data display in grid, second for form and third for displaying a single record.

Prestashop already created .tpl files for them which you can find in "admin_folder/themes/default/templates". In our controllers for admin, for form and for data grid, we just create arrays and PS handles to view the form and data grid according to the arrays we created.

So if you need a custom form at admin, then create a public function renderForm and create the form array in it, like below:

$this->fields_form = array(
        'legend' => array(
            'title' => $this->l('Video'),
            'image' => '../img/admin/tab-genders.gif'
        ),
        'input' => array(
            array(
                'type' => 'text',
                'label' => $this->l('Video Title:'),
                'name' => 'title',
                'lang' => true,
                'size' => 70,
                'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()@#"�{}_$%:',
                'required' => true
            ),

            array(
                'type' => 'textarea',
                'label' => $this->l('Video Code'),
                'name' => 'video_code',
                'rows' => 5,
                'cols' => 70,
                'desc' => $this->l('Place the embed code for the video')
            ),

        ),
        'submit' => array(
            'title' => $this->l('Save'),
            'class' => 'button'
        )
    );

    return parent::renderForm();

} /* End of render member */

For other fields, checkout other prestashop admin controllers and you will see that how easily we can create forms in PS using that simple definitions in the arrays and we dont need to create .tpl files.

For front end, we can use the new modules MVC structure, where our module folder have sub folders for controllers (controllers/front, controllers/admin) , views and models .

Hope this will help you.

Thank you

Upvotes: 3

Related Questions