Skip to content

Commit 451634a

Browse files
Mykola Palamarcpartica
authored andcommitted
MAGETWO-50123: Unable to assign blank value to attribute #3545 #4910 #5485
1 parent 8eb6402 commit 451634a

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,45 @@ private function getPreviousSetAttributes()
522522
return $this->prevSetAttributes;
523523
}
524524

525+
/**
526+
* Check is product already exists or we trying to create one
527+
*
528+
* @return bool
529+
*/
530+
private function isProductExists()
531+
{
532+
return (bool) $this->locator->getProduct()->getId();
533+
}
534+
535+
/**
536+
* Check is product has some value for attribute
537+
*
538+
* @param ProductAttributeInterface $attribute
539+
* @return bool
540+
*/
541+
private function isProductHasValueForAttribute(ProductAttributeInterface $attribute)
542+
{
543+
return (bool)($this->locator->getProduct()->getCustomAttribute($attribute->getAttributeCode()) !== null)
544+
&& $this->locator->getProduct()->getCustomAttribute($attribute->getAttributeCode())->getValue();
545+
}
546+
547+
/**
548+
* Check should we display default values for attribute or not
549+
*
550+
* @param ProductAttributeInterface $attribute
551+
* @return bool
552+
*/
553+
private function isShowDefaultValue(ProductAttributeInterface $attribute)
554+
{
555+
if (!$this->isProductExists()) {
556+
return true;
557+
} elseif ($attribute->getIsRequired() && !$this->isProductHasValueForAttribute($attribute)) {
558+
return true;
559+
}
560+
561+
return false;
562+
}
563+
525564
/**
526565
* Initial meta setup
527566
*
@@ -543,7 +582,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
543582
'visible' => $attribute->getIsVisible(),
544583
'required' => $attribute->getIsRequired(),
545584
'notice' => $attribute->getNote(),
546-
'default' => $attribute->getDefaultValue(),
585+
'default' => $this->isShowDefaultValue($attribute) ? $attribute->getDefaultValue() : null,
547586
'label' => $attribute->getDefaultFrontendLabel(),
548587
'code' => $attribute->getAttributeCode(),
549588
'source' => $groupCode,

app/code/Magento/Eav/Model/Entity/Attribute.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ protected function _getDefaultBackendModel()
167167
break;
168168
}
169169

170+
// we should override default behaviour of Magento\Framework\Exception\LocalizedException\AbstractBackend
171+
// but can't do this in abstract model and can't update DB records because of backward compatibility
172+
if ($this->getFrontendInput() == 'select'
173+
&& !$this->getIsRequired()
174+
&& !$this->getBackendModel()
175+
) {
176+
return \Magento\Eav\Model\Entity\Attribute\Backend\SelectBackend::class;
177+
}
178+
170179
return parent::_getDefaultBackendModel();
171180
}
172181

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Model\Entity\Attribute\Backend;
7+
8+
/**
9+
* Entity/Attribute/Model - attribute backend default
10+
*
11+
* @author Magento Core Team <core@magentocommerce.com>
12+
*/
13+
class SelectBackend extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
14+
{
15+
16+
public function beforeSave($object)
17+
{
18+
return $this;
19+
20+
}
21+
}
22+

0 commit comments

Comments
 (0)