1

I'm trying to add a button on the cache page, next to the "Flush Cache Storage" button. My approach is to overwrite the block "Mage_Adminhtml_Block_Cache". But I can't get it to work. Does anyone know what I'm doing wrong?

Mymodule/FlushImages/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mymodule_FlushImages>
            <version>1.0.0</version>
        </Mymodule_FlushImages>
    </modules>
    <global>        
        <blocks>
            <adminhtml>
                <rewrite>
                    <cache>Mymodule_FlushImages_Block_Adminhtml_Cache</cache>
                </rewrite>
            </adminhtml>
        </blocks>                                                                                                   
    </global>
</config>

Mymodule/FlushImages/Block/Adminhtml/Cache.php

<?php

    class Mymodule_FlushImages_Block_Adminhtml_Cache extends Mage_Adminhtml_Block_Cache
    {
        /**
         * Class constructor
         */
        public function __construct()
        {
            $this->_addButton('flush_magento', array(
                'label'     => Mage::helper('core')->__('Flush Images'),
                'onclick'   => 'setLocation(\'' . $this->getFlushImagesUrl() .'\')',
                'class'     => 'delete',
            ));

            parent::__construct();
        }

        /**
         * Get url for delete Images
         */
        public function getFlushImagesUrl()
        {
            return $this->getUrl('*/*/flushImages');
        }
    }

Note: I did create the module config file in app/etc/modules.

Help is highly appreciated.

0

your line:

<adminhtml_cache>Mymodule_FlushImages_Block_Adminhtml_Cache</adminhtml_cache>

should be replaced with:

<cache>Mymodule_FlushImages_Block_Adminhtml_Cache</cache>
  • 1
    also if you want to put your button after "Flush Cache Storage" then at first line call parrent constructor and after that add you button. – Rostislav Sabischenko Feb 17 '16 at 20:00
  • Thanks! I fixed the block node but unfortunately the button still does not show up (ie 1.7.0.2). I updated my initial question so its more specific. – Stefan Feb 18 '16 at 10:08
  • It couldn't work in your case because you created button with ID flush_magento and then run _construct which replaces your button with Magento native button. I mentioned to use construct before your code. Also specify unique ID for button. – Rostislav Sabischenko Feb 18 '16 at 16:26
  • Thanks Rostislav. You are right! This works. Gonna except your answer because it matches more the initial question. – Stefan Feb 19 '16 at 9:27
0

Finally I found a way - by using an model/observer:

etc/config.xml

<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <clearimages_core_block_abstract_prepare_layout_before>
                    <class>clearimages/observer</class>
                    <method>addButton</method>
                </clearimages_core_block_abstract_prepare_layout_before>
            </observers>
        </core_block_abstract_prepare_layout_before>
    </events>
</adminhtml>

Model/Observer

class Mymodule_FlushImages_Model_Observer
{
    public function addButton($observer) {

        $block = $observer->getEvent()->getBlock();
        if(get_class($block) == 'Mage_Adminhtml_Block_Cache') {
            $button = $block->addButton('clearimages', array(
                ...
            ));
        }
    }
}

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.