4

I created a module where I try to create a new Magento Category Layer Filter. I'm working in the "code/local" area because I overrided some core files. Everything was ok until I tryed to add a new Catalog Layer View block.

Let's say that I have this structure:

core/local/Mynamespace/Mymodule/etc/config.xml

Here I defined the module path for controllers, blocks, models and helpers

..

<config>
    ...
    <global>
        <blocks>
            <Mynamespace_Mymodule>
                <class>Mynamespace_Mymodule_Block</class>
            </Mynamespace_Mymodule>
        </blocks>
    ...

So, the blocks are well configured.

In the folder: core/local/Mynamespace/Mymodule/Block I have:

core/local/Mynamespace/Mymodule/Block/Catalog/Layer/View.php

and

core/local/Mynamespace/Mymodule/Block/Catalog/Layer/Newtype.php

In the View.php contains:

    class Mynamespace_Mymodule_Block_Catalog_Layer_View extends Mage_Catalog_Block_Layer_View
{

    protected $_newtypeBlockName;

        protected function _initBlocks()
        {
            parent::_initBlocks();

            $this->_newtypeBlockName = "mynamespace_mymodule/catalog_layer_filter_newtype;
        }


    protected function _prepareLayout()
        {
            $newtypeBlock = $this->getLayout()->createBlock($this->_newtypeBlockName)
                    ->setLayer($this->getLayer())
                    ->init();

            $this->setChild('newtype_filter', $newtypeBlock);

            return parent::_prepareLayout();
        }

In the function _prepareLayout, I receive an error at this code block, where I use the function "setLayer":

$newtypeBlock = $this->getLayout()->createBlock($this->_newtypeBlockName)
                    ->setLayer($this->getLayer())
                    ->init();

I debugged the error and the message is:

Invalid block type: Mage_Mynamespace_Mymodule_Block_Catalog_Layer_View

The error comes from the class: Mage_Core_Model_Layout, more exactly, from this function:

public function createBlock($type, $name='', array $attributes = array())
    {
        try {
            $block = $this->_getBlockInstance($type, $attributes);
        } catch (Exception $e) {
            var_dump($e);die(" - FROM HERE COMES THE ERROR");
            Mage::logException($e);
            return false;
        }

        if (empty($name) || '.'===$name{0}) {
            $block->setIsAnonymous(true);
            if (!empty($name)) {
                $block->setAnonSuffix(substr($name, 1));
            }
            $name = 'ANONYMOUS_'.sizeof($this->_blocks);
        } elseif (isset($this->_blocks[$name]) && Mage::getIsDeveloperMode()) {
            //Mage::throwException(Mage::helper('core')->__('Block with name "%s" already exists', $name));
        }
         ..

You can see that I edited this function and I used var_dump to display the error.

That function also calls this function:

protected function _getBlockInstance($block, array $attributes=array())
    {
        if (is_string($block)) {
            if (strpos($block, '/')!==false) {
                if (!$block = Mage::getConfig()->getBlockClassName($block)) {
                    Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
                }
            }
            if (class_exists($block, false) || mageFindClassFile($block)) {
                $block = new $block($attributes);
            }
        }
        if (!$block instanceof Mage_Core_Block_Abstract) {
            Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
        }
        return $block;
    }

And the exception is thrown on this line: if (!$block instanceof Mage_Core_Block_Abstract) {

In my case, the $block type is Mage_Core_Block_Template (which extends the Mage_Core_Block_Abstract class, but still doesn't help me).

I also have a frontend xml layout file, from where I'm loading the block:

<catalog_category_layered>
    <reference name="left_first">
        <reference name="catalog.leftnav">
            <action method="setTemplate">
                <template>Mynamespace/Mymodule/catalog/layer/view.phtml</template>
            </action>
        </reference>
        <block type="mynamespace_mymodule/catalog_layer_view" name="mynamespace_mymodule.catalog.leftnav" after="currency" template="Mynamespace/Mymodule/catalog/layer/view.phtml"/>
    </reference>
</catalog_category_layered>

When building my module, I followed this http://www.techytalk.info/create-custom-layered-navigation-filter-magento/ tutorial. Any idea why I'm getting the error that I mentioned earlyer?

The error was: Invalid block type: Mage_Mynamespace_Mymodule_Block_Catalog_Layer_View

Any help will be great, thanks.

2 Answers 2

1

You have call all the block types by block prefix mynamespace_mymodule

But you have define it as Mynamespace_Mymodule

 <blocks>
            <Mynamespace_Mymodule> <!-- call as block prefix -->
                <class>Mynamespace_Mymodule_Block</class>
            </Mynamespace_Mymodule>
        </blocks>

You need to change the block prefix Mynamespace_Mymodule by mynamespace_mymodule

<config>
    ...
    <global>
        <blocks>
            <mynamespace_mymodule>
                <class>Mynamespace_Mymodule_Block</class>
            </mynamespace_mymodule>
        </blocks>
    ...
1
  • Can't believe it worked.. I used Mynamespace_Mymodule for models and helper also. There it worked well. I'll just refactor the code to use lowercase words everywhere.. Thanks again.
    – Pascut
    Jul 7, 2015 at 14:14
0

In some cases (Magento 1.9), if we forgot to use construct function in block PHP file then put it and the issue will be fixed:

 protected function _construct()
    {
        parent::_construct();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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