Magento – How to add a button to the Customer Edit Page within the admin

adminhtmlcustom-buttoncustomermagento2

I've been working on a module to put a custom button, that opens a popup for input values, onto the Customer Edit page (customer/index/edit/id/) within the admin:

enter image description here

I've been using this for the module.xml :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Foo_CustomerResetPass" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

This for the Block :

<?php

namespace Foo\CustomerResetPass\Block\Adminhtml\Customer\Edit;

use Magento\Customer\Block\Adminhtml\Edit\GenericButton;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
 * Set Cust Pass button
 */
class Login extends GenericButton implements ButtonProviderInterface
{
    /**
     * @var \Magento\Framework\AuthorizationInterface
     */
    protected $_authorization;

    /**
     * Constructor
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param AccountManagementInterface $customerAccountManagement
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Framework\Registry $registry
    ) {
        parent::__construct($context, $registry);
        $this->_authorization = $context->getAuthorization();
    }

    /**
     * @return array
     */
    public function getButtonData()
    {
        $customerId = $this->getCustomerId();
        $data = [];
        $canModify = $customerId && $this->_authorization->isAllowed('Foo_CustomerResetPass::reset_button');
        if ($canModify) {
            $data = [
                'label' => __('Set Customer Password'),
                'class' => 'reset reset-button',
                'on_click' => 'window.open( \'' . $this->getInvalidateTokenUrl() .
                    '\')',
                'sort_order' => 80,
            ];
        }
        return $data;
    }

    /**
     * @return string
     */
    public function getInvalidateTokenUrl()
    {
        return $this->getUrl('/foo/reset.php', ['customer_id' => $this->getCustomerId()]);
    }

}

And this is for the index.xml under view/adminhtml/layout

<?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="resetpass_login_grid_block"/>
    <body>
        <referenceContainer name="content">
            <block class="Foo\CustomerResetPass\Block\Adminhtml\ResetPass" name="resetpass.container"/>
        </referenceContainer>
    </body>
</page>

Any help or if anyone could point me in the right direction it would be greatly appreciated and thanks in advance!

Best Answer

Ok ... I found an answer create xml file on /view/adminhtml/ui_component/customer_form.xml

<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="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">customer_form.customer_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Customer Information</item>
        <item name="reverseMetadataMerge" xsi:type="boolean">true</item>
    </argument>
    <settings>
        <buttons>
            <button name="add_sms" class="Netweb\Sms\Block\Adminhtml\SmsButton"/>
        </buttons>
    </settings>
</form>

and one block

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

class SmsButton   implements ButtonProviderInterface
{
    /**
     * @return array
     */
    public function getButtonData()
    {
        $url="your url";
        $data = [
            'label' => __('send sms'),
            'on_click' => sprintf("location.href = '%s';", $url),
            'class' => 'add',
            'sort_order' => 40,
        ];

        return $data;
    }
}