6

I have set dropdown in form.xml like

<argument name="data" xsi:type="array">
    <item name="options" xsi:type="string">Book\Flip\Model\Category\Attribute\Source\Category</item>
    <item name="config" xsi:type="array">
        <item name="dataType" xsi:type="string">text</item>
        <item name="label" translate="true" xsi:type="string">category</item>
        <item name="formElement" xsi:type="string">select</item>
        <item name="source" xsi:type="string">flip</item>
        <item name="sortOrder" xsi:type="number">20</item>
        <item name="dataScope" xsi:type="string">category</item>
        <item name="validation" xsi:type="array">
            <item name="required-entry" xsi:type="boolean">false</item>
        </item>
    </item>
</argument>

and create model

namespace Book\Flip\Model\Category\Attribute\Source;

    class Category extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
    {
        protected $_optionsData;

        public function __construct(array $options)
        {
            $this->_optionsData = $options;
        }

        public function getAllOptions()
        {
            if ($this->_options === null) {
                $this->_options = [
                    ['value' => '1', 'label' => __('science')]
                ];
            }
            return $this->_options;
        }
    }

But in admin form it's shown a blank dropdown.

2

4 Answers 4

8

Change Category class by following code:

namespace Book\Flip\Model\Source;

class Category implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * Retrieve options array.
     *
     * @return array
     */
    public function toOptionArray()
    {
        $result = [];

        foreach (self::getOptionArray() as $index => $value) {
            $result[] = ['value' => $index, 'label' => $value];
        }

        return $result;
    }

    /**
     * Retrieve option array
     *
     * @return string[]
     */
    public static function getOptionArray()
    {
        return [1 => __('science')];
    }

    /**
     * Retrieve option array with empty value
     *
     * @return string[]
     */
    public function getAllOptions()
    {
        $result = [];

        foreach (self::getOptionArray() as $index => $value) {
            $result[] = ['value' => $index, 'label' => $value];
        }

        return $result;
    }

    /**
     * Retrieve option text by option value
     *
     * @param string $optionId
     * @return string
     */
    public function getOptionText($optionId)
    {
        $options = self::getOptionArray();

        return isset($options[$optionId]) ? $options[$optionId] : null;
    }
}
2
  • i get this error Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Framework\View\Element\UiComponentFactory::argumentsResolver() must be of the type array, null given, called in /var/www/html/codility-magento-market-place/vendor/magento/framework/View/Element/UiComponentFactory.php on line 206 and defined in /var/www/html/codility-magento-market-place/vendor/magento/framework/View/Element/UiComponentFactory.php:172
    – HaFiz Umer
    Commented Jul 23, 2019 at 14:49
  • How to pass block file in magento 2.3.1 @Sohel Rana Commented Dec 4, 2020 at 15:00
6

I assume you found this out by now, but the xml in the form ui

<item name="options" xsi:type="string">Book\Flip\Model\Category\Attribute\Source\Category</item>

belongs under data as a sibling of config not a child, and its xsi:type should be "object"

Like this

<argument name="data" xsi:type="array">
    <item name="options" xsi:type="object">Book\Flip\Model\Category\Attribute\Source\Category</item>
    <item name="config" xsi:type="array">
        <item name="dataType" xsi:type="string">text</item>
        <item name="label" translate="true" xsi:type="string">category</item>
        ...

I also was stuck on this issue for awhile until I figured out that bit.

1
  • I see this as the common way to specify a source model; however, is there documentation to support this? I am confused as to what this means for column eav_attribute.source_model -- moreover, the UI definition for a similar attribute, landing_page, does not have such arguments and yet it works. Looking for some authoritative answers. Commented Feb 26, 2018 at 17:37
4
class category implements \Magento\Framework\Option\ArrayInterface
{ 
    //Below function is supposed to return options.
    public function toOptionArray()
    {
        return [
            ['value' => 1, 'label' => 'label1'],
            ['value' => 2, 'label' => 'label2']
        ];
    }
}
1
  • 2
    Tried your method but it does not seem to work. Please Update to ``` public function toOptionArray() { return [ ['value' => 1, 'label' => 'label1'], ['value' => 2, 'label' => 'label2'] ]; } ```
    – Ervin
    Commented Mar 18, 2017 at 13:00
1

If you need to add an extra select field to an already existing form not built with ui-components and if you need to apply to that field a custom model config source you can use this code:

         $fieldset->addField(
            'x_custom_slideshow',
            'select',
            [
                'name' => 'brand_custom_slideshow',
                'label' => __('Choose slideshow'),
                'title' => __('Choose slideshow'),
                'required' => false,
                'values' => $sliderConfig->toOptionArray()
            ]
        );

after you previuosly specified/loaded the model config class via contructor or via object manager for eg.

$sliderConfig = \Magento\Framework\App\ObjectManager::getInstance()->get('Namespace\Slideshow\Model\Config\Source\Slideshow');

I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.