Bryan Seeds

23Mar/100

Custom Magento Module for CMS Page

Today just wanted to discuss how to make a custom Magento module that will allow you to make your own blocks to load into a CMS page using the layout updates.  Really not going to get in depth in the the Models and controllers part of the module creation, going to save that for another time..  anyhow here it is.

First let's create the file to enable the module in magento :

app/etc/modules/{Namespace}_{ModuleName}.xml

<?xml version="1.0"?>
<config>
    <modules>
       <[Namespace_ModuleName]>
           <active>true</active>
           <codePool>local</codePool>
       </[Namespace_ModuleName]>
     </modules>
</config>

Next, we are just going to create a configuration file to define the Block configuration only at this point, for our module :

app/code/local/{Namespace}/{ModuleName}/etc/config.xml

<?xml version="1.0"?>
<config>
<global>
<blocks>
<[namespace]_[modulename]>
<class>[Namespace]_[ModuleName]_Block</class>
</[namespace]_[modulename]>
</blocks>
</global>
</config>

Now, let's go ahead and create our own Block class code to define the object used for our CMS/template page.

app/code/local/{Namespace}/{ModuleName}/Block/MyBlock.php

<?php
 
class <Namespace>_<ModuleName>_Block_MyBlock extends Mage_Core_Block_Abstract
{
 
public function __construct()
{
parent::__construct();
// any instantiation code here
}
 
protected function _toHtml()
{
 
// by overriding this method, you can define here everything you will render to browser, and use the inherited methods and accessors i.e. - $this->get[ParamName]()
 
return $html;
}
 
}

Last thing is to just throw this line into the CMS page content text area to load this custom Magento Block module.

 
{{block type="[namespace]_[modulename]/myblock" param1="value" param2="value"}}

So, that's it in a nutshell.. I will expand on this later for those who need more =)