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月09日T14: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.
3 Answers 3
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.
-
thanks, I've tried your suggestions. I've renamed my module to 'Home' and updated all the references and folders. Still doesn't work. I'll edit my question to show the changes.Stephen Last– Stephen Last2014年04月09日 14:11:24 +00:00Commented Apr 9, 2014 at 14:11
-
1It looks better certainly. I wonder if there is still a sneaky typing error somewhere. 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 esp. XML cache and the the compiler is off or re-enabled. It wouldn't be a file permission issue would it?Malachy– Malachy2014年04月09日 14:40:32 +00:00Commented Apr 9, 2014 at 14:40
-
1You mentioned the magic word - Complier... I disabled the complier and it seems to work now. Thank you! If you add this as an answer I'll accept it for you! :-)Stephen Last– Stephen Last2014年04月09日 15:48:10 +00:00Commented Apr 9, 2014 at 15:48
-
Actually, it was not just the compiler. Your original XML
<block type="crusaderhome/qwerty"is incorrect. The solution is to both a) correct the XML and b) re-compileMalachy– Malachy2014年04月11日 17:25:45 +00:00Commented Apr 11, 2014 at 17:25
reply your question at the first place
<block type="core/template" name="homepage" template="crusader/home.phtml">
<block type="crusaderhome/qwerty" name="homeads" as="homeads" template="crusader/homeads.phtml" />
</block>
should be replace by
<block type="core/template" name="homepage" template="crusader/home.phtml"></block>
<block type="crusaderhome/qwerty" name="homeads" as="homeads"
template="crusader/homeads.phtml" />
because homeads is not the child of homepage. I think this would work.
-
Thanks! I had tried that before with no luck. The parent/child thing was working fine when the block type was core/template (rather than my custom block type) so my problem is not block placement.Stephen Last– Stephen Last2014年04月09日 15:28:34 +00:00Commented Apr 9, 2014 at 15:28
I got the same problem and resolved it by replacing following code in config.xml
<global>
<blocks>
<crusaderhome>
<class>Crusader_CLHomePage_Block</class>
</crusaderhome>
</blocks>
</global>
to this
<global>
<blocks>
<crusader_clhomepage>
<class>Crusader_CLHomePage_Block</class>
</crusader_clhomepage>
</blocks>
</global>
It should solve the problem.
exception 'Mage_Core_Exception' with message 'Invalid block type: Crusader_Home_Block_Qwerty'