How do I get all product attribute in select option in the custom form in Magento 2. Do you have any idea.
Faisal Sheikh
1,3901 gold badge9 silver badges18 bronze badges
asked Aug 27, 2019 at 10:42
MichaelHa
6611 gold badge8 silver badges24 bronze badges
-
have you created that form using UI component?Balwant Singh– Balwant Singh2019年08月27日 13:55:04 +00:00Commented Aug 27, 2019 at 13:55
-
No, This is Magento's product attribute table. and I want to get these properties to put in the select option in my custom form.MichaelHa– MichaelHa2019年08月28日 01:24:53 +00:00Commented Aug 28, 2019 at 1:24
-
I am asking about your custom form. Is that form created using UI Component?Balwant Singh– Balwant Singh2019年08月28日 10:56:26 +00:00Commented Aug 28, 2019 at 10:56
-
oh yes, sorry for not understanding you, do you have any idea.MichaelHa– MichaelHa2019年08月28日 13:12:09 +00:00Commented Aug 28, 2019 at 13:12
-
i guess below my answer will fulfil your requirement. Try once.Balwant Singh– Balwant Singh2019年08月28日 14:07:04 +00:00Commented Aug 28, 2019 at 14:07
1 Answer 1
Try below code:
In your UI form
product_attr_form.xml
<field name="pro_attr">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Namespace\ModuleName\Model\Config\Source\Proatr</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Product Attribute List</item>
<item name="componentType" xsi:type="string">field</item>
<item name="formElement" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
<item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
<item name="dataScope" xsi:type="string">coupon_code</item>
<item name="filterOptions" xsi:type="boolean">false</item>
<item name="showCheckbox" xsi:type="boolean">true</item>
<item name="chipsEnabled" xsi:type="boolean">true</item>
<item name="disableLabel" xsi:type="boolean">true</item>
<item name="levelsVisibility" xsi:type="number">1</item>
<item name="multiple" xsi:type="boolean">0</item>
<item name="sortOrder" xsi:type="number">2</item>
<item name="listens" xsi:type="array">
<item name="newOption" xsi:type="string">toggleOptionSelected</item>
</item>
<item name="required" xsi:type="boolean">0</item>
<item name="source" xsi:type="string">conditions</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
Namespace\ModuleName\Model\Config\Source\Proatr
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Namespace\ModuleName\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
/**
* Class Coupon
*/
class Proatr implements OptionSourceInterface
{
protected $_attributeFactory;
public function __construct (
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeFactory
) {
$this->_attributeFactory = $attributeFactory;
}
/**
* Get options
*
* @return array
*/
public function toOptionArray()
{
$availableOptions[] = array('value' => 0, 'label' => '--- Please Select ---');
$attributeInfo = $this->_attributeFactory->create()->addVisibleFilter();
foreach($attributeInfo as $attributes)
{
$attributeId = $attributes->getAttributeId();
// You can get all fields of attribute here
$attrlabel = $attributes->getFrontendLabel();
$availableOptions[] = array('value' => $attributeId, 'label' => $attrlabel);
}
//echo '<pre>';print_r($availableOptions);die;
return $availableOptions;
}
}
I hope it will help...!!!
answered Aug 28, 2019 at 14:01
Balwant Singh
1,2902 gold badges12 silver badges29 bronze badges
-
It works, Thank you, Keeps helping.MichaelHa– MichaelHa2019年08月29日 05:00:54 +00:00Commented Aug 29, 2019 at 5:00
-
@MichaelHa : welcome :) happy coding...!!!Balwant Singh– Balwant Singh2019年08月29日 05:15:15 +00:00Commented Aug 29, 2019 at 5:15
Explore related questions
See similar questions with these tags.
default