Attaching values ​in a Magento2 XML type input

Attaching values ​in a Magento2 XML type input

October 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Attaching values ​in a Magento2 XML type input

Attaching Values in Magento2 XML Type Input: A Comprehensive Guide

If you’re working on customizing your Magento2 UI forms using XML type inputs but struggling to attach values successfully, you’ve come to the right place. In this guide, we’ll walk through a practical solution step-by-step, ensuring you understand the process thoroughly.

The Problem at Hand

You tried adding a value in your UI form using an XML type input but found that the methods you attempted didn’t yield the desired results. You’re stuck with a snippet of code that looks something like this:

<field name="client_id">
    <argument name="data" xsi:type="array">
        <!-- ... various items here ... -->
    </argument>
</field>

Additionally, your custom class is structured as follows:

namespace Module\Core\DataObjects;

use Magento\Framework\App\ObjectManager;
use Module\Core\Model\SettingsFactory;

class SettingsData
{
    // constructor and methods as described in the problem statement
}

Despite trying several approaches, including attaching the class name to the “class” attribute of the XML field definition, you weren’t able to successfully attach the value from your custom class.

The Solution: A Step-by-Step Guide

To achieve this, we’ll need to make a few adjustments to both your XML definition and your custom class. Let’s dive into the details:

Step 1: Adjusting the XML Field Definition

The secret lies in properly configuring the “source” attribute within your field argument.

<field name="client_id">
    <argument name="data" xsi:type="array">
        <!-- ... various items here ... -->
        <item name="source" xsi:type="string">Module\Core\DataObjects\SettingsData</item>
    </argument>
</field>

Notice the change made to the “source” attribute. It now references your custom class directly, using its full namespace and class name.

Step 2: Adjusting Your Custom Class

Next, we need to make sure your custom class is properly set up to handle the data retrieval.

namespace Module\Core\DataObjects;

use Magento\Framework\App\ObjectManager;
use Module\Core\Model\SettingsFactory;

class SettingsData implements \Magento\Framework\DataObject\IdentityInterface
{
    // constructor and methods as described in the problem statement

    public function getIdentities()
    {
        return [settings::CACHE_TAG . '_' . $this->getClientId()];
    }

    public static function cacheTag()
    {
        return settings::CACHE_TAG;
    }
}

The key change here is the implementation of the Magento\Framework\DataObject\IdentityInterface and the addition of a getIdentities() method.

Step 3: The Magic Ingredient

We’re almost there! The next step involves setting up the appropriate cache management for your data object. This is crucial for Magento2 to recognize and use the values from your custom class in the UI form.

To achieve this, you’ll need to create a separate file (e.g., SettingsData.php) in your module’s etc directory. Inside this file, define your cache settings:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <Module_Core>
            <settings_data_cache_lifetime>86400</settings_data_cache_lifetime>
            <settings_data_cache_tags>
                <settings>
                    Module/Core
                </settings>
            </settings_data_cache_tags>
        </Module_Core>
    </default>
</config>

This configuration ensures that Magento2 recognizes your data object as cacheable and associates it with the correct cache tag.

Putting It All Together

With these adjustments in place, you should now be able to successfully attach values from your custom class to the XML type input in your Magento2 UI form. Remember, patience and attention to detail are key when working with Magento2’s complex configuration system.

If you’re looking for more in-depth guides on Magento development, I highly recommend checking out Laravel Company, a leading provider of comprehensive Magento services and resources.

Conclusion

In this blog post, we’ve explored the intricacies of attaching values to Magento2 XML type inputs. By adjusting your XML field definition, custom class, and cache management settings, you can now successfully integrate data from your custom classes into your UI forms.

Remember, the key to success in any development project lies in understanding the underlying architecture and making targeted, well-informed changes. With this guide as a reference, you’re well-equipped to tackle similar challenges in your future Magento2 projects.

Happy coding!

Magento 2 Extensions Configuration Errors

Our network