2

Basically I want to add button in system.xml file that is linked to google home page in system.xml file.

Does anyone know how to solve it? Thanks in advance.

3

You need to create the system.xml file under

app/code/Vendor/Module/etc/adminhtml/system.xml

and use the code below:

<?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>
        <section id="section_id" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
            <label>Section Label</label>
            <tab>tab_id</tab>
            <group id="group_id" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Group Label</label>
                <field id="field_id" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Field Label</label>
                    <frontend_model>Vendor\Module\Block\Adminhtml\System\Config\Button</frontend_model>
                </field>
            </group>
        </section>
    </system>
</config>

You need to create the Button.php file under

app/code/Vendor/Module/Block/Adminhtml/System/Config/Button.php

and use the code below:

<?php
namespace Vendor\Module\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class Button extends Field
{
    /**
     * Get the button Run
     *
     * @param AbstractElement $element
     * @return string
     */
    protected function _getElementHtml(AbstractElement $element)
    {
        $url = "'https://www.google.com'";
        return '<button onclick="location.href=' . $url . '" type="button">' . __('Google') . '</button>';
    }
}
?>

Please run below command for display changes :

php bin/magento cache:flush

Hope this will help you!

  • button click is not working .it dispaly sentex error in console – usman Nov 5 at 10:47
  • Answer updated @usman – Naresh Rupareliya NR Nov 5 at 11:00
2

You can add these lines in your system.xml file here in your custom module

app/code/Vendor/Module/etc/adminhtml/system.xml

You can add field like this..

<field id="button" translate="label" type="label" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
    <label>Custom Button</label>
    <frontend_model>Vendor\Module\Block\Adminhtml\System\Config\Form\Field\Button</frontend_model>
</field>

You need to create one Block file now here on this path in your custom module

app/code/Vendor/Module/Block/Adminhtml/System/Config/Form/Field/Button.php

Content for this file is..

<?php
namespace Vendor\Module\Block\Adminhtml\System\Config\Form\Field;

class Button extends \Magento\Config\Block\System\Config\Form\Field
{
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if (!$this->getTemplate()) {
            $this->setTemplate('system/config/fieldset/custom_button.phtml');
        }

        return $this;
    }

    protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        return $this->_toHtml();
    }
}

Now you finally need to create one template file in your custom module here on this path..

app/code/Vendor/Module/view/adminhtml/templates/system/config/fieldset/custom_button.phtml

Content for this file is..

<input type="button" name="google_button" id="google_button" value="Google Button">

<script type="text/javascript">  
(function  () {    
    require(["jquery"], function($) {
        $(document).ready(function($) {
            $("#google_button").on("click", function(e){
                window.open('https://www.google.com/', '_blank');
            })
        }); 
    });
})();
</script>

Hope this will work for you!

  • Approve answer if that is helpful to you! Thanks. :) – Kishan Savaliya Nov 6 at 5:02

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.