/** * Get entity type object by entity type code/identifier * * @param int|string|Type $code * @return Type * @throws \Magento\Framework\Exception\LocalizedException */ public function getEntityType($code) { if ($code instanceof Type) { return $code; } $this->_initEntityTypes(); \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]); if (is_numeric($code)) { $entityCode = $this->_getEntityTypeReference($code); if ($entityCode !== null) { $code = $entityCode; } } $entityKey = $this->_getEntityKey($code); $entityType = $this->_load($entityKey); if ($entityType) { \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__); return $entityType; } $entityType = $this->_entityTypeFactory->create(['data' => isset($this->_entityTypeData[$code]) ? $this->_entityTypeData[$code] : []]); if (!$entityType->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid entity_type specified: %1', $code)); } $this->_addEntityTypeReference($entityType->getId(), $entityType->getEntityTypeCode()); $this->_save($entityType, $entityKey); \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__); return $entityType; }
/** * Convert configuration * * @param \DOMDocument $source * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function convert($source) { $data = []; $output = $this->toArray($source); foreach ($output['config']['dataSource'] as $dataSource) { $data[$dataSource['@attributes']['name']] = ['name' => $dataSource['@attributes']['name'], 'label' => $dataSource['@attributes']['label'], 'dataSet' => $dataSource['@attributes']['dataSet']]; $fields = []; if (isset($dataSource['fields']['@attributes']['entityType'])) { $entityType = $this->entityTypeFactory->create()->load($dataSource['fields']['@attributes']['entityType'], 'entity_type_code'); $attributeCollection = $entityType->getAttributeCollection(); foreach ($attributeCollection as $attribute) { if ($attribute->getIsUserDefined()) { $fields[$attribute->getAttributeCode()] = ['name' => $attribute->getAttributeCode(), 'source' => 'eav', 'formElement' => $this->mapFrontendInput($attribute->getFrontendInput()), 'is_required' => $attribute->getScopeIsRequired(), 'default_value' => $attribute->getScopeDefaultValue(), 'visible' => $attribute->getScopeIsVisible(), 'multiline_count' => $attribute->getScopeMultilineCount()]; if ($attribute->getValidateRules()) { $fields[$attribute->getAttributeCode()]['constraints']['validate'] = $attribute->getValidateRules(); } } } } foreach ($dataSource['fields']['field'] as $field) { foreach ($field['@attributes'] as $key => $value) { $fields[$field['@attributes']['name']][$key] = $value; } if (isset($field['@attributes']['source'])) { if (in_array($field['@attributes']['source'], ['lookup', 'option', 'reference'])) { $fields[$field['@attributes']['name']]['reference'] = ['target' => $field['reference']['@attributes']['target'], 'targetField' => $field['reference']['@attributes']['targetField'], 'referencedField' => $field['reference']['@attributes']['referencedField'], 'neededField' => $field['reference']['@attributes']['neededField']]; } } if (isset($field['tooltip'])) { $fields[$field['@attributes']['name']]['tooltip'] = ['link' => $field['tooltip']['link'], 'description' => $field['tooltip']['description']]; } if (isset($field['constraints']['validate'])) { foreach ($field['constraints']['validate'] as $rule) { $fields[$field['@attributes']['name']]['constraints']['validate'][$rule['@attributes']['name']] = isset($rule['@attribute']['value']) ? $rule['@attribute']['value'] : true; } } if (isset($field['constraints']['filter'])) { foreach ($field['constraints']['filter'] as $filter) { $filterValues['on'] = isset($filter['@attributes']['on']) ? $filter['@attributes']['on'] : null; $filterValues['by'] = isset($filter['@attributes']['by']) ? $filter['@attributes']['by'] : null; $filterValues['value'] = isset($filter['@attributes']['value']) ? $filter['@attributes']['value'] : null; $fields[$field['@attributes']['name']]['constraints']['filter'][] = $filterValues; } } if (isset($field['constraints']['readonly'])) { foreach ($field['constraints']['readonly'] as $condition) { $fields[$field['@attributes']['name']]['constraints']['readonly'][] = ['on' => $condition['@attributes']['on'], 'value' => $condition['@attributes']['value']]; } } } $data[$dataSource['@attributes']['name']]['fields'] = $fields; if (!empty($dataSource['references'])) { foreach ($dataSource['references'] as $reference) { $data[$reference['@attributes']['target']]['children'][$dataSource['@attributes']['name']][] = ['targetField' => $reference['@attributes']['targetField'], 'referencedField' => $reference['@attributes']['referencedField']]; } } } return $data; }
/** * Check Current Attribute Set is a default * * @return bool * @SuppressWarnings(PHPMD.BooleanGetMethodName) */ public function getIsCurrentSetDefault() { $isDefault = $this->getData('is_current_set_default'); if ($isDefault === null) { $defaultSetId = $this->_typeFactory->create()->load($this->_coreRegistry->registry('entityType'))->getDefaultAttributeSetId(); $isDefault = $this->_getSetId() == $defaultSetId; $this->setData('is_current_set_default', $isDefault); } return $isDefault; }
/** * {@inheritdoc} */ public function validate($object) { //validate attribute set entity type $entityType = $this->typeFactory->create()->loadByCode(\Magento\Catalog\Model\Product::ENTITY); $attributeSet = $this->setFactory->create()->load($object->getAttributeSetId()); if ($attributeSet->getEntityTypeId() != $entityType->getId()) { return ['attribute_set' => 'Invalid product template entity type']; } return parent::validate($object); }
public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Eav\Model\Entity\TypeFactory $attributeTypeFactory, \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\ObjectManagerInterface $objectManager) { parent::__construct($context); $this->_messageManager = $messageManager; $this->_objectManager = $objectManager; $typeId = -1; $resTypeId = $attributeTypeFactory->create()->getCollection()->addFieldToFilter('entity_type_code', ['eq' => 'catalog_product']); foreach ($resTypeId as $re) { $typeId = $re['entity_type_id']; } $attributesList = $attributeFactory->create()->getCollection()->addFieldToFilter('entity_type_id', ['eq' => $typeId]); $this->_listOfAttributes = []; foreach ($attributesList as $key => $attr) { array_push($this->_listOfAttributes, $attr['attribute_code']); } }
/** * Load attribute data by code * * @param string|int|\Magento\Eav\Model\Entity\Type $entityType * @param string $code * @return $this * @throws LocalizedException */ public function loadByCode($entityType, $code) { \Magento\Framework\Profiler::start('load_by_code'); if (is_numeric($entityType)) { $entityTypeId = $entityType; } elseif (is_string($entityType)) { $entityType = $this->_eavTypeFactory->create()->loadByCode($entityType); } if ($entityType instanceof \Magento\Eav\Model\Entity\Type) { $entityTypeId = $entityType->getId(); } if (empty($entityTypeId)) { throw new LocalizedException(__('Invalid entity supplied')); } $this->_getResource()->loadByCode($this, $entityTypeId, $code); $this->_afterLoad(); \Magento\Framework\Profiler::stop('load_by_code'); return $this; }
/** * @return int */ protected function _getSetId() { return intval($this->getRequest()->getParam('id')) > 0 ? intval($this->getRequest()->getParam('id')) : $this->_typeFactory->create()->load($this->_coreRegistry->registry('entityType'))->getDefaultAttributeSetId(); }
/** * Perform actions before object save * * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { if ($this->_useIncrementId && !$object->getIncrementId()) { /* @var $entityType \Magento\Eav\Model\Entity\Type */ $entityType = $this->_eavEntityTypeFactory->create()->loadByCode($this->_entityTypeForIncrementId); $object->setIncrementId($entityType->fetchNewIncrementId($object->getStoreId())); } parent::_beforeSave($object); return $this; }