0

I'm trying to create a super simple Admin module that will add a button to the Cache Management page. So far I have created the following files.

Gamma/ExtendedAdmin/registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gamma_ExtendedAdmin',
    __DIR__
);

Gamma/ExtendedAdmin/etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Gamma_ExtendedAdmin" setup_version="2.1.0">
    </module>
</config>

Gamma/ExtendedAdmin/view/adminhtml/layout/adminhtml_cache_index.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="formkey"/>
    <update handle="adminhtml_cache_block"/>
    <body>
        <referenceContainer name="content">
            <block class="Gamma\ExtendedAdmin\Block\Cache" name="adminhtml.cache.container"/>
            <block class="Magento\Backend\Block\Cache\Additional" name="cache.additional" template="Magento_Backend::system/cache/additional.phtml"/>
        </referenceContainer>
    </body>
</page>

Gamma/ExtendedAdmin/Block/Cache.php:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Gamma\ExtendedAdmin\Block;

/**
 * @api
 * @since 100.0.2
 */
class Cache extends \Magento\Backend\Block\Widget\Grid\Container
{
    /**
     * Class constructor
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_controller = 'cache';
        $this->_headerText = __('Cache Storage Management');
        parent::_construct();
        $this->buttonList->remove('add');
        $this->buttonList->add(
            'flush_magento',
            [
                'label' => __('Flush Magento Cache'),
                'onclick' => 'setLocation(\'' . $this->getFlushSystemUrl() . '\')',
                'class' => 'primary flush-cache-magento'
            ]
        );

        $message = __('The cache storage may contain additional data. Are you sure that you want to flush it?');
        $this->buttonList->add(
            'flush_system',
            [
                'label' => __('Flush Cache Storage'),
                'onclick' => 'confirmSetLocation(\'' . $message . '\', \'' . $this->getFlushStorageUrl() . '\')',
                'class' => 'flush-cache-storage'
            ]
        );
        $this->buttonList->add(
            'flush_varnish_cache',
            [
                'label' => __('Flush Varnish Cache'),
                'onclick' => 'confirmSetLocation(\'' . $message . '\', \'' . $this->getFlushVarnishUrl() . '\')',
                'class' => 'flush-varnish-cache'
            ]
        );
    }

    /**
     * Get url for clean cache storage
     *
     * @return string
     */
    public function getFlushStorageUrl()
    {
        return $this->getUrl('adminhtml/*/flushAll');
    }

    /**
     * Get url for clean cache storage
     *
     * @return string
     */
    public function getFlushVarnishUrl()
    {
        return $this->getUrl('/run/cleanCache.php');
    }

    /**
     * Get url for clean cache storage
     *
     * @return string
     */
    public function getFlushSystemUrl()
    {
        return $this->getUrl('adminhtml/*/flushSystem');
    }
}

I have enabled the extension but when I go to the cache management page the button is not present. I'm sure I'm missing something as this is my first Admin module. Any help you guys can offer to get this button to show up would be appreciated greatly!

EDIT: I have done some more testing and discovered if I change vendor/magento/module-backend/view/adminhtml/layout/adminhtml_cache_index.xml to match my version it works. So it seems there is something wrong with my adminhtml_cache_index.xml file that isn't pointing it at my Cache.php file.

5
  • Did you log out and back in to the admin?
    – Haim
    Oct 26, 2018 at 20:02
  • @Haim just tried that, no difference. Thanks for the suggestion! Oct 26, 2018 at 20:06
  • 1
    You layout file path is wrong. It should be Gamma/ExtendedAdmin/view/adminhtml/layout/adminhtml_cache_index.xml Oct 26, 2018 at 20:16
  • @AbhishekPanchal thanks for catching that, I've made the change but no luck. The button still is not appearing. Oct 26, 2018 at 20:22
  • @DanielBlack, please check my answer Oct 26, 2018 at 21:16

1 Answer 1

1

Edit your module.xml with this code -

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Gamma_ExtendedAdmin" setup_version="2.1.0">
        <sequence>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>

in above code you just need dependency of core module Magento_Backend

then run command -

php bin magento setup:upgrade

chmod -R 777 var/ generated

1
  • Worked like a charm, thank you soo much :) Oct 29, 2018 at 19:06

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.