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;
}
}