I have created a field in system->config to save an attribute code which is already created.(For eg:color)

Based on that value, I need to get all the option values of that attribute.(ie ,In my admin form drop down, I need to display its option values as red,blue,green).

How it is possible ? Please help.


Add to the constructor in your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    \Magento\Eav\Model\Config $eavConfig,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    $this->eavConfig = $eavConfig;
    $this->scopeConfig = $scopeConfig;

then you can use that in your class

$attribute_code_here = $this->scopeConfig->getValue('dev/debug/template_hints', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$attribute = $this->eavConfig->getAttribute('catalog_product', $attribute_code_here);
$options = $attribute->getSource()->getAllOptions();
  • @V.P, If my answer solve your issue then mark as accepted. So other user take benefits from this. Dec 29 '16 at 13:36
  • I also want to show product attribute label in custom grid instead of attribute value. Currently it showing attribute option value. please suggest Aug 25 '17 at 14:07
  • I want to display attribute options dropdown in admin form and grid, please help me
    – Ashna
    Jul 23 '18 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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