0

I am trying to insert a custom module/block in my home page. but I am unable to figure out how to call the block from layout file. can some one explain how to debug the log file. I can see that my code stuck at _getBlockInstance but I am unable to figure it out what's wrong. please help.

LOG

    exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_Market_Block_Featured' in /Applications/MAMP/htdocs/market/app/Mage.php:595
    Stack trace:
    #0 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/Layout.php(495): Mage::throwException('Invalid block t...')
    #1 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('market/featured', Array)
    #2 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/Layout.php(472): Mage_Core_Model_Layout->createBlock('market/featured', 'featured_featur...')
    #3 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/Layout.php(239): Mage_Core_Model_Layout->addBlock('market/featured', 'featured_featur...')
    #4 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/Layout.php(205): Mage_Core_Model_Layout->_generateBlock(Object(Mage_Core_Model_Layout_Element), Object(Mage_Core_Model_Layout_Element))
    #5 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/Layout.php(210): Mage_Core_Model_Layout->generateBlocks(Object(Mage_Core_Model_Layout_Element))
    #6 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Controller/Varien/Action.php(344): Mage_Core_Model_Layout->generateBlocks()
    #7 /Applications/MAMP/htdocs/market/app/code/core/Mage/Cms/Helper/Page.php(113): Mage_Core_Controller_Varien_Action->generateLayoutBlocks()
    #8 /Applications/MAMP/htdocs/market/app/code/core/Mage/Cms/Helper/Page.php(52): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_IndexController), 'home')
    #9 /Applications/MAMP/htdocs/market/app/code/core/Mage/Cms/controllers/IndexController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_IndexController), 'home')
    #10 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Cms_IndexController->indexAction()
    #11 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
    #12 /Applications/MAMP/htdocs/market/app/code/community/BalkeTechnologies/StoreMaintenance/Controller/Router/Standard.php(91): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #13 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Controller/Varien/Front.php(172): BalkeTechnologies_StoreMaintenance_Controller_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #14 /Applications/MAMP/htdocs/market/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
    #15 /Applications/MAMP/htdocs/market/app/Mage.php(684): Mage_Core_Model_App->run(Array)
    #16 /Applications/MAMP/htdocs/market/index.php(105): Mage::run('', 'store')
    #17 {main}

local.xml

<?xml version="1.0" encoding="UTF-8"?>
    <layout version="0.1.0">
        <default>
            <cms_index_index>    
                <reference name="content">    
                    <block type="market/featured" name="featured_featured" as="feature" template="ecom/featured.phtml">
                    </block>
                </reference> 
            </cms_index_index>
        <default>
    </layout>

app/code/local/Market/Featured/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Market_Featured>
            <version>1.0.0</version>
        </Market_Featured>
    </modules>
    <global>
        <blocks>
            <featured>
                <class>Market_Featured_Block</class>
            </featured>
        </blocks>
        <helpers>
            <featured>
                <class>Market_Featured_Helper</class>
            </featured>
        </helpers>
        <models>
            <featured>
                <class>Market_Featured_Model</class>
            </featured>
        </models>
    </global>
</config>

/Block/Featured.php

class Market_Featured_Block_Featured extends Mage_Core_Block_Template {
        public function getFeatureProducts() {
            $products = Mage::getModel("featured/featured")­->getFeatureProducts();
            return $products;
  }
}

/Model/Featured.php

class Market_Featured_Model_Featured extends Mage_Core_Model_Abstract {
    public function getFeatureProducts() {
        $feature_products = Mage::getModel('catalog/product')->getCollection();
        $feature_products->addAttributeToSelect(array(
                                       'image',
                                       'name',
                                       'short_description', 
                       ))
                       ->addFieldToFilter('visibility', array(
                                   Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                                   Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                       )) //showing just products visible in catalog or both search and catalog
                       ->addFieldToFilter('feature', array('eq' => 1))
                       ->addAttributeToSort('category_id', 'ASC')
                       ->addFinalPrice()
                       ->getSelect()
                       ->where('price_index.final_price < price_index.price')
                       ->limit(3) //we can specify how many products we want to show on this page
    //                 ->order(new Zend_Db_Expr('RAND()')) //in case we would like to sort products randomly
                       ;

        return $feature_products;
    }
}
1
  • 1
    I'm at least 70% certain all you need to do in local.xml is change your block type from market/featured to market_featured/featured
    – andyjv
    Jan 26, 2016 at 21:27

1 Answer 1

2

In the layout.xml file you have to write featured/featured for block type:

<block type="featured/featured" name="featured_featured" as="feature" template="ecom/featured.phtml">

Hope this helps.

1
  • type attribute is used to set block class alias. The alias is based on config.xml file of your module.
    – Max Pronko
    Jan 26, 2016 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.