In Magento 2 we can get all options of specific EAV entity types attribute. In Magento 2 There are many native EAV Entity Types are available.
You can get all options of specific attribute, Let’s we take a catalog_product Entity Type.
We take Color attribute from Catalog_Product Eav Entity Types. Fetch all the options of color attribute.
Create Block file,
<?php
namespace Rbj\AttributeOption\Block
class Option extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Eav\Model\Config $eavConfig,
array $data = []
) {
$this->eavConfig = $eavConfig;
parent::__construct($context, $data);
}
/**
* Entity Type Catalog
* Attribute code color
* @return array
*/
public function getOptionList()
{
$attribute = $this->eavConfig->getAttribute('catalog_product', 'color');
return $attribute->getSource()->getAllOptions();
}
}
Call Function From Template file,
$result = $block->getOptionList();
foreach($result as $option) {
echo $option['value']. '=>' .$option['label'];echo "<br />";
}
echo "<pre>";print_r($result);
The result looks like,
Array
(
[0] => Array
(
[label] =>
[value] =>
)
[1] => Array
(
[value] => 49
[label] => Black
)
[2] => Array
(
[value] => 50
[label] => Blue
)
[3] => Array
(
[value] => 51
[label] => Brown
)
[4] => Array
(
[value] => 52
[label] => Gray
)
....
