Hello Everyone,
In this blog, we will learn about how to open Popup on Button Click in System Configuration in Magento 2.
Magento 2 provides more customization options, one is open popup on button click of System Configuration field.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Open Popup on Button Click in Magento 2.
STEPS FOR OPEN POPUP ON BUTTON CLICK IN ADMIN IN MAGENTO 2
Step 1: Create system.xml file
app/code/Vendor/Extension/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="magecurious" class="magecurious" translate="label" sortOrder="10">
<label>Magecurious</label>
</tab>
<section id="extension" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Magecurious</label>
<tab>magecurious</tab>
<resource>Vendor_Extension::config_extension</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Extension</label>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="clickpopup" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Click Button Popup</label>
<frontend_model>Vendor\Extension\Block\Adminhtml\System\Config\Popup</frontend_model>
</field>
</group>
</section>
</system>
</config>
Step 2: Create Popup.php file
app/code/Vendor/Extension/Block/Adminhtml/System/Config/Popup.php
<?php
namespace Vendor\Extension\Block\Adminhtml\System\Config;
class Popup extends \Magento\Config\Block\System\Config\Form\Field
{
protected $_template = 'system/config/popupbutton.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 getButtonHtml()
{
$button = $this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Button'
)->setData(
[
'id' => 'popupbutton',
'label' => __('Magecurious Popup'),
'class' => 'primary'
]
);
return $button->toHtml();
}
}
Step 3: Create pop up button.phtml file
app/code/Vendor/Extension/view/adminhtml/system/config/popupbutton.phtml
<div id="messages">
<div class="message"> MessageContents
<button class="primary">Click Me</button>
</div>
</div>
<script type="text/javascript">// <![CDATA[
require([
'jquery',
'Magento_Ui/js/modal/alert'
],
function($, alert) {
$('#messages').on('click', 'button.primary', function(event){
alert({
content: $(event.target).parent().text()
})
})
}
);
// ]]></script>
</div>
Step 4: Finally run the below commands
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 5: Output:
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Add Popup on Button Click in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
