We have already published several articles about overwriting templates in Prestashop. Whenever working with a live store it is possible to apply changes directly into the template. However, this is not the best way. Using this method, we will not be able to update to new versions because our modified templates will be lost which will cause bugs and errors. The modules have to be developed in such a way, so that the users are able to install them with a single click without modifying any files.
So, today we will describe 2 ways to modify a template in admin panel.
The first example:
For instance, we need to modify the template which is used in the edit product form: adm/themes/default/template/controllers/products/features.tpl
So, we rewrite the function AdminProductsController::initFormFeatures($obj)
And instead of the default template
1 |
$data = $this->createTemplate($this->tpl_form); |
we will use our custom template:
1 2 |
$tpl_path = _PS_MODULE_DIR_ .'belvg_searchpro/views/templates/admin/features.tpl'; $data = $this->context->smarty->createTemplate($tpl_path, $this->context->smarty); |
Quite simple, isn’t it?
The second example:
For example, we use a default Helper to generate forms in the admin panel and we need to generate a custom input which cannot be implemented by using default Prestashop templates.
1 2 3 4 5 6 7 8 |
$obj = $this->loadObject(TRUE); $this->fields_form[0]['form']['input'][] = array( 'type' => 'categories_select', 'label' => $this->l('Children value:'), 'category_tree' => BelvgSearchProFilters::renderParentOnOffFields($obj), //this method generates html 'name' => 'is_parent', 'desc' => $this->l('Turn on if you want using dependent search values in Advanced Search'), ); |
A simple code of the method BelvgSearchProFilters::renderParentOnOffFields($obj)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static function renderParentOnOffFields(FeatureValue $valuesObj) { if (!Module::isEnabled('belvg_searchpro')) { $html = Tools::displayError('Enable BelVG Advanced Search Pro module for using this feature'); } else { Context::getContext()->smarty->assign(array( 'is_parent' => self::getChildByParentId($valuesObj->id) )); $html = self::getModuleInstance()->display(self::getFile(), 'views/templates/admin/features_parent_onoff.tpl'); } return $html; } |
The main specifics of this method is that it uses ‘type’ => ‘categories_select’, which can transfer HTML into the property “category_tree“.
So, these are the methods we use in our modules and we are really interested in the feedback of the community!
Hi Alex,
I’m a bit confused. You mention that modules have to be developed in such a way that user don’t need to modify any files.
But the first example requires that a Prestashop core file is modified, right?
Or is something crucial escaping my attention?
Cheers,
Menno
i have get error Fatal error: Uncaught –> Smarty: Unable to load template file
Okey, so I should create folder: C:\wamp\www\ps\modules\mymodule\override\controllers\admin\templates\orders and put the _product_line.tpl file here? I have done it and after install any changes. If I check prestashop override files I even can`t see this file. Why? Should I write additional function to use it (override)? Even if I do it manuall – no effect.
ml,
If you need to change the template not inside the module it is better to to copy this file to override folder.
Hey,
what about if I would like to override admin/templates/orders/_product_line.tpl ?
Cyd example is not working. I just want to add a few html rows to _product_line.
Cyd,
Thank you for your comment, this new way is really great! But if we are talking about the module, the standard install process will not move tpl files into the override directory, and therefore it is possible that some may still need to use the “old” way.
Hi Alex,
Regarding your first example, there is even a simpler way to do it now in PS 1.6.
You simply use the /override/controllers/admin/templates/ directory.
In your case, you would put your new features.tpl file in the /override/controllers/admin/templates/products/ directory. Et voilà!
But thanks for the tips anyway.
Best regards.