/** * Create Conversion id validator * * @param int|string $currentId * @return \Magento\Framework\Validator */ public function createConversionIdValidator($currentId) { $message = __('Conversion Id value is not valid "%1". Conversion Id should be an integer.', $currentId); /** @var \Magento\Framework\Validator\Builder $builder */ $builder = $this->_validatorBuilderFactory->create('Magento\\Framework\\Validator\\Builder', ['constraints' => [['alias' => 'Int', 'type' => '', 'class' => 'Magento\\Framework\\Validator\\IntUtils', 'options' => ['methods' => [['method' => 'setMessages', 'arguments' => [[IntUtils::NOT_INT => $message, IntUtils::INVALID => $message]]]]]]]]); return $builder->createValidator(); }
/** * Return default static virtual attribute that doesn't exists in EAV attributes * * @param \Magento\Eav\Model\Entity\AbstractEntity $resource * @param string $attributeCode * @return Attribute */ protected function _getDefaultAttribute(AbstractEntity $resource, $attributeCode) { $entityTypeId = $resource->getEntityType()->getId(); if (!isset($this->defaultAttributes[$entityTypeId][$attributeCode])) { $attribute = $this->objectManager->create($resource->getEntityType()->getAttributeModel())->setAttributeCode($attributeCode)->setBackendType(AbstractAttribute::TYPE_STATIC)->setIsGlobal(1)->setEntityType($resource->getEntityType())->setEntityTypeId($resource->getEntityType()->getId()); $this->defaultAttributes[$entityTypeId][$attributeCode] = $attribute; } return $this->defaultAttributes[$entityTypeId][$attributeCode]; }
/** * Create attribute from attribute data array * * @param string $entityType * @param array $attributeData * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */ protected function _createAttribute($entityType, $attributeData) { $entityType = $this->getEntityType($entityType); $entityTypeCode = $entityType->getEntityTypeCode(); $attributeKey = $this->_getAttributeKey($entityTypeCode, $attributeData['attribute_code']); $attribute = $this->_load($attributeKey); if ($attribute) { $existsFullAttribute = $attribute->hasIsRequired(); $fullAttributeData = array_key_exists('is_required', $attributeData); if ($existsFullAttribute || !$existsFullAttribute && !$fullAttributeData) { return $attribute; } } if (!empty($attributeData['attribute_model'])) { $model = $attributeData['attribute_model']; } else { $model = $entityType->getAttributeModel(); } /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ $attribute = $this->_universalFactory->create($model)->setData($attributeData); $this->_addAttributeReference($attributeData['attribute_id'], $attributeData['attribute_code'], $entityTypeCode); $attributeKey = $this->_getAttributeKey($entityTypeCode, $attributeData['attribute_code']); $this->_save($attribute, $attributeKey); return $attribute; }
/** * Retrieve option values collection * It is represented by an array in case of system attribute * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return array|\Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection */ protected function _getOptionValuesCollection(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { if ($this->canManageOptionDefaultOnly()) { $options = $this->_universalFactory->create($attribute->getSourceModel())->setAttribute($attribute)->getAllOptions(); return $options; } else { return $this->_attrOptionCollectionFactory->create()->setAttributeFilter($attribute->getId())->setPositionOrder('asc', true)->load(); } }
/** * Create constraint validator instance * * @param array $data * @return \Magento\Framework\Validator\ValidatorInterface * @throws \InvalidArgumentException */ protected function _createConstraintValidator(array $data) { $validator = $this->_oneValidatorFactory->create($data['class'], isset($data['options']['arguments']) ? $this->_applyArgumentsCallback($data['options']['arguments']) : []); // Check validator type if (!$validator instanceof \Magento\Framework\Validator\ValidatorInterface) { throw new \InvalidArgumentException(sprintf('Constraint class "%s" must implement \\Magento\\Framework\\Validator\\ValidatorInterface', $data['class'])); } return $validator; }
/** * Create validator builder instance based on entity and group. * * @param string $entityName * @param string $groupName * @param array|null $builderConfig * @throws \InvalidArgumentException * @return \Magento\Framework\Validator\Builder */ public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null) { if (!isset($this->_data[$entityName])) { throw new \InvalidArgumentException(sprintf('Unknown validation entity "%s"', $entityName)); } if (!isset($this->_data[$entityName][$groupName])) { throw new \InvalidArgumentException(sprintf('Unknown validation group "%s" in entity "%s"', $groupName, $entityName)); } $builderClass = isset($this->_data[$entityName][$groupName]['builder']) ? $this->_data[$entityName][$groupName]['builder'] : $this->_defaultBuilderClass; if (!class_exists($builderClass)) { throw new \InvalidArgumentException(sprintf('Builder class "%s" was not found', $builderClass)); } $builder = $this->_builderFactory->create($builderClass, ['constraints' => $this->_data[$entityName][$groupName]['constraints']]); if (!$builder instanceof \Magento\Framework\Validator\Builder) { throw new \InvalidArgumentException(sprintf('Builder "%s" must extend \\Magento\\Framework\\Validator\\Builder', $builderClass)); } if ($builderConfig) { $builder->addConfigurations($builderConfig); } return $builder; }
/** * Retrieve source instance * * @return \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource * @throws LocalizedException */ public function getSource() { if (empty($this->_source)) { if (!$this->getSourceModel()) { $this->setSourceModel($this->_getDefaultSourceModel()); } $source = $this->_universalFactory->create($this->getSourceModel()); if (!$source) { throw new LocalizedException(__('Source model "%1" not found for attribute "%2"', $this->getSourceModel(), $this->getAttributeCode())); } $this->_source = $source->setAttribute($this); } return $this->_source; }
/** * @param string $name * @param array $field * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function prepare($name, array &$field) { if ($name == self::CHILD_DATA_SOURCES) { foreach ($field as $childName => $childConfig) { $field[$childName] = $this->manager->getMetadata($childName); } return; } $options = []; if (isset($field['source'])) { if ($field['source'] == 'option') { $rawOptions = $this->manager->getData($field['reference']['target']); $options[] = ['label' => __('Please, select...'), 'value' => null]; foreach ($rawOptions as $rawOption) { $options[] = ['label' => $rawOption[$field['reference']['neededField']], 'value' => $rawOption[$field['reference']['targetField']]]; } } } else { if (isset($field['optionProvider'])) { list($source, $method) = explode('::', $field['optionProvider']); $sourceModel = $this->universalFactory->create($source); $options = $sourceModel->{$method}(); } } $attributeCodes = ['options' => ['eav_map' => 'options', 'default' => $options], 'dataType' => ['eav_map' => 'frontend_input', 'default' => 'text'], 'filterType' => ['default' => 'input_filter'], 'formElement' => ['default' => 'input'], 'displayArea' => ['default' => 'body'], 'visible' => ['eav_map' => 'is_visible', 'default' => true], 'required' => ['eav_map' => 'is_required', 'default' => false], 'label' => ['eav_map' => 'frontend_label'], 'sortOrder' => ['eav_map' => 'sort_order'], 'notice' => ['eav_map' => 'note'], 'default' => ['eav_map' => 'default_value'], 'unique' => [], 'description' => [], 'constraints' => [], 'customEntry' => [], 'size' => ['eav_map' => 'scope_multiline_count'], 'tooltip' => [], 'fieldGroup' => []]; foreach ($attributeCodes as $code => $info) { if (!isset($field[$code])) { if (isset($this->attributes[$name]) && isset($info['eav_map'])) { $field[$code] = $this->attributes[$name][$info['eav_map']]; } elseif (empty($field[$code]) && !empty($info['default'])) { $field[$code] = $info['default']; } } } if (isset($field['required']) && $field['required']) { $field['constraints']['validate']['required-entry'] = true; } }
/** * Retrieve Object instance with original data * * @param \Magento\Framework\Object $object * @return \Magento\Framework\Object */ protected function _getOrigObject($object) { $className = get_class($object); $origObject = $this->_universalFactory->create($className); $origObject->setData(array()); $this->load($origObject, $object->getData($this->getEntityIdField())); return $origObject; }
/** * Standard resource collection initialization * * @param string $model * @param string $entityModel * @return $this */ protected function _init($model, $entityModel) { $this->setItemObjectClass($model); $entity = $this->_universalFactory->create($entityModel); $this->setEntity($entity); return $this; }
/** * Get EAV Entity Form Attribute Collection * * @return mixed */ protected function _getFormAttributeCollection() { return $this->_universalFactory->create(str_replace('_', '\\', $this->_moduleName) . '\\Model\\Resource\\Form\\Attribute\\Collection'); }
/** * Retrieve resource entity object * * @return \Magento\Framework\Model\Resource\AbstractResource */ public function getEntity() { return $this->_universalFactory->create($this->_data['entity_model']); }