Page 1 of 1

OC 3.0.x Events System: How to modify twig template file?

Posted: Fri May 24, 2019 8:43 pm
by Jsf
Hello,

OpenCart Events systems (https://github.com/opencart/opencart/wiki/Events-System) looks interesting, but I has a question:
How to edit twig file without ocmod/vqmod? For example I need to replace one line in product_form.twig:

Code: Select all

{% if customer_group.customer_group_id == product_special.customer_group_id %}
to

Code: Select all

{% if customer_group.customer_group_id == product_special.customer_group_id and some_new_variable_equal_true %}
Please give a hint how can I do it.

Re: OC 3.0.x Events System: How to modify twig template file?

Posted: Fri May 24, 2019 10:36 pm
by D3MO
without ocmod you can edit it either in theme editor or directly in ftp no other way

Re: OC 3.0.x Events System: How to modify twig template file?

Posted: Sat May 25, 2019 5:31 am
by diegospm
Jsf wrote:
Fri May 24, 2019 8:43 pm
Hello,

OpenCart Events systems (https://github.com/opencart/opencart/wiki/Events-System) looks interesting, but I has a question:
How to edit twig file without ocmod/vqmod? For example I need to replace one line in product_form.twig:

Code: Select all

{% if customer_group.customer_group_id == product_special.customer_group_id %}
to

Code: Select all

{% if customer_group.customer_group_id == product_special.customer_group_id and some_new_variable_equal_true %}
Please give a hint how can I do it.
Just like @D3MO has specified, to change this line you can do in the OC native theme editor since version 3.0. But I do not recommend doing the editing with FTP or another remote method, because you will have to make backups locally of the files if you want to recover the last version or the original.

If you really need the event, you need to add a controller file and add an event trigger before into the class of that file. In the class that will receive the event you need to change the array key that you want to change in the twig file.

After that, you need to create a twig file that will receive the new render and then edit as needed.

Events are a milestone in the OC, but it's a not-so-clear subject yet and in my point of view, editing the twig file could has edited directly (without going through the controller). But there must be a good reason to be so, the devs/managers of the OC are incredible in designing the tools for platform.

Re: OC 3.0.x Events System: How to modify twig template file?

Posted: Sat May 25, 2019 11:35 am
by grgr
Not sure you can even do that. One way to do it though would be to replace the template product_form.twig with your own template file (my_product_form.twig) with the changes you want in it.

First, add an event; normally do this this by the install function of a module but can be done directly in phpMyadmin
In module:

Code: Select all

        $this->model_setting_event->addEvent('change_product_form', 'admin/view/catalog/product_form/before', 'extension/module/my_module/changeProductFormTemplate');
By phpMyAdmin (change db name)

Code: Select all

INSERT INTO `3032`.`oc_event` (`event_id` ,`code` ,`trigger` ,`action` ,`status` ,`sort_order`) VALUES (NULL , 'change_product_form', 'admin/view/catalog/product_form/before', 'extension/module/my_module/changeProductFormTemplate', '1', '1');
Create the module file ..admin/controller/extension/module/my_module.php and put in the following code:

Code: Select all

<?php
class ControllerExtensionModuleMyModule extends Controller {
/*	
Name: 		change_product_form
Trigger:	admin/view/catalog/product_form/before
Action:		extension/module/my_module/changeProductFormTemplate
Order:		1
Purpose:	Change the product_form.twig template to my_product_form.twig template.
*/
	public function changeProductFormTemplate(&$route, &$data) {
		$route = str_replace('catalog/product_form', 'catalog/my_product_form', $route);
	}
}
Create your new template file: admin/view/template/catalog/my_product_form.twig

Re: OC 3.0.x Events System: How to modify twig template file?

Posted: Sat May 25, 2019 2:48 pm
by Jsf
Thank you for replies. All mentioned methods not very good looking forward because unexpected problems with other modules may occur. Events is good when you need to add something new or replace data array in controller. But as I understand to make small replaces in template files still best way with ocmod.