3

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!

  • Hey did you found any way ? – gh darvishani Dec 16 '17 at 13:04
4

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;
    }
}
  • 1
    Thanks for the information! It got me pretty close. The xml was similar, just needed to change the buttons to items and slightly change the argument, then put it in the xml file/directory you gave me and it popped right up. – Rich Dec 18 '17 at 21:06
  • Thanks @gh darvishani ! I'm looking for the equivalent in XML in the case of \Magento\Backend\Block\Widget\Container where there is no UI component I think. I'm trying to add a button before Flush Cache Storage in Cache Management, but in Cache block it's done with the method $this->buttonList->add(... I though XML would get me where I wanted without having to override cache block, is it still possible ? thank you ! (does this matter deserve it's own question?) – medmek Feb 15 at 15:51
  • @gh how to show confirmation box before action – sumeet bajaj May 1 at 10:50

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.