Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2.1.4 : ArrayBackend cannot save value '0' #8590

Closed
kanduvisla opened this issue Feb 17, 2017 · 2 comments
Closed

M2.1.4 : ArrayBackend cannot save value '0' #8590

kanduvisla opened this issue Feb 17, 2017 · 2 comments
Assignees
Labels
Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@kanduvisla
Copy link
Contributor

kanduvisla commented Feb 17, 2017

When saving a multiselect attribute with value '0', the value gets not saved.

Preconditions

  1. Magento 2.1.4

Steps to reproduce

  1. Create a mutliselect attribute
  2. Set it's source model (in the database to a custom source model.
  3. Have this source model return options that have a value of '0'.

For example: a custom source model that returns the list of customer groups including the 'Not Logged In'-group (which has ID 0):

/**
 * @var GroupManagementInterface
 */
protected $groupManagement;

/**
 * CustomerGroup constructor.
 * @param GroupManagementInterface $groupManagement
 */
public function __construct(
    GroupManagementInterface $groupManagement
) {
    $this->groupManagement = $groupManagement;
}

/**
 * @return array
 */
public function toArray()
{
    $notLoggedInGroup = $this->groupManagement->getNotLoggedInGroup();
    $groups = [
        $notLoggedInGroup->getId() => $notLoggedInGroup->getCode()
    ];

    foreach ($this->groupManagement->getLoggedInGroups() as $group) {
        $groups[$group->getId()] = $group->getCode();
    }

    return $groups;
}

Result:

screen shot 2017-02-17 at 11 48 37

Expected result

  1. When I selected the groups, I expect the 'Not Logged In' group to be saved as well

Actual result

  1. 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;
}
@vherasymenko vherasymenko self-assigned this Mar 14, 2017
@vherasymenko vherasymenko added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Mar 14, 2017
@nkajic
Copy link
Member

nkajic commented Mar 16, 2017

@nkajic working on issue

@okorshenko
Copy link
Contributor

@nkajic thank you for the fix
@kanduvisla thank you for reporting the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests

5 participants