Magento 2: How to display the list of manufacturers in a drop-down
-
16-04-2021 - |
Pergunta
i have a dropdown ui-component in which i want to display a list of manufacturers.
In the example below, i have a working drop-down with shipping methods.
<field name="manufacturer_id">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Shipping\Model\Config\Source\Allmethods</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/select</item>
<item name="disableLabel" xsi:type="boolean">true</item>
<item name="multiple" xsi:type="boolean">false</item>
<item name="label" xsi:type="string" translate="true">Manufacturer</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
Is there an easy way of passing a class instead of Magento\Shipping\Model\Config\Source\Allmethods to get the list of manufacturers?
From this answer, getting the manufacturers is done via product attributes
$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */
$attribute = $om->get(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::class)
->get('manufacturer');
foreach ($attribute->getOptions() as $option) {
var_dump($option->getValue() . ' -> ' . $option->getLabel());
}
But how do i assign this data to my drop-down (and in what format?) ?
Thank you.
Solução
If I understand correctly, you want to show the options for a select UI component. I think this question have been answered here maybe this will help you.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange