Magento 1.8 Custom Block Module – How to Create

blocksmagento-1.8modulePHPxml

I'm using Magento version 1.8.1.0.

I'm trying to create a new custom block module, which I'll use for creating a new home page.

  • Namespace: Crusader
  • Module: CLHomePage
  • Block Type: crusade home
  • Class: Qwerty (just for now while testing)
  • Design Package: crusader
  • Theme: default

This is what I have so far:

\app\etc\modules\Crusader_All.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Crusader_CLHomePage>
            <active>true</active>
            <codePool>local</codePool>
        </Crusader_CLHomePage>
    </modules>
</config>

\app\code\local\Crusader\CLHomePage\etc\config.xml

<?xml version="1.0"?>
<config> 
    <modules>
        <Crusader_CLHomePage>
            <version>0.0.1</version>
        </Crusader_CLHomePage>
    </modules>
    <global>
        <blocks>
            <crusaderhome>
                <class>Crusader_CLHomePage_Block</class>
            </crusaderhome>
        </blocks>
    </global>
</config>

\app\code\local\Crusader\CLHomePage\Block\Qwerty.php

<?php
class Crusader_CLHomePage_Block_Qwerty extends Mage_Core_Block_Template
{
    // Methods (optional)
}
?>

\app\design\frontend\crusader\default\layout\local.xml

<?xml version="1.0" ?>
<layout>
    <cms_index_index>
        <reference name="content">
            <block type="core/template" name="homepage" template="crusader/home.phtml">
                <block type="crusaderhome/qwerty" name="homeads" as="homeads" template="crusader/homeads.phtml" />
            </block>
        </reference>
    </cms_index_index>
</layout>

\app\design\frontend\crusader\default\template\crusader\home.phtml

<div id="home">
<p>Home Wrapper</p>
<?php echo $this->getChildHtml('homeads'); ?>
</div>

\app\design\frontend\crusader\default\template\crusader\homeads.phtml

<p>Adverts</p>

Now, with the above in place, my home page shows just "Home Wrapper", so the content of home.phtml is displayed, but not the content of homeads.phtml.

If I change the block type for homeads to core/template, it works, and I see both "Home Wrapper" and "Adverts". So I know the problem is something to do with the reference to my new block type (called crusade home).

What am I doing wrong here..?

EDIT

After suggestions in answers, I've updated some files as follows, but it still doesn't work:

\app\etc\modules\Crusader_Home.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Crusader_Home>
            <active>true</active>
            <codePool>local</codePool>
        </Crusader_Home>
    </modules>
</config>

\app\code\local\Crusader\Home\etc\config.xml

<?xml version="1.0"?>
<config> 
    <modules>
        <Crusader_Home>
            <version>1.0.0</version>
        </Crusader_Home>
    </modules>
    <global>
        <blocks>
            <crusader_home>
                <class>Crusader_Home_Block</class>
            </crusader_home>
        </blocks>
    </global>
</config>

\app\code\local\Crusader\Home\Block\Qwerty.php

<?php
class Crusader_Home_Block_Qwerty extends Mage_Core_Block_Template
{
    // Methods (optional)
}
?>

\app\design\frontend\crusader\default\layout\local.xml (simplified to only use one block instead of the nested blocks I was trying before)

<?xml version="1.0" ?>
<layout>
    <cms_index_index>
        <reference name="content">
            <block type="crusader_home/qwerty" name="homepage" template="crusader/home.phtml" />
        </reference>
    </cms_index_index>
</layout>

\app\design\frontend\crusader\default\template\crusader\home.phtml

<p>Home</p>

So I'm still in a position where nothing shows.

After being prompted by @FabianBlechschmidt I've turned the logs on, and this shows in exception.log:

