cancel
Showing results for 
Search instead for 
Did you mean: 

Adding value of item to source values in a custom attribute type

SOLVED

Adding value of item to source values in a custom attribute type

Hello!

 

I've managed - with a fair bit of help and hours (days!) of work - to finally get a custom attribute type working, with a custom input renderer (it's to use a custom input field - a dropdown that allows text input too). 

 

But I'm stuck on one last problem.

 

The source on the attribute provides the data for the drop down (ie, A,B,C etc) which works fine.

 

And if I edit my custom product type, that uses this custom attribute type, with one of the options from source (ie, A, B, C), it works. Going back to the product correctly shows the right one selected.

 

But the point of the type is so I can allow users to choose A,B, C, or type in their own. That works, and it stores it... but when the source module is called to populate the input, it only puts in A, B, C - it doesn't put in the actual value - obviously.

 

The code in my class 

class Attributes extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource

is

  public function getAllOptions()
    {
        if (null === $this->_options) {
            $this->_options = [
                ['label' => __('A'), 'value' => '1'],
                ['label' => __('B'), 'value' => '2'],
                ['label' => __('C'), 'value' => '3'],
            ];
      
       return $this->_options;
}

So this populates the field with A,B,C.

 

How can I add the value stored in the product to this list? 

 

Thank you!

 

 

 

1 REPLY

Re: Adding value of item to source values in a custom attribute type

I've managed to solve this myself so I'll post the answer here for anyone in the same boat.

 

The html to display the select box comes from the function

 

public function getElementHtml()

 in the input_renderer class

 

This function gets the data from the source class

 

This data is added to the select element with

 

$this->_optionToHtml

 

All I had to do was check if the value of the attribute 

 

$this->getValue()

is in the array returned from source, and if not add the value manually using _optionToHtml. 

 

Seems to work perfectly.

 

Hope it helps someone.