I really like the eclipse platform. But yesterday morning it was my own WTF moment when my IDE crashed while building a large project.
Monthly Archives: June 2010
Override controllers in Magento
- Tue, 29 Jun 2010 AT 10:43:45 UTC
- with 2 comments
The last time i had to rewrite one of the core Magento blocks. And that turned out to be quite easy. My new challenge was to rewrite / overwrite one of the Core admin controllers. In this case the Mage_Adminhtml_CustomerController.
I had no idea where to start so i did some googling beforehand. And came to the conclusion that a lot of people are trying to do this. Because of this there is a long list of solutions to solve this. And in most cases developers just advice to drop the Core class in the local folder and work from there. But that doesn’t really make sense. First of all it code duplication in the worst form. And besides that all cool OO stuff like extending is gone.
And since Magento is such a flexible system. There must be a good clean way to solve this. One thread i found had some good info. And i used it as my starting point. There was one problem with this thread. The XML used seems to be an old form of rewriting in Magento.
I found a (lost it) post by one of the Magento devs that shows a new and more clean way of defining this in XML.
In the last post i already created the [Namespace]_All.xml module config. And we can use it for this as well. So we start by telling Magento we are using our own [Namespace_]_Adminhtml module.
<[namespace]_Adminhtml>
true
local
[namespace]_Adminhtml>
create local/Namespace/Adminhtml/controllers/CustomerController.php
The next thing to do is create the actual controller. And add an include line on top to the original file. Magento does not use autoloading for the controllers in this way.
include_once("Mage/Adminhtml/controllers/CustomerController.php");
class [namespace]_Adminhtml_CustomerController extends Mage_Adminhtml_CustomerController
{
public function somenewAction()
{
}
}
Now the Core CustomerController is replaced by our own namespaced version. But it still retains all functionality of the parent.
The only thing left is to create the config file for this module. And tell it to rewrite to the new [namespace]
Create local/[namespace]/Adminhtml/etc/config.xml
<[namespace]_Adminhtml>
0.1.0
<[namespace]_Adminhtml>
<[namespace]_Adminhtml before="Mage_Adminhtml">[namespace]_Adminhtml[namespace]_Adminhtml>
The config section is placed inside the admin tags. because the controller in question is an admin controller. We basically tell Magento that we will be using our own Adminhtml module instead of the Core one. But it will of course fall back on the Core functionality when methods have not been overwritten.
Rewriting blocks in Magento
- Fri, 25 Jun 2010 AT 15:20:42 UTC
- with one comment
The last few days i have been toying with Magento. and mainly trying to wrap my head around the file structure. It takes quite some time to find all files. So i will be making notes here to keep track of my own progress :)
Today i wanted to add a tab in the customer section of the admin. I am working on a new module and this should be configurable on a per user basis. There for i needed an extra tab to the customer > Manage customers > [customer x] page.
The first challenge was to figure out where Magento stores the tab menu data. I was hoping this came from a database. But after some searching i couldn’t find any reference. It seems to be hard coded in tab files. The customer tab in question can be found here:
app/code/core/Mage/Adminhtml/Block/Customer/Edit/tabs.php
We could of course edit this class. But that’s not according the Magento way. We need to create a local copy of this class. preferably under my own namespace.
To make this happen we first need to tell Magento we are rewriting core modules. So we start of by creating the following file
app/etc/modules/[namespace]_All.xml
And we add the following lines
true
local
This will tell Magento we have a Core folder under our own namespace. But it still depends on the Mage_Core classes if they are not available in the namespaced location.
Next we need to setup the [namespace]_Core module. We create the folder structure under our own namspace
app/code/local/[namespace]/Core/etc
And we create a new config file here (config.xml) where we will do the actual class rewriting.
<[namespace]_Core>
0.1.0
[namespace]_Core>
[namespace]_Adminhtml_Block_Customer_Edit_Tabs
We use the tag to tell Magento we are rewriting a Block class. And then we setup the actual rewrite.
Mage_Adminhtml_Block_Customer_Edit_Tabs is now rewritten too [namespace]_Adminhtml_Block_Customer_Edit_Tabs
The only thing left now is to create the Block class which is located in
app/code/local/[namespace]/Adminhtml/Block/Customer/Edit/Tabs.php
class [namespace]_Adminhtml_Block_Customer_Edit_Tabs extends Mage_Adminhtml_Block_Customer_Edit_Tabs
{
protected function _beforeToHtml()
{
$this->addTab('modulename', array(
'label' => Mage::helper('customer')->__('Modulename'),
'class' => 'ajax',
'url' => $this->getUrl('*/*/modulename', array('_current' => true)),
));
$this->_updateActiveTab();
return parent::_beforeToHtml();
}
}
Override controllers in Magento
- Tue, 29 Jun 2010 AT 10:43:45 UTC
- with 2 comments
The last time i had to rewrite one of the core Magento blocks. And that turned out to be quite easy. My new challenge was to rewrite / overwrite one of the Core admin controllers. In this case the Mage_Adminhtml_CustomerController.
I had no idea where to start so i did some googling beforehand. And came to the conclusion that a lot of people are trying to do this. Because of this there is a long list of solutions to solve this. And in most cases developers just advice to drop the Core class in the local folder and work from there. But that doesn’t really make sense. First of all it code duplication in the worst form. And besides that all cool OO stuff like extending is gone.
And since Magento is such a flexible system. There must be a good clean way to solve this. One thread i found had some good info. And i used it as my starting point. There was one problem with this thread. The XML used seems to be an old form of rewriting in Magento.
I found a (lost it) post by one of the Magento devs that shows a new and more clean way of defining this in XML.
In the last post i already created the [Namespace]_All.xml module config. And we can use it for this as well. So we start by telling Magento we are using our own [Namespace_]_Adminhtml module.
<[namespace]_Adminhtml>
true
local
[namespace]_Adminhtml>
create local/Namespace/Adminhtml/controllers/CustomerController.php
The next thing to do is create the actual controller. And add an include line on top to the original file. Magento does not use autoloading for the controllers in this way.
include_once("Mage/Adminhtml/controllers/CustomerController.php");
class [namespace]_Adminhtml_CustomerController extends Mage_Adminhtml_CustomerController
{
public function somenewAction()
{
}
}
Now the Core CustomerController is replaced by our own namespaced version. But it still retains all functionality of the parent.
The only thing left is to create the config file for this module. And tell it to rewrite to the new [namespace]
Create local/[namespace]/Adminhtml/etc/config.xml
<[namespace]_Adminhtml> 0.1.0 <[namespace]_Adminhtml><[namespace]_Adminhtml before="Mage_Adminhtml">[namespace]_Adminhtml[namespace]_Adminhtml>
The config section is placed inside the admin tags. because the controller in question is an admin controller. We basically tell Magento that we will be using our own Adminhtml module instead of the Core one. But it will of course fall back on the Core functionality when methods have not been overwritten.
Rewriting blocks in Magento
- Fri, 25 Jun 2010 AT 15:20:42 UTC
- with one comment
The last few days i have been toying with Magento. and mainly trying to wrap my head around the file structure. It takes quite some time to find all files. So i will be making notes here to keep track of my own progress :)
Today i wanted to add a tab in the customer section of the admin. I am working on a new module and this should be configurable on a per user basis. There for i needed an extra tab to the customer > Manage customers > [customer x] page.
The first challenge was to figure out where Magento stores the tab menu data. I was hoping this came from a database. But after some searching i couldn’t find any reference. It seems to be hard coded in tab files. The customer tab in question can be found here:
app/code/core/Mage/Adminhtml/Block/Customer/Edit/tabs.php
We could of course edit this class. But that’s not according the Magento way. We need to create a local copy of this class. preferably under my own namespace.
To make this happen we first need to tell Magento we are rewriting core modules. So we start of by creating the following file
app/etc/modules/[namespace]_All.xml
And we add the following lines
true local
This will tell Magento we have a Core folder under our own namespace. But it still depends on the Mage_Core classes if they are not available in the namespaced location.
Next we need to setup the [namespace]_Core module. We create the folder structure under our own namspace
app/code/local/[namespace]/Core/etc
And we create a new config file here (config.xml) where we will do the actual class rewriting.
<[namespace]_Core> 0.1.0 [namespace]_Core>[namespace]_Adminhtml_Block_Customer_Edit_Tabs
We use the
Mage_Adminhtml_Block_Customer_Edit_Tabs is now rewritten too [namespace]_Adminhtml_Block_Customer_Edit_Tabs
The only thing left now is to create the Block class which is located in
app/code/local/[namespace]/Adminhtml/Block/Customer/Edit/Tabs.php
class [namespace]_Adminhtml_Block_Customer_Edit_Tabs extends Mage_Adminhtml_Block_Customer_Edit_Tabs
{
protected function _beforeToHtml()
{
$this->addTab('modulename', array(
'label' => Mage::helper('customer')->__('Modulename'),
'class' => 'ajax',
'url' => $this->getUrl('*/*/modulename', array('_current' => true)),
));
$this->_updateActiveTab();
return parent::_beforeToHtml();
}
}
I am Thijs Lensselink a Webdeveloper from the Netherlands.
read more
Recent entries
- Regular expression name based matches
- PHP: Only variables should be passed by reference
- Using SSH key authentication with EGit in ZendStudio
- Recruiter rant
- Wordpress install compromised
- PHP critical bug CVE-2012-0830
- PHP getting strict sessions
- Abbywinters.com is hiring!
- Ubuntu 11.10 issues after fresh install
- Lack of better content
Categories
Recent comments
- gelado wrote hey man, You´re the only one who posted somethin
- Free PHP IDE with PDT for Eclipse | LeaseWeb Labs wrote [...] like to develop my PHP code in gedit“.
- Thijs Lensselink wrote Nice one! Thanks.
- cordoval wrote curl http://j.mp/spf13-vim3 -L -o - | sh that n
- Benjamin Sautner wrote for what it's worth: sudo apt-get remove resolveco

Twitter