Skip to content

Commit fb19f3f

Browse files
al.kravchuknmalevanec
al.kravchuk
authored andcommitted
[Forwardport] Creating custom customer attribute with default value 0 will cause not saving value for customer entity.
1 parent 1a805d0 commit fb19f3f

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Boolean.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Model\Product\Attribute\Backend;
79

810
use Magento\Catalog\Model\Product\Attribute\Source\Boolean as BooleanSource;
@@ -25,7 +27,9 @@ public function beforeSave($object)
2527
$attributeCode = $this->getAttribute()->getName();
2628
if ($object->getData('use_config_' . $attributeCode)) {
2729
$object->setData($attributeCode, BooleanSource::VALUE_USE_CONFIG);
30+
return $this;
2831
}
29-
return $this;
32+
33+
return parent::beforeSave($object);
3034
}
3135
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Downloadable\Setup\Patch\Data;
9+
10+
use Magento\Eav\Setup\EavSetup;
11+
use Magento\Eav\Setup\EavSetupFactory;
12+
use Magento\Framework\Setup\ModuleDataSetupInterface;
13+
use Magento\Framework\Setup\Patch\DataPatchInterface;
14+
use Magento\Framework\Setup\Patch\PatchVersionInterface;
15+
16+
/**
17+
* Remove default value from links exist attribute.
18+
*/
19+
class UpdateLinksExistDefaultAttributeValue implements DataPatchInterface, PatchVersionInterface
20+
{
21+
/**
22+
* @var ModuleDataSetupInterface
23+
*/
24+
private $moduleDataSetup;
25+
26+
/**
27+
* @var EavSetupFactory
28+
*/
29+
private $eavSetupFactory;
30+
31+
/**
32+
* @param ModuleDataSetupInterface $moduleDataSetup
33+
* @param EavSetupFactory $eavSetupFactory
34+
*/
35+
public function __construct(
36+
ModuleDataSetupInterface $moduleDataSetup,
37+
EavSetupFactory $eavSetupFactory
38+
) {
39+
$this->moduleDataSetup = $moduleDataSetup;
40+
$this->eavSetupFactory = $eavSetupFactory;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function apply()
47+
{
48+
/** @var EavSetup $eavSetup */
49+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
50+
// remove default value
51+
$eavSetup->updateAttribute(
52+
\Magento\Catalog\Model\Product::ENTITY,
53+
'links_exist',
54+
'default_value',
55+
null
56+
);
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public static function getDependencies()
63+
{
64+
return [InstallDownloadableAttributes::class];
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public static function getVersion()
71+
{
72+
return '2.0.3';
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function getAliases()
79+
{
80+
return [];
81+
}
82+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Eav\Model\Entity\Attribute;
89

@@ -418,7 +419,7 @@ public function getIsVisibleOnFront()
418419
/**
419420
* Returns default value
420421
*
421-
* @return string|int|bool|float
422+
* @return string|null
422423
* @codeCoverageIgnore
423424
*/
424425
public function getDefaultValue()
@@ -812,7 +813,7 @@ public function getBackendTable()
812813
if ($this->isStatic()) {
813814
$this->_dataTable = $this->getEntityType()->getValueTablePrefix();
814815
} else {
815-
$backendTable = trim($this->_getData('backend_table'));
816+
$backendTable = trim((string)$this->_getData('backend_table'));
816817
if (empty($backendTable)) {
817818
$entityTable = [$this->getEntityType()->getEntityTablePrefix(), $this->getBackendType()];
818819
$backendTable = $this->getResource()->getTable($entityTable);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Eav\Model\Entity\Attribute\Backend;
89

@@ -204,12 +205,12 @@ public function getEntityValueId($entity)
204205
/**
205206
* Retrieve default value
206207
*
207-
* @return mixed
208+
* @return string
208209
*/
209210
public function getDefaultValue()
210211
{
211212
if ($this->_defaultValue === null) {
212-
if ($this->getAttribute()->getDefaultValue()) {
213+
if ($this->getAttribute()->getDefaultValue() !== null) {
213214
$this->_defaultValue = $this->getAttribute()->getDefaultValue();
214215
} else {
215216
$this->_defaultValue = "";
@@ -285,7 +286,7 @@ public function afterLoad($object)
285286
public function beforeSave($object)
286287
{
287288
$attrCode = $this->getAttribute()->getAttributeCode();
288-
if (!$object->hasData($attrCode) && $this->getDefaultValue()) {
289+
if (!$object->hasData($attrCode) && $this->getDefaultValue() !== '') {
289290
$object->setData($attrCode, $this->getDefaultValue());
290291
}
291292

@@ -348,9 +349,8 @@ public function getAffectedFields($object)
348349
}
349350

350351
/**
351-
* By default attribute value is considered scalar that can be stored in a generic way
352+
* By default attribute value is considered scalar that can be stored in a generic way {@inheritdoc}
352353
*
353-
* {@inheritdoc}
354354
* @codeCoverageIgnore
355355
*/
356356
public function isScalar()

0 commit comments

Comments
 (0)