Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeFilter.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
11 use \Magento\Catalog\Model\Product;
12 
19 {
31  public function prepareProductAttributes(Product $product, array $productData, array $useDefaults): array
32  {
33  $attributeList = $product->getAttributes();
34  foreach ($productData as $attributeCode => $attributeValue) {
35  if ($this->isAttributeShouldNotBeUpdated($product, $useDefaults, $attributeCode, $attributeValue)) {
37  }
38 
39  if (isset($useDefaults[$attributeCode]) && $useDefaults[$attributeCode] === '1') {
40  $productData = $this->prepareDefaultData($attributeList, $attributeCode, $productData);
41  $productData = $this->prepareConfigData($product, $attributeCode, $productData);
42  }
43  }
44 
45  return $productData;
46  }
47 
54  private function prepareConfigData(Product $product, string $attributeCode, array $productData): array
55  {
56  // UI component sends value even if field is disabled, so 'Use Config Settings' must be reset to false
57  if ($product->hasData('use_config_' . $attributeCode)) {
58  $productData['use_config_' . $attributeCode] = false;
59  }
60 
61  return $productData;
62  }
63 
70  private function prepareDefaultData(array $attributeList, string $attributeCode, array $productData): array
71  {
72  if (isset($attributeList[$attributeCode])) {
74  $attribute = $attributeList[$attributeCode];
75  $attributeType = $attribute->getBackendType();
76  // For non-numberic types set the attributeValue to 'false' to trigger their removal from the db
77  if ($attributeType === 'varchar' || $attributeType === 'text' || $attributeType === 'datetime') {
78  $attribute->setIsRequired(false);
80  } else {
82  }
83  }
84 
85  return $productData;
86  }
87 
95  private function isAttributeShouldNotBeUpdated(Product $product, array $useDefaults, $attribute, $value): bool
96  {
97  $considerUseDefaultsAttribute = !isset($useDefaults[$attribute]) || $useDefaults[$attribute] === '1';
98 
99  return ($value === '' && $considerUseDefaultsAttribute && !$product->getData($attribute));
100  }
101 }
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$productData
prepareProductAttributes(Product $product, array $productData, array $useDefaults)