Loading the contents of a module into a component using Joomla

Posted by Gav on 8 December 2011 | 0 Comments

Tags:

One of the best methods of loading the contents of a module into a component in Joomla is to have another editable content area. You could build this into the component but why bother when there is already a custom HTML module out there.

The first step is to create a module. You have to give the module a name and you also need to assign it to a module position. This module position needs to be a unique name. You can click on the textbox and enter a unique name. I have used mod_publications as the component i'm using is called com_publications.

You will need to also make sure that the module is displayed on all pages.You won't need to worry about the module actually being displayed on all pages as the module position mod_publications does not exist in your template.

The next step is to drop some code yo. This only takes a few lines to get it to work and you will need to edit a view.html.php file and a view template file. The following is an example of the file locations:

  • components/com_publications/views/productdetail/view.html.php
  • components/com_publications/views/productdetail/tmpl/productdetail.php

In the view.html.php file, what we need to do is use the module helper to load the modules for a module position. We have used the mod_publications position. We then assign this to the template using $this->assignRef

view.html.php

<?php

defined ('_JEXEC') or die;

jimport ('joomla.application.component.view');

class PublicationsViewProductDetail extends JView
{
	function display($tmpl = null)
	{
		jimport('joomla.application.module.helper');
		$modules = JModuleHelper::getModules('mod_publications');

		$this->assignRef('modules', $modules);
		parent::display($tmpl);
	}
}

?>

tmpl/productdetail.php

In the tmpl/productdetail.php file, we just need to render the contents of the modules.

<div class="publications-product-modules">
	<?php foreach ($this->modules as $m): ?>
		<?php echo JModuleHelper::renderModule($m); ?>
	<?php endforeach; ?>
</div>

I've only supplied the code required to get this to work.

The benefit of using this approach is that we can load any module. We can have image galleries, forms, etc.


Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments