/**
  * Build validation rules
  *
  * @param ProductAttributeInterface $attribute
  * @param array $data
  * @return array
  */
 public function build(ProductAttributeInterface $attribute, array $data)
 {
     $rules = [];
     if (!empty($data['required'])) {
         $rules['required-entry'] = true;
     }
     if ($attribute->getFrontendInput() === 'price') {
         $rules['validate-zero-or-greater'] = true;
     }
     $validationClasses = explode(' ', $attribute->getFrontendClass());
     foreach ($validationClasses as $class) {
         if (preg_match('/^maximum-length-(\\d+)$/', $class, $matches)) {
             $rules = array_merge($rules, ['max_text_length' => $matches[1]]);
             continue;
         }
         if (preg_match('/^minimum-length-(\\d+)$/', $class, $matches)) {
             $rules = array_merge($rules, ['min_text_length' => $matches[1]]);
             continue;
         }
         $rules = $this->mapRules($class, $rules);
     }
     return $rules;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
 {
     if ($attribute->getAttributeId()) {
         $existingModel = $this->get($attribute->getAttributeCode());
         if (!$existingModel->getAttributeId()) {
             throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
         }
         $attribute->setAttributeId($existingModel->getAttributeId());
         $attribute->setIsUserDefined($existingModel->getIsUserDefined());
         $attribute->setFrontendInput($existingModel->getFrontendInput());
         if (is_array($attribute->getFrontendLabels())) {
             $frontendLabel[0] = $existingModel->getDefaultFrontendLabel();
             foreach ($attribute->getFrontendLabels() as $item) {
                 $frontendLabel[$item->getStoreId()] = $item->getLabel();
             }
             $attribute->setDefaultFrontendLabel($frontendLabel);
         }
         if (!$attribute->getIsUserDefined()) {
             // Unset attribute field for system attributes
             $attribute->setApplyTo(null);
         }
     } else {
         $attribute->setAttributeId(null);
         if (!$attribute->getFrontendLabels() && !$attribute->getDefaultFrontendLabel()) {
             throw InputException::requiredField('frontend_label');
         }
         $frontendLabels = [];
         if ($attribute->getDefaultFrontendLabel()) {
             $frontendLabels[0] = $attribute->getDefaultFrontendLabel();
         }
         if ($attribute->getFrontendLabels() && is_array($attribute->getFrontendLabels())) {
             foreach ($attribute->getFrontendLabels() as $label) {
                 $frontendLabels[$label->getStoreId()] = $label->getLabel();
             }
             if (!isset($frontendLabels[0]) || !$frontendLabels[0]) {
                 throw InputException::invalidFieldValue('frontend_label', null);
             }
             $attribute->setDefaultFrontendLabel($frontendLabels);
         }
         $attribute->setAttributeCode($attribute->getAttributeCode() ?: $this->generateCode($frontendLabels[0]));
         $this->validateCode($attribute->getAttributeCode());
         $this->validateFrontendInput($attribute->getFrontendInput());
         $attribute->setBackendType($attribute->getBackendTypeByInput($attribute->getFrontendInput()));
         $attribute->setSourceModel($this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput()));
         $attribute->setBackendModel($this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput()));
         $attribute->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)->getId());
         $attribute->setIsUserDefined(1);
     }
     $this->attributeResource->save($attribute);
     return $attribute;
 }
 /**
  * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
  * @return string
  */
 protected function getDataType($attribute)
 {
     return isset($this->dataTypeMap[$attribute->getFrontendInput()]) ? $this->dataTypeMap[$attribute->getFrontendInput()] : $this->dataTypeMap['default'];
 }
Exemple #4
0
 /**
  * Retrieve scope label
  *
  * @param ProductAttributeInterface $attribute
  * @return \Magento\Framework\Phrase|string
  */
 private function getScopeLabel(ProductAttributeInterface $attribute)
 {
     if ($this->storeManager->isSingleStoreMode() || $attribute->getFrontendInput() === AttributeInterface::FRONTEND_INPUT) {
         return '';
     }
     switch ($attribute->getScope()) {
         case ProductAttributeInterface::SCOPE_GLOBAL_TEXT:
             return __('[GLOBAL]');
         case ProductAttributeInterface::SCOPE_WEBSITE_TEXT:
             return __('[WEBSITE]');
         case ProductAttributeInterface::SCOPE_STORE_TEXT:
             return __('[STORE VIEW]');
     }
     return '';
 }