Page 1 of 1

[SOLVED] How to get layout id / page area for module

Posted: Tue Jun 14, 2011 7:39 pm
by cryoutcreations
Hi.

I'm trying to update my HTML Module to work on OpenCart 1.5.
The module allows me to display 3 different user-defined html code parts. The issue is that I need to be able to tell where the module is displayed on the frontend (in which one of the four top, bottom, left, right areas) and also on what page, so it shows the right part of code for that place (as configured in the admin)
I noticed there are 2 variables used in the Controller class ($template and $layout) but they seem to have no value (or I am unable to use them, since I'm not that accustomed to classes).
Is there any way to get the [instantaneous] values for "template" and "layout" in a module (that is... the module to know where it is currently being displayed)?

Re: How to get layout id / page area a module is displayed i

Posted: Tue Jun 14, 2011 9:23 pm
by cryoutcreations
Okay, I figured out that $template and $layout are for something else :D...

I managed to find a way to get the current layout_id (actually the 'page' id on which the module is displayed), using the following piece of code (from OpenCart itself):

Code: Select all

		if (isset($this->request->get['route'])) { $route = $this->request->get['route']; } else { $route = 'common/home'; }
		$layout_id = 0;
		if (substr($route, 0, 16) == 'product/category' && isset($this->request->get['path'])) {
			$path = explode('_', (string)$this->request->get['path']);
			$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
		}
		if (substr($route, 0, 16) == 'product/product' && isset($this->request->get['product_id'])) {
			$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
		}
		if (substr($route, 0, 16) == 'product/information' && isset($this->request->get['information_id'])) {
			$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
		}
		if (!$layout_id) { $layout_id = $this->model_design_layout->getLayout($route); }
 		if (!$layout_id) { $layout_id = $this->config->get('config_layout_id'); }
but I see no way to figure out on which of the 4 areas it gets displayed ( content_top, content_bottom, content_left, content_right ) :(
This should somehow be included in a variable somewhere... as it is very useful in case a module needs to display different content / format the content differently according to where it is displayed...

Re: How to get layout id / page area a module is displayed i

Posted: Tue Jun 14, 2011 9:32 pm
by ckonig
you could make use of the config class to get the configured position of your module.
for example, the bestseller module stores its position under 'bestseller_position' in the setting table, which can be retrieved using $this->config->get('bestseller_position');

Re: How to get layout id / page area a module is displayed i

Posted: Tue Jun 14, 2011 9:52 pm
by cryoutcreations
Allow me to be a bit more specific:

Lets say the module is configured to display "code 1" in the left_column and "code 2" in the right_column.

When OpenCart builds the website, it reads all the modules configured to be displayed on the left_column, process them then moves on and processes the modules set for right_column.

But my module is configured to appear in both columns, and display separate contents.
This wasn't an issue on OC 1.4 (as it didn't support displaying the same module twice, so I had to use multiple copies of the same module, with separate configurations), but since 1.5 supports layouts and positions, I should be able to do this with one single module.

The same module is displayed twice (or up to 4 times) on the same page. Since the same piece of code is executed for both left_column and right_column, and the database settings are the same for the module no matter where it is being displayed, the module needs to figure out if the current call is in the left_column or the right_column, so it reads and displays the right piece of html from the db.

I see no other module in OpenCart 1.5 uses this ability to display the content differently for separate areas of the page (they all display the same thing no mater where they are placed), and I also see the position-dependant loading of modules is somewhat hardcoded (no variables are being used). This could be an issue and a limitation for how complex the modules could be (as they could never behave differently according to the area of the page in which they are displayed).

Re: How to get layout id / page area a module is displayed i

Posted: Tue Jun 14, 2011 10:13 pm
by cryoutcreations
Never mind... I need to drink more milk so I can figure this stuff out faster :D

Using

Code: Select all

$this->config->get($this->_name . "_" . $module . '_layout_id');
and

Code: Select all

$this->config->get($this->_name . "_" . $module . '_position');
I can get both the layout id and the position (since $module carries the right value for the currently displayed module)