I Created Custom IconSlider Module That Can Be controlled from Store-Configuration.
Each slider Have the Same 3 Fields (Name,URL,Image).
i added a few groups With that fields But every time i want to add 1 more slider i need to copy and past a new group with the same fields and thats not really smart..
So i manage to add new field type = "Button" that show at the bottom of the groups.
what i am trying to do is When i click on this button that he will add automaticlly a New Group With Empty Fields(Name,URL,Image).
etc/adminhtml/System.xml
<field id="AddNewFields" translate="label" type="button" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>Vendor\Module\Block\System\Config\Button</frontend_model>
</field>
Vendor\Module\Block\System\Config\Button.php
<?php
namespace Vendor\Module\Block\System\Config;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form\Element\AbstractElement;
class Button extends Field
{
protected $_template = 'Vendor_Module::System/Config/Button.phtml';
public function __construct(Context $context, array $data = [])
{
parent::__construct($context, $data);
}
public function render(AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
protected function _getElementHtml(AbstractElement $element)
{
return $this->_toHtml();
}
public function getCustomUrl()
{
return $this->getUrl('router/controller/action');
}
public function getButtonHtml()
{
$button = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')->setData(['id' => 'CustomButtonAddFields', 'label' => __('Add New Icon Slider'),]);
return $button->toHtml();
}
public function addCustomfield(Magento\Framework\Data\Form\Element\Fieldset $fieldset)
{
}
}
Vendor_Module/view/adminhtml/templates/System/Config/Button.phtml
<?php
/* @var \Vendor\Module\Block\System\config\Button $block */
$controller = $block->getCustomUrl();
echo $block->getButtonHtml();
?>
<script>
require([
'jquery'
], function($){
$('#CustomButtonAddFields').on('click', function() {
alert('Clicked!~');
})
});
As You Can See I Created A New Function At Vendor\Module\Block\System\Config\Button.php Called addCustomfield And I want To Activate This Function From The Button.Phtml OnClick Event.
Thats How Its Look In The Admin Panel :
Click Here : Preview
so what Should I Do In This addCustomfield Function ?
Ty .