/** * Create AttributeMetadata Data object from the Attribute Model * * @param \Magento\Customer\Model\Attribute $attribute * @return AttributeMetadata */ public function createMetadataAttribute($attribute) { $options = []; if ($attribute->usesSource()) { foreach ($attribute->getSource()->getAllOptions() as $option) { $options[] = $this->_optionBuilder->setLabel($option['label'])->setValue($option['value'])->create(); } } $validationRules = []; foreach ($attribute->getValidateRules() as $name => $value) { $validationRules[] = $this->_validationRuleBuilder->setName($name)->setValue($value)->create(); } $this->_attributeMetadataBuilder->setAttributeCode($attribute->getAttributeCode())->setFrontendInput($attribute->getFrontendInput())->setInputFilter((string) $attribute->getInputFilter())->setStoreLabel($attribute->getStoreLabel())->setValidationRules($validationRules)->setVisible((bool) $attribute->getIsVisible())->setRequired((bool) $attribute->getIsRequired())->setMultilineCount((int) $attribute->getMultilineCount())->setDataModel((string) $attribute->getDataModel())->setOptions($options)->setFrontendClass($attribute->getFrontend()->getClass())->setFrontendLabel($attribute->getFrontendLabel())->setNote((string) $attribute->getNote())->setIsSystem((bool) $attribute->getIsSystem())->setIsUserDefined((bool) $attribute->getIsUserDefined())->setBackendType($attribute->getBackendType())->setSortOrder((int) $attribute->getSortOrder()); return $this->_attributeMetadataBuilder->create(); }
public function testMergeDataObjectWithArray() { $dataNoOptions = array('attribute_code' => 'prefix', 'front_end_input' => 'text', 'input_filter' => null, 'store_label' => 'Prefix', 'validation_rules' => array(), 'visible' => '0', 'options' => array()); $dataWithOptions = array('attribute_code' => 'country_id', 'front_end_input' => 'select', 'input_filter' => null, 'store_label' => 'Country', 'validation_rules' => array(), 'visible' => '1', 'options' => array(array('label' => '', 'value' => ''), 'Afghanistan' => array('label' => 'Afghanistan', 'value' => 'AF'))); $attributeMetadata = $this->_builder->populateWithArray($dataNoOptions)->create(); $attributeMetadataA = $this->_builder->populateWithArray($dataWithOptions)->create(); $merged = $this->_builder->mergeDataObjectWithArray($attributeMetadata, $dataWithOptions); $this->assertEquals($attributeMetadataA, $merged); }