/** * Build validation rules * * @param AbstractAttribute $attribute * @param array $data * @return array */ public function build(AbstractAttribute $attribute, array $data) { $validation = []; if (isset($data['required']) && $data['required'] == 1) { $validation = array_merge($validation, ['required-entry' => true]); } if ($attribute->getFrontendInput() === 'price') { $validation = array_merge($validation, ['validate-zero-or-greater' => true]); } if ($attribute->getValidateRules()) { $validation = array_merge($validation, $attribute->getValidateRules()); } $rules = []; foreach ($validation as $type => $ruleName) { $rule = [$type => $ruleName]; if ($type === 'input_validation') { $rule = isset($this->validationRules[$ruleName]) ? $this->validationRules[$ruleName] : []; } $rules = array_merge($rules, $rule); } return $rules; }
/** * Get attribute type for upcoming validation. * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\Magento\Eav\Model\Entity\Attribute $attribute * @return string */ public static function getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { $frontendInput = $attribute->getFrontendInput(); if ($attribute->usesSource() && in_array($frontendInput, ['select', 'multiselect', 'boolean'])) { return $frontendInput; } elseif ($attribute->isStatic()) { return $frontendInput == 'date' ? 'datetime' : 'varchar'; } else { return $attribute->getBackendType(); } }
/** * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param array|\Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection $optionCollection * @return array */ protected function _prepareOptionValues(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $optionCollection) { $type = $attribute->getFrontendInput(); if ($type === 'select' || $type === 'multiselect') { $defaultValues = explode(',', $attribute->getDefaultValue()); $inputType = $type === 'select' ? 'radio' : 'checkbox'; } else { $defaultValues = []; $inputType = ''; } $values = []; $isSystemAttribute = is_array($optionCollection); foreach ($optionCollection as $option) { $bunch = $isSystemAttribute ? $this->_prepareSystemAttributeOptionValues($option, $inputType, $defaultValues) : $this->_prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues); foreach ($bunch as $value) { $values[] = new \Magento\Framework\Object($value); } } return $values; }
/** * Check if attribute allowed * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return bool */ protected function _isAllowedAttribute($attribute) { return !in_array($attribute->getFrontendInput(), $this->_ignoredAttributeTypes) && !in_array($attribute->getAttributeCode(), $this->_ignoredAttributeCodes) && $attribute->getFrontendLabel() != ""; }
/** * Get attribute type for upcoming validation. * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\Magento\Eav\Model\Entity\Attribute $attribute * @return string */ public static function getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { if ($attribute->usesSource()) { return $attribute->getFrontendInput() == 'multiselect' ? 'multiselect' : 'select'; } elseif ($attribute->isStatic()) { return $attribute->getFrontendInput() == 'date' ? 'datetime' : 'varchar'; } else { return $attribute->getBackendType(); } }