0

enter image description here

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
5
  • have you created that form using UI component? Commented 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. Commented Aug 28, 2019 at 1:24
  • I am asking about your custom form. Is that form created using UI Component? Commented Aug 28, 2019 at 10:56
  • oh yes, sorry for not understanding you, do you have any idea. Commented Aug 28, 2019 at 13:12
  • i guess below my answer will fulfil your requirement. Try once. Commented Aug 28, 2019 at 14:07

1 Answer 1

2

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
2
  • It works, Thank you, Keeps helping. Commented Aug 29, 2019 at 5:00
  • @MichaelHa : welcome :) happy coding...!!! Commented Aug 29, 2019 at 5:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.