Passing a Helper Class a Parameter Through a Layout XML File

Passing a Helper Class a Parameter Through a Layout XML File

September 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Passing a Helper Class a Parameter Through a Layout XML File

Passing Parameters to Helper Classes in Layout XML Files

As a developer working with Magento 2 layouts, you might find yourself needing to pass parameters to helper classes within your XML layout files. This is especially useful when you want to create dynamic URLs or retrieve configurable values based on specific conditions.

Currently, the default approach allows you to call static methods of helper classes without any parameters. However, if you need to send a parameter to a helper class for processing, it requires a slightly different approach.

Understanding the Limitations

By default, when you use a layout XML file, you’re working within a very limited scope. The XML parser doesn’t support passing complex data structures or parameters directly into PHP functions called via XML tags. It only supports simple string labels and URLs.

To overcome this limitation, we need to find an alternative way to communicate with our helper classes from the XML layout. This is where event observers come into play.

Using Event Observers for Parameter Passing

Event observers are a powerful feature in Magento 2 that allows you to intercept certain events triggered by the system and perform custom actions or data manipulations before, during, or after these events occur.

For your specific use case, you can create an event observer that listens for the layout processing event (layout_generate_blocks_after). In this observer, you can process the URL data passed from the XML layout and then send it to your helper class as a parameter.

Here’s an example of how to implement this:

  1. Create a new file called Vendor_Module/Observer/SendUrlToHelper.php in your module directory:
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http as Request;
use Magento\Framework\DataObject;

class SendUrlToHelper implements ObserverInterface
{
    protected $request;

    public function __construct(
        Request $request
    ) {
        $this->request = $request;
    }

    public function execute(Observer $observer)
    {
        $layout = $observer->getLayout();
        $actionName = $layout->getUpdate()->getAppAction();

        if ($actionName === 'test_action') { // Replace with your desired action name
            $url = $this->request->get('url');
            $helper = \Magento\Framework\App\ObjectManager::getInstance()->create('\Vendor\Module\Helper\Data');

            $resultUrl = $helper->getExternalSiteUrl($url);

            // Now you can use the $resultUrl variable in your layout
        }
    }
}
  1. Add the observer to your module’s events.xml file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_generate_blocks_after">
        <observer name="vendor_module_send_url_to_helper" instance="Vendor\Module\Observer\SendUrlToHelper"/>
    </event>
</config>

Note that in the above example, we’re using a simple URL parameter passed via the request. However, you can modify this approach to suit your specific needs by processing other data structures or retrieving values from the layout block.

Conclusion

Passing parameters to helper classes from layout XML files requires a creative workaround involving event observers and custom logic within these observers. By listening for the appropriate layout processing events, you can intercept the necessary data, process it as required, and then pass it to your helper class for further manipulation or URL generation.

This approach allows you to maintain the flexibility of using static helper methods while still being able to handle dynamic parameters based on specific conditions or user input.

Magento 2 Extensions Themes Configuration

Our network