You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I selected the groups, I expect the 'Not Logged In' group to be saved as well
Actual result
The 'Not Logged In' group is not saved
The reason
This is because the 'Not Logged In' group has a value of '0' and the \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::beforeSave()-method does the following:
/**
* Prepare data for save
*
* @param \Magento\Framework\DataObject $object
* @return \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
*/
public function beforeSave($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$data = $object->getData($attributeCode);
if (is_array($data)) {
$data = array_filter($data);
$object->setData($attributeCode, implode(',', $data));
}
return parent::beforeSave($object);
}
The whole key here is the array_filter-method: this filters out 'falsy' values. '0' is concidered 'falsy', so it's stripped out. Even though we explicitly selected this value.
Workaround
To fix this, explicitly allow string value '0' in array_filter (this is what I'm currently doing in a customer backend model that extends ArrayBackend):
/**
* @param \Magento\Framework\DataObject $object
* @return $this
*/
public function beforeSave($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$data = $object->getData($attributeCode);
if (is_array($data)) {
$data = array_filter($data, function ($value) {
return $value === '0' || !empty($value);
});
$object->setData($attributeCode, implode(',', $data));
}
if (!$object->hasData($attributeCode) && $this->getDefaultValue()) {
$object->setData($attributeCode, $this->getDefaultValue());
}
return $this;
}
The text was updated successfully, but these errors were encountered:
When saving a multiselect attribute with value '0', the value gets not saved.
Preconditions
Steps to reproduce
For example: a custom source model that returns the list of customer groups including the 'Not Logged In'-group (which has ID 0):
Result:
Expected result
Actual result
The reason
This is because the 'Not Logged In' group has a value of '0' and the
\Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::beforeSave()
-method does the following:The whole key here is the
array_filter
-method: this filters out 'falsy' values. '0' is concidered 'falsy', so it's stripped out. Even though we explicitly selected this value.Workaround
To fix this, explicitly allow string value
'0'
inarray_filter
(this is what I'm currently doing in a customer backend model that extendsArrayBackend
):The text was updated successfully, but these errors were encountered: