Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend CMS Block on save event of Magento?

I have this XML structure in my Alchemy Catalog Module:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    @filepath /app/code/local/Alchemy/Catalog/etc
The XML has been extended following
Magento Events API Observers
http://www.excellencemagentoblog.com/magento-part11-series-eventsapi
or
http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/
-->
<config>
    <modules>
        <Alchemy_Catalog>
            <version>0.1.0</version>
        </Alchemy_Catalog>
    </modules>

    <global>
        <models>
            <alchemycatalog>
                <rewrite>
                    <product>Alchemy_Catalog_Model_Product</product>
                    <block>Alchemy_Catalog_Model_Block</block>
                </rewrite>
            </alchemycatalog>
        </models>
        <events>
            <!--
            Examples: catalog_product_save_before, catalog_product_prepare_save
            Check out Magento events cheat sheet at http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ -->
            <catalog_product_save_after>
                <observers>
                    <Alchemy_Catalog>
                        <type>singleton</type>
                        <class>Alchemy_Catalog_Model_Product</class>
                        <method>pingBaseProductService</method>
                    </Alchemy_Catalog>
                </observers>
            </catalog_product_save_after>
            <catalog_category_save_after>
                <observers>
                    <Alchemy_Catalog>
                        <type>singleton</type>
                        <class>Alchemy_Catalog_Model_Product</class>
                        <method>pingBaseCategoryService</method>
                    </Alchemy_Catalog>
                </observers>
            </catalog_category_save_after>
            <cms_block_save_before>
                <observers>
                    <Alchemy_Catalog>
                        <type>singleton</type>
                        <class>Alchemy_Catalog_Model_Block</class>
                        <method>rabbitmqBlockProducer</method>
                    </Alchemy_Catalog>
                </observers>
            </cms_block_save_before>
        </events>
    </global>
</config>

and this model that should log a message to prove it works:

<?php
class Alchemy_Catalog_Model_Block extends Mage_Cms_Model_Block {
    /**
     * Implement function rabbitmqBlockProducer().
     * This function writes a message to the rabbit
     * mq server
     */
    protected $_eventPrefix = 'cms_block';

    public function rabbitmqBlockProducer ($event) {
        Mage::log('save3 block invoked', null, 'marian.log');
    }
    /**
     * Prevent blocks recursion
     *
     * @throws Mage_Core_Exception
     * @return Mage_Core_Model_Abstract
     */
    protected function _beforeSave() {
        Mage::log('save2 block invoked', null, 'marian.log');
    }
}

But the rabbitmqBlockProducer() method never gets called. Note: The other methods for product and catalog work just fine.

Any help fixing this or any other method will be appreciated

like image 609
Marian Zburlea Avatar asked Jul 24 '13 11:07

Marian Zburlea


People also ask

How do you call a CMS block in XML file in Magento 2?

Except for displaying CMS block on CMS Page or CMS Block, showing it on category page there is an option to call CMS block programmatically using XML file. You need to replace "my_cmsblock_identifier" with your CMS block Identifier or ID (we recommend to use Identifier).

How do I display CMS block in Magento 2?

Use the below code snippet if you want to show CMS Static Block from template phtml file in Magento 2: echo $this->getLayout() ->createBlock('Magento\Cms\Block\Block') ->setBlockId('your_block_identifier') ->toHtml(); For another block within CMS Block in Magento 2, please use the below code to show it.

What is CMS Static Block?

Content blocks are sometimes referred to as static blocks, or CMS blocks. They can be used to display fixed information such as text, images, and embedded video, and dynamic information that is provided by a widget or originates in a database or other source.


1 Answers

Mage_Cms_Model_Block does not override the _eventPrefix property, so it therefore will only trigger two generic events before saving: model_save_before and core_abstract_save_before.

Whereas you are already rewriting the CMS block model, you can simply override the _eventPrefix property and set it to cms_block; this will allow your observer to work. However, because you are already performing a rewrite and using your model as an observer, you could simply override the _beforeSave() method and add your logic in there and simply things a bit.

Edit: Your rewrite isn't working. Rewrite syntax for cms/block model is

<global>
    <models>
        <cms>
            <rewrite>
                <block>Alchemy_Catalog_Model_Block</block>
            </rewrite>
        </cms>
    </models>
</global>
like image 51
benmarks Avatar answered Sep 19 '22 22:09

benmarks