Solved: Magento 1.9.2.0 static block display issue

Are you upgrading your e-commerce store to Magento 1.9.2.0? You may face an issue with static block display. It may result in a display of the wrong block instead of the correct one or duplicate blocks in some places. Luckily for you, we have found different solutions to the problem. Learn how to troubleshoot the problem and get ready for an error-free e-commerce store.
While upgrading a Magento store version we found a bug in Magento 1.9.2.0 and here, we are giving a solution for this bug.

Bug Description: While inserting two different CMS blocks in a page, Magento 1.9.2 displays static blocks sporadically which sometimes shows the wrong block rather than the correct one and sometimes displays the same block twice.

Reason: The problem occurs due to generation of caching key info for CMS static blocks using the names rather thank block ID. Thus static blocks shares the same cache key and get mixed up in cache.

If you want to reproduce the issue to better understand, perform following steps.

* Create a static block with identifier “test_1? and content “This is test block 1?
* Create a static block with identifier “test_2? and content “This is test block 2?
* Edit the CMS home page. Using the widget selector, add “test_1? and “test_2? cms blocks
* Enable cache.

Expected Result:
This is test block 1
This is test block 2

Actual Result due to bug:
This is test block 1
This is test block 1

We have resolved this bug in Magento 1.9.2.0 and here is the solution explained.

This happens due to the following code being added to the constructor of Mage_Cms_Block_Block:

$this->setCacheTags(array(Mage_Cms_Model_Block::CACHE_TAG));
$this->setCacheLifetime(false);

It adds cache functionality to static blocks. But cache key info is generated in Mage_Core_Block_Abstract, which use block name in layout. Block name does not exist since static block are not added though layout, and as a result they have the same cache key.

Solution 1:
To fix it we have created simple extension. It override Mage_Cms_Block_Block to generate correct cache key info for each static block based on store id, block id and secure connection.

  1. Create /app/etc/modules/NameSpace_Modulename.xml.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0"?>
    <config>
        <modules>
            <NameSpace_Modulename>
                <active>true</active>
                <codePool>local</codePool>
                <depends>
                    <Mage_Cms/>
                </depends>
            </NameSpace_Modulename>
        </modules>
    </config>
  2. Create /app/code/local/NameSpace/Modulename/etc/config.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?xml version="1.0"?>
    <config>
        <modules>
            <NameSpace_Modulename>
                <version>1.0.0</version>
            </NameSpace_Modulename>
        </modules>
        <global>
            <blocks>
                <cms>
                    <rewrite>
                        <block>NameSpace_Modulename_Block_Block</block>
                        <widget_block>NameSpace_Modulename_Block_Widget_Block</widget_block>
                    </rewrite>
                </cms>
            </blocks>
        </global>
    </config>
  3. Create /app/code/local/NameSpace/Modulename/Block/Block.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?php class NameSpace_Modulename_Block_Block extends Mage_Cms_Block_Block {
               public function getCacheKeyInfo() {
                if ($this->getBlockId())
                {
                return array(
                    Mage_Cms_Model_Block::CACHE_TAG,
                    Mage::app()->getStore()->getId(),
                    $this->getBlockId(),
                    (int) Mage::app()->getStore()->isCurrentlySecure()
                );
            } else {
                return parent::getCacheKeyInfo();
            }
          }
    }
  4. Create /app/code/local/NameSpace/Modulename/Block/Widget/Block.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <?php class NameSpace_Modulename_Block_Widget_Block extends Mage_Cms_Block_Widget_Block {
    /** * Storage for used widgets * * @var array */
    static protected $_widgetUsageMap = array();
    /** * Prepare block text and determine whether block output enabled or not
    * Prevent blocks recursion if needed * * @return Mage_Cms_Block_Widget_Block */
            protected function _beforeToHtml() {
            parent::_beforeToHtml();
            $blockId = $this->getData('block_id');
            $blockHash = get_class($this) . $blockId;
            if (isset(self::$_widgetUsageMap[$blockHash])) {
                return $this;
            }
            self::$_widgetUsageMap[$blockHash] = true;
            if ($blockId) {
                $block = Mage::getModel('cms/block')
                    ->setStoreId(Mage::app()->getStore()->getId())
                    ->load($blockId);
                if ($block->getIsActive()) {
                    /* @var $helper Mage_Cms_Helper_Data */
                    $helper = Mage::helper('cms');
                    $processor = $helper->getBlockTemplateProcessor();
                    $this->setText($processor->filter($block->getContent()));
                    $this->addModelTags($block);
                }
            }
            unset(self::$_widgetUsageMap[$blockHash]);
            return $this;
        }
         /**
         * Retrieve values of properties that unambiguously identify unique content
         *
         * @return array
         */
        public function getCacheKeyInfo()
        {
            $result = parent::getCacheKeyInfo();
            $blockId = $this->getBlockId();
            if ($blockId) {
                $result[] = $blockId;
           }
            return $result;
       }
    }
  5. Thats it. CMS static blocks issue solved.

Solution 2:
Temporary Fix Disable the Blocks HTML output cache and the blocks displayed are correctly.

cachemanagement

Leave a Comment