0

Exception #0 (Exception): Warning: Illegal offset type in isset or empty in /var/www/html/magento2/app/code/Magento/Eav/Model/Entity/Attribute/Source/AbstractSource.php on line 76

Steps:

  • create a multiselect attribute via setup script
    • 'type' => 'varchar',
    • 'input' => 'multiselect',
  • add backend-model
    • 'backend' => 'Vendor\Module\Model\Attribute\Backend\Country',
      • this class extends Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend to use beforeSave() to store input as comma seperated value
  • make attribute visible in frontend (either config setting or catalog_attributes.xml

In my case it's multiselect attribute that store country code ... and I want to output localized country name.


Some code:

Vendor\Module\Model\Attribute\Backend\Country

There is only an àfterLoad method that converts comma seperated string to an array ...

/**
 * @param DataObject $object
 * @return $this
 */
public function afterLoad($object): Country
{
    $code = $this->getAttribute()->getAttributeCode();
    $data = $object->getData($code);
    $data = explode(',', $data);
    $object->setData($code, $data);

    return $this;
}

... but this doesn't matter. The same problem occurs if I directly use ArrayBackend as backend-model.

Debug:

It doesnt matter if I use this $data = explode(',', $data); (or remove method) from backend-model ... either a comma-seperated value or an array is passed to Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::getOptionText($value)

public function getOptionText($value)
{
    $options = $this->getAllOptions();
    // Fixed for tax_class_id and custom_design
    if (sizeof($options) > 0) {
        foreach ($options as $option) {
            if (isset($option['value']) && $option['value'] == $value) {
                return isset($option['label']) ? $option['label'] : $option['value'];
            }
        }
    }
    // End
    // problem here!
    if (isset($options[$value])) {
        return $options[$value];
    }
    return false;
}

If I use my backend-model it throws this exception and w/o afterLoad attribute isnt displayed (since $options[$value] isnt set)

Illegal offset: enter image description here

false: enter image description here


Any ideas what wrong with my code?

I remember this question strlen() issue in layered navigation, where obviously an array was passed to getOptionText().

I'd be nice if someone would try to reproduce.


Maybe realted to https://github.com/magento/magento2/issues/13612

Your Answer

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

Browse other questions tagged or ask your own question.