Make custom modules by patching mod_custom and using chromes

Your code modifications and patches you want to share with others.
Locked
tobiasn
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Tue Feb 27, 2007 6:34 pm

Make custom modules by patching mod_custom and using chromes

Post by tobiasn » Tue Jun 16, 2009 9:50 pm

There's been some interest about this thread: http://forum.joomla.org/viewtopic.php?f=178&t=320017, where I explore how you fairly easily can extend the content class with your own parameters and put them to good use.

So we thought it would be easy to do this with our beloved mod_custom as well. And yeah, it's basically the same. It's a breeze.

Get down and dirty:

Find your modules/mod_custom/mod_custom.xml. Here you can add params as laid out in this nice article:
http://docs.joomla.org/Tutorial:Template_parameters
YES, template parameter types are applicable to other XML param blocks.

In a recent project, we needed three fairly basic modules. One to display speed, price, etc on a broadband offer, and two custom banner-type modules. All needed to be easy to update for the customer. So my the <param> block in my mod_custom.xml looked like this:

Code: Select all

		<params>
			<param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="PARAMMODULECLASSSUFFIX" />
			<param type="spacer" default="<b> Pricing params </b>" />
			<param name="pris" type="text" default="" label="Pris" description="Pris" />
			<param name="type" type="text" default="" label="Type" description="Type" />
			<param name="speed" type="text" default="" label="Speed" description="Speed" />
			<param type="spacer" default="<b> Params for Box 1 </b>" />
			<param name="big_header" type="text" default="" label="Headline" description="Box Headline" />
			<param name="big_link" type="menuitem" default="45" label="Link" description="Link to menu item" />
			<param name="bg_image_big" type="imagelist" default="" label="Background image" description="Choose bg-img" directory="templates/beez/images" exclude="" stripext="" />
			<param type="spacer" default="<b> Params for Box 2 </b>" />
			<param name="small_header" type="text" default="" label="Headline" description="Box Headline" />
			<param name="small_link" type="menuitem" default="45" label="Link" description="Link to menu item" />
		</params>


Now, I'm not going to go into how to choose/implement module chromes, other than to link to this:
Chrome for dummies: http://docs.joomla.org/What_is_module_chrome%3F
Applying custom chromes: http://docs.joomla.org/Applying_custom_module_chrome

Basically, in your template's html folder you've got the modules.php, that you need to get to know intimately.

So far, we've developed three chromes to put these params to use. My html/modules.php looks like this:

Code: Select all

function modChrome_Priser($module, &$params, &$attribs)
{
		?>
		<div class="olesmod">
        	<div class="prisboble"><div class="text">M&aring;nedspris</div>
			<div class="pris"><?php echo $params->get('pris'); ?>,-</div>
            </div>
            <div class="prispakkebox">
            <div class="type"><?php echo $params->get('type'); ?></div>
            <div class="speed"><?php echo $params->get('speed'); ?></div>
            <div class="infotxt"><?php echo $module->content; ?></div><div class="clear"></div>
            </div>
		</div>
	<?php 
}
function modChrome_bigBox($module, &$params, &$attribs)
{
	// Get link
	$menu = &JSite::getMenu();
	$menuObject = $menu->getItem($params->get('big_link'));
	$link = $menuObject->link ."&Itemid=".$params->get('big_link');
		?>
		<div class="blokk1" <?php if ($params->get('bg_image_big')){echo 'style="background: url('. JURI::base( true ) .'/templates/beez/images/'. $params->get('bg_image_big') .') no-repeat"';} ?>>
        <h1>
        <?php if ($params->get('big_link')){echo '<a href="'. $link .'">';} ?>
        <?php if ($params->get('big_header')){echo $params->get('big_header');} else {echo "Husk &aring; skrive inn overskrift i modulen";} ?>
        <?php if ($params->get('big_link')){echo '</a>';} ?>
        </h1>
        <?php echo $module->content; ?>
        </div>
	<?php 
}
function modChrome_smallBox($module, &$params, &$attribs)
{
	// Get link
	$menu = &JSite::getMenu();
	$menuObject = $menu->getItem($params->get('small_link'));
	$link = $menuObject->link ."&Itemid=".$params->get('small_link');
		?>
        <div class="blokk3">
        <h3><?php echo $params->get('small_header') ?></h3>
        <?php echo $module->content; ?>
        <a href="<?php echo $link ?>" title="<?php echo strip_tags($params->get('small_header')) ?>" class="smallLink"><img src="images/stories/bilder/pil_1.gif" class="pil_1" /></a>
        </div>
	<?php 
}
What you probably and hopefully can see is this:
1. We're not really that adept at coding, so you can do this too
2. We're not really that picky when it comes to php standards
3. We're Norwegian and I didn't translate everything
4. This is easy.

Some explaining:

If you study the XML, we use a type="menuitem" for one of the parameters, to easily link to a menu item (doh). But this only gets us the Itemid - we need some Joomla API wizardry to get the actual link. The code abobe works fine

We use JURI::base( true ) when we style the background images in the big_box so that it's bulletproof when (if) we move the site from a subdir on the testing server to production.

Some of you have stopped reading and started coding. Good for you! Some of you probably need explaining. I'll try to do that. Shoot. :)

loke
Joomla! Apprentice
Joomla! Apprentice
Posts: 40
Joined: Mon Jul 09, 2007 7:20 am
Location: Thisted

Re: Make custom modules by patching mod_custom and using chromes

Post by loke » Sat Nov 14, 2009 10:59 am

Hej Tobiassn

Can you explain this more simply because I am a newbee.
I would like to see the code for only one background image.
I get confused by all the code in the wizard.

på forhånd tak loke...

User avatar
joomfriend
Joomla! Explorer
Joomla! Explorer
Posts: 277
Joined: Sun Feb 08, 2009 5:10 pm
Contact:

Re: Make custom modules by patching mod_custom and using chromes

Post by joomfriend » Mon Dec 07, 2009 5:43 am

Well, it will be difficult to explain it more simply since this is for advanced users. You will first need to understand how templates work.

Please read:
http://docs.joomla.org/Tutorial:Template_parameters
http://docs.joomla.org/What_is_module_chrome%3F
http://docs.joomla.org/Applying_custom_module_chrome

It might be a good starting point.
- https://www.adelnipet.com: Adelni Pet - Your Social Pet Network
- https://www.egliseprimitive.org: Christian Website

tez
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 248
Joined: Tue Nov 14, 2006 3:29 am
Contact:

Re: Make custom modules by patching mod_custom and using chromes

Post by tez » Tue Mar 23, 2010 1:00 pm

I'd like to know how to make a new module which uses the same editor as mod_custom. Basically clone mod_custom as my own module so I can add parameters like "Exclude ItemID" and have a HTML module which will show everywhere EXCEPT pages specified (I can do that part easy, just can't work out how to clone the mod_custom and retain the editor).


Locked

Return to “Core Hacks and Patches”