cancel
Showing results for 
Search instead for 
Did you mean: 

how do I use button_url and button_label in system.xml

how do I use button_url and button_label in system.xml

I've been reading the devdocs about button_url and button_label and I haven't seen a solid example implementing those in system.xml

https://devdocs.magento.com/guides/v2.3/config-guide/prod/config-reference-systemxml.html#field-node...

 

In the docs it says 

Displays a button if button_url and button_label are specified. 
Usually used in combination with a custom frontend model.

Now I've been reading on the various websites how to create that button with a custom frontend model but I'm unsure about how to get these values from the configuration field data.Most tutorials out there show that we extend

\Magento\Config\Block\System\Config\Form\Field

In our custom Button block and then we render our own template.

 

However on our template those values are not accessible. Is there an example on how these two options work?

 

 

 

 

 

3 REPLIES 3

Re: how do I use button_url and button_label in system.xml

Hi @iowebgr 

Follow the below blog, might be it help you!

add-a-button-in-magento-2-stores-configuration-with-custom-action/ 

Re: how do I use button_url and button_label in system.xml

Hi, yes I have followed this tutorial and from mageplaza and I can successfully add a button like that, thanks.

 

However, my question is specifically about the options referenced in the devdocs and how they are used to create a button.

 

I mean, they are there, but how are they used? If they're not supposed to be used, why are they there in the first place?

Re: how do I use button_url and button_label in system.xml

If anyone is still looking for the solution to this:

$element->getData('field_config/button_url')
$element->getData('field_config/button_label')

Following along with that MageComp tutorial, this is what I'm doing to access those values in the template:

public function render(AbstractElement $element)
{
// Set the data here to be available to the template $this->setData($this->getViewData($element)); $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); return parent::render($element); } private function getViewData(AbstractElement $element) : array {
// Get all the data that you want from the system field config return [ 'buttonLabel' => $element->getData('field_config/button_label'), 'buttonUrl' => $element->getData('field_config/button_url') ]; } public function getButtonHtml() {
// Get the data that we set in the render function $button = $this->getLayout()->createBlock( 'Magento\Backend\Block\Widget\Button' )->setData( [ 'id' => $this->getData('buttonId'), 'label' => __('Button_Name'), ] ); return $button->toHtml(); }

In the .phtml file:

$block->getData('buttonLabel');