7

I want to remove the Create Order button from the admin customer edit form and I don't know how to do this via a plugin or an event.

The button is added from Magento\Customer\Block\Adminhtml\Edit::_construct and I cannot write a plugin for a protected method.
Is there an event, or a public method that is called after the buttons are added and before the layout is rendered that I can use?

4 Answers 4

7

After test I think it work with Magento\Customer\Block\Adminhtml\Edit\OrderButton and not Magento\Customer\Block\Adminhtml\Edit it has public function:

public function getButtonData()
{
    $customerId = $this->getCustomerId();
    $data = [];
    if ($customerId && $this->authorization->isAllowed('Magento_Sales::create')) {
        $data = [
            'label' => __('Create Order'),
            'on_click' => sprintf("location.href = '%s';", $this->getCreateOrderUrl()),
            'class' => 'add',
            'sort_order' => 40,
        ];
    }
    return $data;
}

you can use plugin is public function

1
  • 1
    You are right. I can pluginize that method using around plugin and make my method return null. This makes the button go away. Thanks.
    – Marius
    Jan 8, 2016 at 14:26
7

There is no need to write plugin for class which is configured via UI Component configuration file.

There is a app/code/Magento/Customer/view/base/ui_component/customer_form.xml file with array of buttons including Create Order Buton:

 <item name="order" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\OrderButton</item>

In case this item is removed from an array the form will be rendered without "Create Order" button. In your custom module customer_form.xml file has to be prepared.

    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
    ...
    <item name="buttons" xsi:type="array">
        <item name="back" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\BackButton</item>
        <item name="delete" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\DeleteButton</item>
        <item name="invalidateToken" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\InvalidateTokenButton</item>
        <item name="resetPassword" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\ResetPasswordButton</item>
        <item name="reset" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\ResetButton</item>
        <item name="save" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\SaveButton</item>
        <item name="save_and_continue" xsi:type="string">Magento\Customer\Block\Adminhtml\Edit\SaveAndContinueButton</item>
    </item>
    ...
</argument>
4
  • 1
    Thanks. This works nicely. +1. I still orefer the plugin approach because even if by some miracle someone changes the admin theme, the button is still removed.
    – Marius
    Jan 8, 2016 at 17:09
  • 1
    Marius, the OrderButton class should not be instantiated in case you do not want to see the button. With the plugin there are 2 classes will be instantiated to perform the action.
    – Max Pronko
    Jan 8, 2016 at 17:59
  • 1
    Hmmm...you have a valid point...i will make up my mind on monday
    – Marius
    Jan 8, 2016 at 19:22
  • not working in magento 2.0.16 Jan 3, 2018 at 14:19
4

You can easily remove one of the buttons added into vendor/magento/module-customer/view/base/ui_component/customer_form.xml, declaring a new customer_form.xml file into a custom module with the following path:

app/code/VendorName/ModuleName/view/base/ui_component/customer_form.xml

and the content will be:

<?xml version="1.0" encoding="UTF-8"?>

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="buttons" xsi:type="array">
            <item name="order" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="componentDisabled" xsi:type="boolean">true</item>
                </item>
            </item>
        </item>
    </argument>
</form>
1

I came across this issue and ended up solving it a different way. I wrote up a class NullButton extending button where getButtonData() returns an empty array. Then I used a custom xml layout file to assign this class to the offending button. The original layout xml is still being used but that single button is replaced with nothing.

My use case was for a form from a purchased extension but it should work the same for core magento too.

Vendor/Module/view/adminhtml/ui_component/order_export_profile_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
  <argument name="data" xsi:type="array">
    <item name="buttons" xsi:type="array">
      <item name="save_and_run" xsi:type="string">Vendor\Module\Block\Adminhtml\Edit\Button\NullButton</item>
    </item>
  </argument>
</form>    

Vendor\Module\Block\Adminhtml\Edit\Button\NullButton.php

<?php

namespace Vendor\Module\Block\Adminhtml\Edit\Button;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
 * Used as a placeholder for where a button class is expected
 * but not be expressed in the resulting html. To fully
 * remove existing button, set this as the Block.
 */
class NullButton implements ButtonProviderInterface
{
    /**
     * @return array
     */
    public function getButtonData()
    {
        return [];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.