Domanda

I have a custom form like this. how can I get all product attribute and Pour it into the select option in the job field. Hope anyone help me. Thank you. enter image description here

È stato utile?

Soluzione

Please check this code to add select option field by ui-component

in your ui-component xml file add code like this.

<field formElement="select" name="status" sortOrder="40">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Vendor\ModuleName\Model\Config\Source\JobOptions</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Job</item>
                    <item name="source" xsi:type="string">ModuleName</item>
                    <item name="dataScope" xsi:type="string">job</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

Then create file JobOptions.php in Vendor\ModuleName\Model\Config\Source , its defined in ui-component xml.

and add below code to get Product attributes in dropdown.

<?php 
namespace Vendor\ModuleName\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class JobOptions implements ArrayInterface
{
    /**
     * @return array
     */

    protected $_attributeFactory;

    public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeFactory

    ){
        $this->_attributeFactory = $attributeFactory;
    }

    public function toOptionArray()
    {
        $attribute_data = [];
        $attributeInfo = $this->_attributeFactory->create();
        foreach ($attributeInfo as $items) {
            $attribute_data[] = $items->getData();
        }

        $attributeOptions = [];
        $i = 0;
        foreach ($attribute_data as $attribute) {
            $attributeOptions[$i]['label'] = $attribute['frontend_label'];
            $attributeOptions[$i]['value'] = $attribute['attribute_id'];
            $i++;
        }
        return $attributeOptions;
    }
}

check this screenshot

enter image description here

Hope it will work for you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top