cancel
Showing results for 
Search instead for 
Did you mean: 

Button in system.xml not working properly

Button in system.xml not working properly

Hey, I've created a button that shows in a tab, but it does nothing, here's what I've done

 

Custom/ExportSubs/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="export" translate="label" sortOrder="1000">
            <label>Subscribers</label>
        </tab>
        <section id="export" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Export</label>
            <tab>export</tab>
            <resource>Custom_ExportSubs::export</resource>
            <group id="export2" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Exp2</label>
                <field id="export2" translate="label comment tooltip" type="button" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Export CSV</label>
                    <frontend_model>Custom\ExportSubs\Block\System\Config\Button</frontend_model>
                </field>
            </group>
            <group id="export1" translate="label" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Exp1</label>
                <field id="export1" translate="label comment tooltip" type="button" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Export CSV</label>
                    <frontend_model>Custom\ExportSubs\Block\System\Config\Button</frontend_model>
                </field>
            </group>
        </section>
    </system>
</config>

Custom/ExportSubs/Block/System/Config/Button.php

<?php
namespace Custom\ExportSubs\Block\System\Config;
class Button extends \Magento\Config\Block\System\Config\Form\Field
{
     protected $_template = 'Custom_ExportSubs::system/config/button.phtml';

     public function __construct(        \Magento\Backend\Block\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

     public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
        return parent::render($element);
    }
       protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        return $this->_toHtml();
    }
    public function getAjaxUrl()
    {
        return $this->getUrl('system_config/button');
    }
    public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock(
            'Magento\Backend\Block\Widget\Button'
        )->setData(
            [
                'id' => 'btnid',
                'label' => __('EXPORT'),
            ]
        );

        return $button->toHtml();
    }
}

Custom/ExportSubs/view/adminhtml/templates/system/config/button.phtml

<script>require([
    'jquery',
    'prototype',
], function(jQuery){
    function function_name() {
        params = {
        };

        new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
            loaderArea:     false,
            asynchronous:   true,
            parameters:     params,
            onSuccess: function(transport) {
                var response = JSON.parse(transport.responseText);
            }
        });
    }

    jQuery('#btnid').click(function () {
        function_name ();
    });
});
</script>

<?php echo $block->getButtonHtml() ?>

Custom/ExportSubs/controller/Adminhtml/System/Config

<?php
namespace Custom\ExportSubs\Controller\Adminhtml\System\Config;

use \Magento\Catalog\Model\Product\Visibility;

class Button extends \Magento\Backend\App\Action
{
    protected $_logger;
    public function __construct(        \Magento\Backend\App\Action\Context $context,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;
        parent::__construct($context);
    }
    public function execute()
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/exporttest.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Teste');
        // Code to perform specific action    
    }
}

This is what I've done so far following tutorials, but I can't get it to work properly, there was some parts I didn't understand quite good, so I think it's probably something I did wrong, just don't know what.

 

Thanks in advance.

Best regards,

Rui Silva