7

I'd like to add another button in the backend on the cms page edit view. So I've created a module (with the help of: https://community.magento.com/t5/Programming-Questions/Overriding-a-block-in-Magento-2/m-p/6831):

app/code/Company/Modulename/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Company_Modulename" schema_version="0.0.1" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Cms" />
        </sequence>
    </module>
</config>

app/code/Company/Modulename/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Cms\Block\Adminhtml\Page\Edit" type="Company\Modulename\Block\Adminhtml\Page\Edit" />
</config>

app/code/Company/Modulename/Block/Adminhtml/Page/Edit.php

<?php
namespace Company\Modulename\Block\Adminhtml\Page;

class Edit extends Magento\Cms\Block\Adminhtml\Page\Edit {

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

        $this->buttonList->add('Test',[
            'label' => __('Test')
        ]);
    }
}

And added it to the app/etc/config.php file: 'Company_Modulename' => 1

But this gives me an error: Fatal error: Class 'Company\Modulename\Block\Adminhtml\Page\Magento\Cms\Block\Adminhtml\Page\Edit' not found in C:\git-projects\magento2\app\code\Company\Modulename\Block\Adminhtml\Page\Edit.php on line 4

Do I need to reference to something else, because the app/code/Magento/Cms/Block/Adminhtml/Page/Edit.php file starts with:

namespace Magento\Cms\Block\Adminhtml\Page;
class Edit extends \Magento\Backend\Block\Widget\Form\Container

And is this the cleanest way to accomplish this in Magento 2?

5

The problem is that Magento\Cms\Block\Adminhtml\Page\Edit { is using the namespace you have declared Magento\Cms\Block\Adminhtml\Page\Edit so becomes Company\Modulename\Block\Adminhtml\Page\Magento\Cms\Block\Adminhtml\Page\Edit

Your missing a \ from the front of the extended class so that it doesn't use the current namespace - you should then have

class Edit extends \Magento\Cms\Block\Adminhtml\Page\Edit {

That fixes the error - but there may still be a better way to do this.

  • Just missed one backslash, thank you! Any suggestions to do this in a better way are welcome! – Roy Nov 13 '15 at 15:05
4

It is better to use plugins instead of declaring preference in your case. The main benefit is that there will be no conflicts if another extension will try to modify/extend the same functionality. Keep in mind that there can be only one active preference for each class/interface, priority is defined according to sequence in the final merged module.xml

Preferences should be used when there is a need to provide custom implementation of core interface.

  • That looks nice! I'll dig into that. Thanks! – Roy Nov 13 '15 at 15:24
3

Can be done with a plugin on \Magento\Cms\Block\Adminhtml\Page\Edit using beforeSetLayout since there is in \Magento\Backend\Block\Widget\Container this:

protected function _prepareLayout()
{
    $this->toolbar->pushButtons($this, $this->buttonList);
    return parent::_prepareLayout();
}

In method beforeSetLayout use $subject->addButton().

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.