3

I am migrating m1 code to m2, below is the code i used in m1 to get eav collection.

  $customerattrs = Mage::getModel('eav/entity_attribute')->getCollection();
    $customerattrs->addFieldToFilter('is_user_defined', 1);
    $customerattrs->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('customer')->getTypeId());

how this can be used in magento2?

here is my controller code

 class CreatePost extends \Magento\Framework\App\Action\Action
 {
protected $helperData;
public function __construct(

    \[Vendor}\[Module]\Helper\Data $helperData
) {
    $this->helperData = $helperData;
}    
public function execute()
{       
    if ($this->getRequest()->isPost()) {

        $helderDataObj = $this->helperData- >getCustomerUserDefinedAttributes();
        echo '<pre>';
        print_r($helderDataObj);            
        die;            
    }

  }

 }

I have declared helper file like below

class Data extends \Magento\Framework\App\Helper\AbstractHelper
 {
   protected $eavAttribute;
   protected $entity;   
   public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Eav\Model\Attribute $eavAttribute,
    \Magento\Eav\Model\Entity $entity
) {
    $this->eavAttribute = $eavAttribute;
    $this->entity = $entity;

    parent::__construct($context);
}
 public function getCustomerUserDefinedAttributes()
{
    $attributeCollection = $this->eavAttribute->getCollection();
    $attributeCollection->addFieldToFilter('is_user_defined', 1);
    $attributeCollection->addFieldToFilter('entity_type_id',$this->entity->setType('customer')->getTypeId());

    return $attributeCollection;
 }
}

2 Answers 2

3

add instance of Magento\Eav\Model\Attribute to the constructor of your class

protected $eavAttribute;
protected $entity;

public function __construct(
    ...
    \Magento\Eav\Model\Attribute $eavAttribute,
    \Magento\Eav\Model\Entity $entity
    ...
){
    ...
    $this->eavAttribute = $eavAttribute;
    $this->entity = $entity;
    ...
}

and then use it like this.

$attributeCollection = $this->eavAttribute->getCollection();
$attributeCollection->addFieldToFilter('is_user_defined', 1);
$attributeCollection->addFieldToFilter(
    'entity_type_id',
    $this->entity->setType('customer')->getTypeId()
);

UPDATE

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \[Vendor}\[Module]\Helper\Data $helperData
) {
    $this->helperData = $helperData;
    parent::__construct($context);
}  
9
  • Can we use it in controller? Aug 4, 2018 at 11:43
  • Yes you can use it Aug 4, 2018 at 11:45
  • hi, I am getting internal server error below is the error PHP Fatal error: Uncaught Error: Call to a member function dispatch() on null Aug 4, 2018 at 11:56
  • Update your controller code in question Aug 4, 2018 at 12:05
  • Please check it. I have updated in my question Aug 4, 2018 at 12:11
2

Since getCollection is deprecated, I would go this way:

namespace Vendor\Module\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Eav\Model\Config;
use Magento\Catalog\Model\Product as ProductEntityType;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Framework\Exception\LocalizedException;

class ProductAttribute implements OptionSourceInterface
{
    /**
     * @var AttributeCollectionFactory
     */
    private $attributeFactory;

    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * ProductAttribute constructor.
     *
     * @param AttributeCollectionFactory $attributeFactory
     * @param Config    $eavConfig
     */
    public function __construct(
        AttributeCollectionFactory $attributeFactory,
        Config $eavConfig
    )
    {
        $this->eavConfig = $eavConfig;
        $this->attributeFactory = $attributeFactory;
    }

    /**
     * @return array
     * @throws LocalizedException
     */
    public function toOptionArray() : array
    {
        $collection = $this->attributeFactory->create();
        $collection->addFieldToFilter('entity_type_id', $this->eavConfig->getEntityType(ProductEntityType::ENTITY)->getEntityTypeId());
        $attributeCodes = [];
        foreach($collection as $attributes)
        {
                $attributeCodes[] = [
                    'value' => $attributes->getData(AttributeInterface::ATTRIBUTE_CODE),
                    'label' => $attributes->getData(AttributeInterface::FRONTEND_LABEL)
                ];
        }
        return $attributeCodes;
    }
}

Same functionality with Object Manager:

   use Magento\Catalog\Model\Product as ProductEntityType;
   use Magento\Eav\Api\Data\AttributeInterface;

   class ProductAttribute implements OptionSourceInterface
   {
    
   /**
   * @return array
   * @throws LocalizedException
   */
    public function toOptionArray() : array
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $attributeFactory = $objectManager->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory');
        $eavConfig = $objectManager->create('Magento\Eav\Model\Config');
        $collection = $attributeFactory->create();
        $collection->addFieldToFilter('entity_type_id', $eavConfig->getEntityType(ProductEntityType::ENTITY)->getEntityTypeId());      
        $attributeCodes = [];
        foreach($collection as $attributes)
        {
          $attributeCodes[] = [
            'value' => $attributes->getData(AttributeInterface::ATTRIBUTE_CODE),
            'label' => $attributes->getData(AttributeInterface::FRONTEND_LABEL)
        ];
    }
  }
}
2
  • 1
    Thanks so much . it is so help full. the good thing about your method is that, it returns all attributes easily.
    – moein kh
    Jul 14, 2021 at 15:58
  • 1
    @moeinkh glad that it helped someone. Cheers ! :) Jul 15, 2021 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.