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
Error’d
- On June 30th, 2010
- without comments
Override controllers in Magento
- On June 29th, 2010
- 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
- On June 25th, 2010
- 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();
}
}
Error’d
- On June 30th, 2010
- without comments
Override controllers in Magento
- On June 29th, 2010
- 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
- On June 25th, 2010
- 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();
}
}
Recent entries
- 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
- The current state of SSL And The Future Of Authenticity
- PHP slow on 32-bit Ubuntu
- PHP5 filesize limit on 32-bit system
- Jquery unrecognized expression error
- PHP locale dates adventure
Categories
Recent comments
- Thijs Lensselink wrote Thanks for the code addition. I'll see if i can ad
- Thomas Dutrion wrote Hi, Thanks for the tutorial and the sources. One
- Zend Framework « Blog Ramon RDM wrote [...] http://lenss.nl/2010/02/zend-framework-boots
- Thijs Lensselink wrote Good to hear you're writing an article about it as
- stephenchow wrote Dont worry. And yes about your comment. I understa
Stuff i read
- Ajaxian
- Andrei Zmievski
- Anton Shevchuk
- Antony Dovgal
- Ben Ramsey
- Brian DeShong
- Bruce Schneier
- Cal Evans
- Chris Shiflett
- Clint Berry
- Codinghorror
- Derick Rethans
- ha.ckers
- Ivo Jansch
- Jon Udell
- Kore Nordmann
- Ligaya Turmelle
- Lorna Mitchell
- Marco Tabini
- Michelangelo van Dam
- miek.nl
- Pádraic Brady
- Paul M. Jones
- phpgangsta
- PHPGuru
- Quirksmode
- Ralph Schindler
- Rob Allen
- Sean Coates
- Sebastian Bergmann
- Sebastian Mordziol
- Stefan Koopmanschap
- the daily WTF
- The Spanner




Thijs Lensselink is a PHP developer, consultant and all out open source enthusiast.
He has over 12+ years of experience in building and maintaining web applications mostly
on linux/Unix/BSD platforms. Besides a full time job he does freelance work with his ...