Skip to content

How to pass options from class to admin form ui component checkboxset

I have an admin ui component form, one of the elements I’m using is checkboxset. The checkboxset seems be a bit of a ghost in core code, and documentation.

When I implement the checkboxset like this (manual adding options like https://developer.adobe.com/commerce/frontend-core/ui-components/components/checkbox-set/) everything works:

        <checkboxset name="checkboxset_example">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="additionalInfo" xsi:type="string">Additional information</item>
                </item>
            </argument>
            <settings>
                <label translate="true">Checkboxset Component Example</label>
                <options>
                    <option name="0" xsi:type="array">
                        <item name="value" xsi:type="number">1</item>
                        <item name="label" xsi:type="string" translate="true">Option #1</item>
                    </option>
                    <option name="1" xsi:type="array">
                        <item name="value" xsi:type="number">2</item>
                        <item name="label" xsi:type="string" translate="true">Option #2</item>
                    </option>
                    <option name="2" xsi:type="array">
                        <item name="value" xsi:type="number">3</item>
                        <item name="label" xsi:type="string" translate="true">Option #3</item>
                    </option>
                </options>
            </settings>
        </checkboxset>

What I’d like to do is something like the below, where I can pass options from a class:

        <checkboxset name="checkboxset_example">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataScope" xsi:type="string">checkboxset_example</item>
                    <item name="options" xsi:type="array">VendorModuleModelSourceStoreOptions</item><!-- this doesn't work -->
                </item>
            </argument>
            <settings>
                <label translate="true">Checkboxset Component Example</label>
            </settings>
        </checkboxset>

The class I’m using is like this, but it’s never instantiated and isn’t being used. I can only get the value for the options in the prepare() method of magentoecevendormagentomodule-uiComponentFormElementAbstractOptionsField.php to come back with an empty array, or the entire object of the options class I’m trying to use.

use MagentoFrameworkDataOptionSourceInterface;

class Options implements OptionSourceInterface
{
    public function toOptionArray(): array
    {
        return [
            ['value' => "1", 'label' => __('One')],
            ['value' => "2", 'label' => __('Two')]
            ['value' => "2", 'label' => __('Three')]
        ];
    }
}

Ideally, I’d like get a list of websites with ids (not a hard coded array), as this instance of Adobe Commerce has multiple websites.