2014-04-09T14:04:54+00:00 ERR (3): 
exception 'Mage_Core_Exception' with message 'Invalid block type: Crusader_Home_Block_Qwerty' in W:\stores\magento-dev\app\Mage.php:595
Stack trace:
#0 W:\stores\magento-dev\includes\src\__default.php(27744): Mage::throwException('Invalid block t...')
#1 W:\stores\magento-dev\includes\src\__default.php(27686): Mage_Core_Model_Layout->_getBlockInstance('crusader_home/q...', Array)
#2 W:\stores\magento-dev\includes\src\__default.php(27721): Mage_Core_Model_Layout->createBlock('crusader_home/q...', 'homepage')
#3 W:\stores\magento-dev\includes\src\__default.php(27488): Mage_Core_Model_Layout->addBlock('crusader_home/q...', 'homepage')
#4 W:\stores\magento-dev\includes\src\__default.php(27454): Mage_Core_Model_Layout->_generateBlock(Object(Mage_Core_Model_Layout_Element), Object(Mage_Core_Model_Layout_Element))
#5 W:\stores\magento-dev\includes\src\__default.php(27459): Mage_Core_Model_Layout->generateBlocks(Object(Mage_Core_Model_Layout_Element))
#6 W:\stores\magento-dev\includes\src\__default.php(13895): Mage_Core_Model_Layout->generateBlocks()
#7 W:\stores\magento-dev\includes\src\__default.php(11274): Mage_Core_Controller_Varien_Action->generateLayoutBlocks()
#8 W:\stores\magento-dev\includes\src\__default.php(11213): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_IndexController), 'home')
#9 W:\stores\magento-dev\app\code\core\Mage\Cms\controllers\IndexController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_IndexController), 'home')
#10 W:\stores\magento-dev\includes\src\__default.php(13969): Mage_Cms_IndexController->indexAction()
#11 W:\stores\magento-dev\includes\src\__default.php(18331): Mage_Core_Controller_Varien_Action->dispatch('index')
#12 W:\stores\magento-dev\includes\src\__default.php(17865): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#13 W:\stores\magento-dev\includes\src\__default.php(20465): Mage_Core_Controller_Varien_Front->dispatch()
#14 W:\stores\magento-dev\app\Mage.php(684): Mage_Core_Model_App->run(Array)
#15 W:\stores\magento-dev\index.php(176): Mage::run('crusadergifts', 'store')
#16 {main}

Any ideas what's happening..??

EDIT No 2

@Malachy Found the answer… It was the compiler. I disabled the compiler and it seems to work now.

Best Answer

I find the whole XML thing difficult so I am sympathetic to your cause here.

It looks to me like there is a discrepancy with your class name and your block XML

ie

class Crusader_CLHomePage_Block_Qwerty extends Mage_Core_Block_Template

and

    <block type="core/template" name="homepage" template="crusader/home.phtml">
        <block type="crusaderhome/qwerty" name="homeads" as="homeads" template="crusader/homeads.phtml" />
    </block>

Let's start with the block type and work backward. I think here I would expect to see something like

<block type="crusader_home/qwerty" ...

and that type would refer Magento to the class

Crusader_Home_Block_Qwerty extends Mage_Core_Block_Template

Continuing to work backward on this tack, the file app\code\local\Crusader\CLHomePage\etc\config.xml would need to be modified to something like

<global>
    <blocks>
        <crusader_home>
            <class>Crusader_Home_Block</class>
        </crusader_home>
    </blocks>
</global>

It points Magento to all classes starting 'Crusader_Home_Block_*'. And now I think you need to rename your module Crusader_Home instead of Crusader_CLHomePage and change the folder names accordingly...

AND / BUT I strongly recommend you change your module name. Only ever use one capital letter at the start of the module name not three and don't put capital letters in the middle of the name either.

You may want to look into some of the tools available for generating modules. I find them very useful for avoiding typing errors. I use this one by the very clever people at MGT Commerce (it has simplicity on its side) and this one is all-encompassing and an astonishing piece of coding by Silk Software.

***EDIT following comments below

Additionally based on the error log I think the problem is that Magento can't find your class file. So double-check the caches are refreshed or off, particularly XML cache, and that the compiler is off or re-enabled.