Part of customizing Magento is, of course, creating custom Modules. These allow you to inject functionality anywhere, whether in a “static” block fashion that’s more than static, or a shipping/payment module, or large module to do something as large as integrating a 3rd party system (or multiple systems).
There are many things custom Modules can do, from editing your Database, to handling module upgrades to overriding classes (Blocks, Controllers, Models) … and more!
This blog post is a very basic start on creating your own custom module and hooking it up to a phtml file in your own theme.
I will be creating a Flexsin Example module. ‘Flexsin’ is the namespace (vs Mage), ‘Example’ is the module (vs Catalog, Page, Core, Shipping etc).
Step One
Inform Magento that you have a custom module.
app/etc/modules/Flexsin_All.xml Notice the _All in the xml file name. I can declare all of my modules here. (Say I have more than Example, I can also declare my Example2 module in this file).
<?xml version="1.0"?>
<config>
<modules>
<Flexsin_Example>
<active>true</active>
<codePool>local</codePool>
</Flexsin_Example>
</modules>
</config>
I have informed Magento that I have an active module (you can turn it off from here by setting ‘active’ to false. I have also informed Magento that it is located in the ‘local’ code pool.
Step Two
Configure your new module. Note the file locations (need to create directories as necessary).
app/code/local/Flexsin/Example/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Flexsin_Example>
<version>0.1.0</version>
</flexsin_Example>
</modules>
<global>
<blocks>
<flexsin_example>
<class>Flexsin_Example_Block</class>
</Flexsin_example>
</blocks>
</global>
</config>
I have informed Magento of my module version (it’s an arbitrary version). Version matters when you set up your module to be update-able. (A newer version will inform Magento to run the update files if you have them).
I have also informed Magento that my module contains block files which are found in flexsin/example/block. My class name will have “Flexsin_Example_Block”. If you want to see the many possibilities of stuff that goes in here, check out Mage config files (such as Catalog/etc/config.xml). You’ll also see other xml files in there. Those explanations are for another post.
Step Three
Here is my block code. It doesn’t really do anything, but shows some functionality.
app\code\local\Flexsin\Example\Block\View.php
<?php
/**
* Example View block
*
* @codepool Local
* @category Flexsin
* @package Flexsin_Example
* @module Example
*/
class Flexsin_Example_Block_View extends Mage_Core_Block_Template
{
private $message;
private $att;
protected function createMessage($msg) {
$this->message = $msg;
}
public function receiveMessage() {
if($this->message != '') {
return $this->message;
} else {
$this->createMessage('Hello World');
return $this->message;
}
}
protected function _toHtml() {
$html = parent::_toHtml();
if($this->att = $this->getMyCustom() && $this->getMyCustom() != '') {
$html .= '<br />'.$this->att;
} else {
$html .= '<br />No Custom Attribute Found';
}
return $html;
}
}
The function receiveMessage() just returns “Hello World”
The function _toHtml() is responsible for outputting the template associated with the block. Make sure you run paret::_toHtml and return it as part of any other items returned in that function!
Step Four
Here we create our template (phtml) file.
app\design\frontend\default\flexsin\template\example\view.phtml
<?php
/**
* Flexsin view template
*
* @see Flexsin_Example_Block_View
*
*/
?>
<div>
<span><strong>This is the output of the Flexsin example:</strong></span><br />
<span style="color:#FF9933;">
<?php
echo $this->receiveMessage();
?>
</span>
</div>
This just outputs some HTML and also runs the receiveMessage() function from our block (view.php).
Two caveats here. By placing our view.phtml file in it’s location, we have created our own theme. You must make sure that
a) Magento knows about your theme (Admin->System->Design)
and
b) If you use the this block in a CMS page, you set the CMS page to use your theme (Admin->CMS->Manage Pages->’Your Page’->Custom Design->Custom Theme drop down)
You’re custom module is now ready for use.
In a cms page, add this to your content:
{{block type="flexsin_example/view" my_custom="Test" template="example/view.phtml" }}
Or something like this in the Layout Update XML area (Custom Design area in a CMS page)
<reference name="right">
<block type="flexsin_example/view" my_custom="Test" template="example/view.phtml" />
</reference>
//this will add your block in the right column
Now you should successfully have your block and the Hello World message being displayed (on your CMS page).
There are many things custom Modules can do, from editing your Database, to handling module upgrades to overriding classes (Blocks, Controllers, Models) … and more!
This blog post is a very basic start on creating your own custom module and hooking it up to a phtml file in your own theme.
I will be creating a Flexsin Example module. ‘Flexsin’ is the namespace (vs Mage), ‘Example’ is the module (vs Catalog, Page, Core, Shipping etc).
Step One
Inform Magento that you have a custom module.
app/etc/modules/Flexsin_All.xml Notice the _All in the xml file name. I can declare all of my modules here. (Say I have more than Example, I can also declare my Example2 module in this file).
<?xml version="1.0"?>
<config>
<modules>
<Flexsin_Example>
<active>true</active>
<codePool>local</codePool>
</Flexsin_Example>
</modules>
</config>
I have informed Magento that I have an active module (you can turn it off from here by setting ‘active’ to false. I have also informed Magento that it is located in the ‘local’ code pool.
Step Two
Configure your new module. Note the file locations (need to create directories as necessary).
app/code/local/Flexsin/Example/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Flexsin_Example>
<version>0.1.0</version>
</flexsin_Example>
</modules>
<global>
<blocks>
<flexsin_example>
<class>Flexsin_Example_Block</class>
</Flexsin_example>
</blocks>
</global>
</config>
I have informed Magento of my module version (it’s an arbitrary version). Version matters when you set up your module to be update-able. (A newer version will inform Magento to run the update files if you have them).
I have also informed Magento that my module contains block files which are found in flexsin/example/block. My class name will have “Flexsin_Example_Block”. If you want to see the many possibilities of stuff that goes in here, check out Mage config files (such as Catalog/etc/config.xml). You’ll also see other xml files in there. Those explanations are for another post.
Step Three
Here is my block code. It doesn’t really do anything, but shows some functionality.
app\code\local\Flexsin\Example\Block\View.php
<?php
/**
* Example View block
*
* @codepool Local
* @category Flexsin
* @package Flexsin_Example
* @module Example
*/
class Flexsin_Example_Block_View extends Mage_Core_Block_Template
{
private $message;
private $att;
protected function createMessage($msg) {
$this->message = $msg;
}
public function receiveMessage() {
if($this->message != '') {
return $this->message;
} else {
$this->createMessage('Hello World');
return $this->message;
}
}
protected function _toHtml() {
$html = parent::_toHtml();
if($this->att = $this->getMyCustom() && $this->getMyCustom() != '') {
$html .= '<br />'.$this->att;
} else {
$html .= '<br />No Custom Attribute Found';
}
return $html;
}
}
The function receiveMessage() just returns “Hello World”
The function _toHtml() is responsible for outputting the template associated with the block. Make sure you run paret::_toHtml and return it as part of any other items returned in that function!
Step Four
Here we create our template (phtml) file.
app\design\frontend\default\flexsin\template\example\view.phtml
<?php
/**
* Flexsin view template
*
* @see Flexsin_Example_Block_View
*
*/
?>
<div>
<span><strong>This is the output of the Flexsin example:</strong></span><br />
<span style="color:#FF9933;">
<?php
echo $this->receiveMessage();
?>
</span>
</div>
This just outputs some HTML and also runs the receiveMessage() function from our block (view.php).
Two caveats here. By placing our view.phtml file in it’s location, we have created our own theme. You must make sure that
a) Magento knows about your theme (Admin->System->Design)
and
b) If you use the this block in a CMS page, you set the CMS page to use your theme (Admin->CMS->Manage Pages->’Your Page’->Custom Design->Custom Theme drop down)
You’re custom module is now ready for use.
In a cms page, add this to your content:
{{block type="flexsin_example/view" my_custom="Test" template="example/view.phtml" }}
Or something like this in the Layout Update XML area (Custom Design area in a CMS page)
<reference name="right">
<block type="flexsin_example/view" my_custom="Test" template="example/view.phtml" />
</reference>
//this will add your block in the right column
Now you should successfully have your block and the Hello World message being displayed (on your CMS page).
No comments:
Post a Comment