cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all the attributes on Admin Ui Component

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How to get all the attributes on Admin Ui Component

Hi,

 

 

I am making an admin form includes a select of attribute values. My code is returning an empty select.

 

<field name="group">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Vendor\Module\Model\Source\Myvalues</item>
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">Attribute</item>
            <item name="formElement" xsi:type="string">select</item>
        </item>
    </argument>
</field>

I have created Myvalues.php

 

<?php

namespace Vendor\Module\Model\Source;
    use Magento\Store\Model\StoreManagerInterface;
    use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;

class Myvalues implements \Magento\Framework\Option\ArrayInterface
{
        public function __construct(
            CollectionFactory $collectionFactory,
            \Magento\Framework\ObjectManagerInterface $objectManager
        ) {
           $this->_collectionFactory = $collectionFactory;
            $this->_objectManager = $objectManager;
        }


    public function toOptionArray()
    {
		$attributes = $this->getAttributes();
        return  $attributes;
    }



            public function getAttributes() {
                    $collection = $this->_collectionFactory->create();
                    $attr_groups = array();
                    foreach ($collection as $items) {
                        $attr_groups[] = $items->getData();
                    }

                    return $attr_groups; 
    }


 
 
}

I would like to know how to get a collection of attributes in default attribute set.

3 REPLIES 3

Re: How to get all the attributes on Admin Ui Component

Hello @tvgarden

 

<?php

    /** @var  $coll \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection */
    $coll = $this->_objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class);
    // add filter by entity type to get product attributes only
    // '4' is the default type ID for 'catalog_product' entity - see 'eav_entity_type' table)
    // or skip the next line to get all attributes for all types of entities
    $coll->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);
    $attrAll = $coll->load()->getItems();

hope it will help you.

 

If works then mark as solution.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to get all the attributes on Admin Ui Component

HI @Sunil Patel

Thanks I used your code but it's not getting any options.

 

 

Spoiler
<?php

namespace Vendor\Module\Model\Source;
    use Magento\Store\Model\StoreManagerInterface;
    use Magento\Framework\App\Filesystem\DirectoryList;
    use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;

class Myvalues implements \Magento\Framework\Option\ArrayInterface
{


        public function __construct(
 
            CollectionFactory $collectionFactory,
            \Magento\Framework\ObjectManagerInterface $objectManager
        ) {
        	 $this->_collectionFactory = $collectionFactory;
            $this->_objectManager = $objectManager;
        }


    public function toOptionArray()
    {

		$categories = $this->getCategoriesTree();
		$attributes = $this->getAttributes();

        return  $attributes;
    }



    public function getAttributes() {
        
         $collection = $this->_collectionFactory->create();
         
  /** @var  $coll \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection */
    $coll = $this->_objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class);
    // add filter by entity type to get product attributes only
    // '4' is the default type ID for 'catalog_product' entity - see 'eav_entity_type' table)
    // or skip the next line to get all attributes for all types of entities
    $coll->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);
    $attrAll = $coll->load()->getItems();

         
         $attr_groups = array();
          
        foreach ($collection as $items) {
            $attr_groups[] = $items->getData();
        }

        return $attrAll; //This will give you the collection of all the attributes which are available in that magento instance
    }


    public function getCategoriesTree()
        {
            $categories = $this->_objectManager->create(
                'Magento\Catalog\Ui\Component\Product\Form\Categories\Options'
            )->toOptionArray();
            return $categories;
        }


}

 

Re: How to get all the attributes on Admin Ui Component

Hi,

 

I have made it work:

    public function getAttributes() {
        
         $collection = $this->_collectionFactory->create();
         
         $attr_groups = array();
          
        foreach ($collection as $item) {
         $attr_groups[] = ['value' => $item->getData()['attribute_id'], 'label' => $item->getData()['frontend_label']];
        }

        return $attr_groups; 
    }