Magento - Add a button to system.xml with method attached to it

Magento - Add a button to system.xml with method attached to it

July 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Add a button to system.xml with method attached to it

Running Magento Cron Jobs On Demand: Attaching Methods to System.xml Buttons

As an experienced Magento developer, I often encounter situations where clients want more control over cron jobs—specifically, the ability to run them on demand via the admin panel. In this post, we’ll explore how to add a “Run now” button in your system configuration file (system.xml) and attach a specific method from your module to that button.

The Challenge: Attaching Methods to Buttons in System.xml

The default Magento setup allows you to create buttons using the frontend_type attribute in your system.xml file, but it lacks built-in functionality for attaching methods or adding labels/values. This limitation often leads developers like yourself to resort to custom solutions that aren’t ideal from an ACL perspective.

A Closer Look at Your Current Setup

You’ve already attempted to create a button using the button frontend type in your system.xml file:

<run translate="label">
  <label>Run now</label>
  <frontend_type>button</frontend_type>
  <backend_model>SOME BACKEND MODEL</backend_model>
  <sort_order>20</sort_order>
  <show_in_default>1</show_in_default>
  <show_in_website>1</show_in_website>
  <show_in_store>1</show_in_store>
</run>

While this sets up a clickable button, it doesn’t specify how to handle the click event or what method should be triggered.

The Solution: Utilizing the Config Field for JavaScript Injection

To overcome this limitation, we’ll use a workaround involving injecting custom JavaScript into your config field. This script will be responsible for handling the button’s click event and triggering the desired method in your module.

Here’s how you can achieve this:

  1. Create a new configuration field in your system.xml file to store the JavaScript code. For example, add the following section before or after your existing “run” field:
<javascript_code translate="label">
  <label>JavaScript Code</label>
  <frontend_type>text</frontend_type>
  <backend_model>SOME_BACKEND_MODEL</backend_model>
  <sort_order>21</sort_order>
  <show_in_default>1</show_in_default>
  <show_in_website>1</show_in_website>
  <show_in_store>1</show_in_store>
</javascript_code>

Replace SOME_BACKEND_MODEL with the appropriate backend model for your text field.

  1. Customize your JavaScript to handle button click events and trigger a method in your module. For instance, you can use jQuery to bind a click event listener to the “Run now” button and then send an AJAX request to a specific controller action:
function runExport() {
  $.ajax({
    type: 'POST',
    url: '{{blockType=SOME_CONTROLLER_PATH}}',
    data: {action: 'run_export'},
    success: function(data) {
      // Handle successful response
    },
    error: function(jqXHR, textStatus, errorThrown) {
      // Handle errors
    }
  });
}

$(document).ready(function() {
  $('#button_run').click(function(e) {
    e.preventDefault();
    runExport();
  });
});

Replace SOME_CONTROLLER_PATH with the path to your controller that contains the run_export() method you want to trigger.

  1. Inject the JavaScript into the config field using a Magento event observer:
public function injectJavascriptCode(Observer $observer)
{
    $transport = $observer->getTransport();
    $transport->setHtml(
        str_replace('</head>', $this->getJavaScriptCode() . '</head>', $transport->getHtml())
    );
}

private function getJavaScriptCode()
{
    return '<script type="text/javascript">
              // Your custom JavaScript code here
             </script>';
}
  1. Create the controller method to handle the AJAX request:
public function executeRunExportAction()
{
    // Implement your export logic here

    return $this->getResponse()->setContents(json_encode(['success' => true]));
}

Conclusion: Achieving On-Demand Cron Jobs in Magento

By following this approach, you can now add a “Run now” button to your system configuration and attach it to any desired method in your module. This workaround leverages the flexibility of custom JavaScript injection within the admin panel.

Remember that proper error handling and security measures should be implemented when working with AJAX requests and sensitive data.

For more advanced techniques and best practices in Magento development, I recommend exploring our comprehensive guides on Laravel Company.

Extensions Admin Configuration Cron

Our network