Post by karlpers » Thu Oct 23, 2014 1:48 am

I'm not a very experienced coder, but have made some extensions etc.

Now, I try to add a simple thing to a module in opencart 2.0. The thing I want to archive is:

1. A multilingual input field in the module admin
2. The multilingual text to be displayed in the header in front end.

There is no need to have multiple modules in the admin panel. There will only be one simple text line.

Also, I really want to avoid using the catalog/controller/common/header.php file.

How would this be done the best way, in opencart 1.5 I could use "$this->config->get" to view the message.

This is how its done:
Admin controller file:

Code: Select all

$data['languages'] = $languages;

$this->load->model('localisation/language');

foreach ($languages as $language) { 
$custom_text = 'message_' . $language['language_id'];
if (isset($this->request->post[$custom_text])) {
$data[$custom_text] = $this->request->post[$custom_text];
} else {
$data[$custom_text] = $this->config->get($custom_text);
}}
Admin view file:

Code: Select all

<?php foreach ($languages as $language) { ?>
<img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>"  /> Message:
<input name="message_<?php echo $language['language_id']; ?>" value="<?php echo isset(${'message_' . $language['language_id']}) ? ${'message_' . $language['language_id']} : ''; ?>"> 
<?php } ?>
In catalog view header.tpl file:

Code: Select all

<?php echo $this->config->get('message_' . $this->config->get('config_language_id')); ?>
What am I doing wrong?

Really thankful if someone would give me some inputs, I guess other amateur developers would like something similar :)

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

Post by uksitebuilder » Thu Oct 23, 2014 1:53 am

It appears to be removed at .tpl level

You can set a variable in you controler though and then access that in your .tpl

i.e. in controller

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

Then in .tpl

If($config_something).......

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by karlpers » Thu Oct 23, 2014 2:16 am

Thanks a lot for your response!

You mean in the admin controller, right?

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

Post by karlpers » Thu Oct 23, 2014 2:18 am

How would you rewrite the new codes to fit my sample code above?

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

Post by uksitebuilder » Thu Oct 23, 2014 2:32 am

If you want to access config vars in header.tpl you need to assign them in the header.php controller

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by rph » Thu Oct 23, 2014 6:47 am

View now has its own loader. Since it's no longer part of the base controller none of the registry objects (e.g. $this->config, $this->currency, $this->language, $this->db) are available in templates. I know this will be an annoyance for some developers but it's really a good thing since it bolsters separation of concerns.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by xseon » Fri Oct 31, 2014 10:58 pm

I have a similar problem with an extension of mine in the catalog part. I can't figure out how to call a module setting variable as $this->config->get('foo') returns nothing.

I have public function index($setting) { ... and here I get the variables with this: $setting['foo'] or $this->config->get('foo').

But then I have another public function test() { ... and here I encounter the problem.

So, how do I call my module setting variable here? The above two do not work.

Deeper and Better Category Module
Mass Product Price Change


User avatar
New member

Posts

Joined
Thu Dec 01, 2011 3:04 pm
Location - Bulgaria

Post by uksitebuilder » Sat Nov 01, 2014 12:52 am

You can call any setting in a controller using $this->config->get('WHATEVER');

So to use in a template, assign that setting to a variable in your controller first

$config_whatever = $this->config->get('WHATEVER');

In your template you would then use: echo $config_whatever

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by karlpers » Mon Nov 03, 2014 9:16 am

Thanks for the response, very thankful.

Im still trying to figure out how to replace this in the new version. For example, if I have a theme where this this->config call is done quite many times, as class names etc. I would like to avoid modify all controller files.

I have seen some older themes, like shoppica, that adds a new class in the index.php. Anyone using the same technique?

An example, you add an option to a theme control panel about how many products to show on each row, 3,4,5 or 6. The value is then added in the ".product-grid" class.

Any way to archive this without edit controller/category.php, manufacturer_info.php, search.php, special.php etc?

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

Post by rph » Tue Nov 04, 2014 11:35 pm

karlpers wrote:I have seen some older themes, like shoppica, that adds a new class in the index.php.
Shoppica is a good illustration of what templates shouldn't be doing. It's a good example on how to break third-party interoperability.

The only real option now is to use vQmod. You can find a pull request in the Github repo which adds the dependency/container registry object back to the view. I would really recommend against doing that but it's up to you.

Hopefully the event system will be expanded before the next release and the issue can be handled cleanly.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by karlpers » Fri Nov 07, 2014 2:52 am

Yeah, I totally agree about shoppica.

Even though shoppica had a brand new framework with half the opencart engine rebuilt. It might be a way to add a class in the index file that only store values from an admin theme control panel, so these values can be used in the catalog/template files in some easy way., without need to edit all catalog/controller files via vqmod

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm
Who is online

Users browsing this forum: No registered users and 7 guests