get eav attribute collection Code format in Magento2
-
21-01-2021 - |
Question
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;
}
}
Solution
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);
}
OTHER TIPS
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;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange