0
votes

I would like to add a variable/display in common/header twig file, which can be managed from an new extension. The new extension is created. starter_module

I added in: admin/view/template/extension/module/starter_module.twig

<div class="form-group">
        <label class="col-sm-2 control-label" for="input-new">New</label>
        <div class="col-sm-10">
        <select name="new" id="input-new" class="form-control">
            {% if new %}
            <option value="1" selected="selected">Enabled</option>
            <option value="0">Disabled</option>
            {% else %}
            <option value="1">Enabled</option>
            <option value="0" selected="selected">Disabled</option>
            {% endif %}
        </select>
        </div>
</div>

in admin/controller/extension/module/starter_module.php

if (isset($this->request->post['new'])) {
  $data['new'] = $this->request->post['new'];
} elseif (!empty($module_info)) {
  $data['new'] = $module_info['new'];
} else {
  $data['new'] = '';
}

in catalog/controller/extension/module/starter_module.php

            $data['new'] = $this->config->get('new');

            $data['new'] = (int) $setting['new'];  

in catalog/view/theme/default/template/common/header.twig

{% if new %}Enabled {% else %} disabled{% endif %}

But always I got the result only disabled, what is missing? cannot be sent variable from extension to common header?

Please, help me if you know the issue, the non working files are here https://github.com/bblori/Opencart3

You can see here one of my working variable which was set in setting/setting files and is working.

https://github.com/bblori/Enable-Style-OC3

XML code

<modification>
<name>Starter Module</name>
<code>starter-module</code>
<version>1.0.0</version>
<author>Author</author>
<link>http://domain/</link>
    <file path="catalog/controller/common/header.php">
        <operation>
            <search><![CDATA[return $this->load->view('common/header', $data);]]></search>
            <add position="before"><![CDATA[
                    $data['config_new'] = $this->config->get('config_new'); 
           ]]></add>
        </operation>
    </file>

Many thanks

2
Solution: I created a xml file in vqmod, where I added the code to insert into controller/header.bblori

2 Answers

0
votes

move your code from your starter module to header.php

$data['new'] = $this->config->get('new');
$data['new'] = (int) $setting['new']; 
0
votes

Neither editing core files or using vqmod is acceptable. Core files should not be modified, because later updates would make your modifications obsolete. Vqmod on the other hand adds an unnecessary complexity to a system well designed.

Since version 3 Opencart team have introduced events. Events are a new way of executing custom functionality when it's needed. Next time your have a similar problem add event (either manually or during installation of your module as shown below).

public function install() {
    $this->load->model('setting/event');
    $this->model_setting_event->addEvent('my_data_manager', 'catalog/view/*/before', 'extension/module/my_data_manager/beforeAll');
}

Later during execution cycle your function will be called automatically every time common/header is being rendered.

class ControllerExtensionModuleMyDataManager extends Controller {

    public function beforeAll(&$route, &$data, &$output){
        if ($route == 'common/header') {
            $data['my_custom_data'] = 'Mickey Mouse is not a bird!';
        }
    }
}

Finally, add {{ my_custom_data }} to template/common/header.twig