/** * Retrieve catalog_product entity type id * * @return int */ public function getEntityTypeId() { if ($this->_entityTypeId === null) { $this->_entityTypeId = (int) $this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); } return $this->_entityTypeId; }
/** * Constructor * * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param EavValidationRules $eavValidationRules * @param CustomerCollectionFactory $customerCollectionFactory * @param Config $eavConfig * @param FilterPool $filterPool * @param array $meta * @param array $data */ public function __construct($name, $primaryFieldName, $requestFieldName, EavValidationRules $eavValidationRules, CustomerCollectionFactory $customerCollectionFactory, Config $eavConfig, FilterPool $filterPool, array $meta = [], array $data = []) { parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->eavValidationRules = $eavValidationRules; $this->collection = $customerCollectionFactory->create(); $this->collection->addAttributeToSelect('*'); $this->eavConfig = $eavConfig; $this->filterPool = $filterPool; $this->meta['customer']['fields'] = $this->getAttributesMeta($this->eavConfig->getEntityType('customer')); $this->meta['address']['fields'] = $this->getAttributesMeta($this->eavConfig->getEntityType('customer_address')); }
/** * @param int $skeletonId * @return void * @throws StateException */ protected function validateSkeletonSet($skeletonId) { try { $skeletonSet = $this->attributeSetRepository->get($skeletonId); $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); if ($skeletonSet->getEntityTypeId() != $productEntityId) { throw new StateException(__('Can not create attribute set based on non product attribute set.')); } } catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { throw new StateException(__('Can not create attribute set based on not existing attribute set')); } }
/** * Get fields prefixes * * @return array */ public function getPrefixes() { // use cached eav config $entityTypeId = $this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->_attributeCollectionFactory->create(); $collection->setEntityTypeFilter($entityTypeId); $collection->setFrontendInputTypeFilter('media_image'); $prefixes = []; foreach ($collection as $attribute) { /* @var $attribute \Magento\Eav\Model\Entity\Attribute */ $prefixes[] = ['field' => $attribute->getAttributeCode() . '_', 'label' => $attribute->getFrontend()->getLabel()]; } return $prefixes; }
/** * Update all attribute-dependant index * * @param \Magento\Framework\Event\Observer $observer * @return \Magento\CatalogSearch\Model\Fulltext\Observer */ public function eavAttributeChange(\Magento\Framework\Event\Observer $observer) { $attribute = $observer->getEvent()->getAttribute(); /* @var $attribute \Magento\Eav\Model\Entity\Attribute */ $entityType = $this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY); /* @var $entityType \Magento\Eav\Model\Entity\Type */ if ($attribute->getEntityTypeId() != $entityType->getId()) { return $this; } $delete = $observer->getEventName() == 'eav_entity_attribute_delete_after'; if (!$delete && !$attribute->dataHasChangedFor('is_searchable')) { return $this; } $showNotice = false; if ($delete) { if ($attribute->getIsSearchable()) { $showNotice = true; } } elseif ($attribute->dataHasChangedFor('is_searchable')) { $showNotice = true; } if ($showNotice) { $url = $this->_backendUrl->getUrl('adminhtml/system_cache'); $this->messageManager->addNotice(__('Attribute setting change related with Search Index. Please run <a href="%1">Rebuild Search Index</a> process.', $url)); } return $this; }
/** * {@inheritdoc} */ public function create(\Magento\Catalog\Service\V1\Data\Eav\AttributeMetadata $attributeMetadata) { /** * @var $model \Magento\Catalog\Model\Resource\Eav\Attribute */ $model = $this->attributeFactory->create(); $data = $attributeMetadata->__toArray(); // unset attribute id because we create new attribute (does not rewrite existing one) unset($data[AttributeMetadata::ATTRIBUTE_ID]); // define frontend label if (!$attributeMetadata->getFrontendLabel()) { throw InputException::requiredField(AttributeMetadata::FRONTEND_LABEL); } $data[AttributeMetadata::FRONTEND_LABEL] = []; foreach ($attributeMetadata->getFrontendLabel() as $label) { $data[AttributeMetadata::FRONTEND_LABEL][$label->getStoreId()] = $label->getLabel(); } if (!isset($data[AttributeMetadata::FRONTEND_LABEL][0]) || !$data[AttributeMetadata::FRONTEND_LABEL][0]) { throw InputException::invalidFieldValue(AttributeMetadata::FRONTEND_LABEL, null); } $data[AttributeMetadata::ATTRIBUTE_CODE] = $attributeMetadata->getAttributeCode() ?: $this->generateCode($data[AttributeMetadata::FRONTEND_LABEL][0]); $this->validateCode($data[AttributeMetadata::ATTRIBUTE_CODE]); $this->validateFrontendInput($attributeMetadata->getFrontendInput()); $data[AttributeMetadata::BACKEND_TYPE] = $model->getBackendTypeByInput($attributeMetadata->getFrontendInput()); $data[AttributeMetadata::SOURCE_MODEL] = $this->helper->getAttributeSourceModelByInputType($attributeMetadata->getFrontendInput()); $data[AttributeMetadata::BACKEND_MODEL] = $this->helper->getAttributeBackendModelByInputType($attributeMetadata->getFrontendInput()); $model->addData($data); $model->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId())->setIsUserDefined(1); return $model->save()->getAttributeCode(); }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\Catalog\Model\Resource\Product\Collection $collection */ $collection = $this->collectionFactory->create(); $defaultAttributeSetId = $this->eavConfig->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)->getDefaultAttributeSetId(); $extendedSearchCriteria = $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField('attribute_set_id')->setValue($defaultAttributeSetId)->create()]); foreach ($this->metadataService->getList($extendedSearchCriteria->create())->getItems() as $metadata) { $collection->addAttributeToSelect($metadata->getAttributeCode()); } $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner'); $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner'); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } /** @var SortOrder $sortOrder */ foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) { $field = $sortOrder->getField(); $collection->addOrder($field, $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC'); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $collection->load(); $searchResult = $this->searchResultsFactory->create(); $searchResult->setSearchCriteria($searchCriteria); $searchResult->setItems($collection->getItems()); $searchResult->setTotalCount($collection->getSize()); return $searchResult; }
/** * Validate Frontend Input Type * * @param \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet * @return void * @throws \Magento\Framework\Exception\InputException */ protected function validate(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet) { $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); if ($attributeSet->getEntityTypeId() != $productEntityId) { throw new \Magento\Framework\Exception\StateException(__('Provided Attribute set non product Attribute set.')); } }
/** * Retrieve all attributes * * @return Attribute[] */ private function getAttributes() { if ($this->attributes === null) { $this->attributes = []; $entityType = $this->eavConfig->getEntityType(static::ENTITY); /** @var \Magento\Customer\Model\Attribute[] $attributes */ $attributes = $entityType->getAttributeCollection()->getItems(); /** @var \Magento\Customer\Model\ResourceModel\Customer $entity */ $entity = $entityType->getEntity(); foreach ($attributes as $attribute) { $attribute->setEntity($entity); } $this->attributes = $attributes; } return $this->attributes; }
/** * Return eav entity type instance * * @return \Magento\Eav\Model\Entity\Type */ public function getEntityType() { if ($this->_entityType === null) { $this->_entityType = $this->_eavConfig->getEntityType($this->_getEntityTypeCode()); } return $this->_entityType; }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Set\Collection $collection */ $collection = $this->collectionFactory->create(); /** The only possible/meaningful search criteria for attribute set is entity type code */ $entityTypeCode = $this->getEntityTypeCode($searchCriteria); if ($entityTypeCode !== null) { $collection->setEntityTypeFilter($this->eavConfig->getEntityType($entityTypeCode)->getId()); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setItems($collection->getItems()); $searchResults->setTotalCount($collection->getSize()); return $searchResults; }
/** * Retrieve searchable attributes * * @return Attribute[] */ private function getSearchableAttributes() { if ($this->searchableAttributes === null) { $this->searchableAttributes = []; /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $productAttributes */ $productAttributes = $this->collectionFactory->create(); $productAttributes->addToIndexFilter(true); /** @var \Magento\Eav\Model\Entity\Attribute[] $attributes */ $attributes = $productAttributes->getItems(); $entity = $this->eavConfig->getEntityType(Product::ENTITY)->getEntity(); foreach ($attributes as $attribute) { $attribute->setEntity($entity); } $this->searchableAttributes = $attributes; } return $this->searchableAttributes; }
/** * Collect fields for the entity with eav type. * * @param array $fixture * @return array */ protected function eavCollectFields(array $fixture) { $entityType = $fixture['entity_type']; $collection = $this->eavConfig->getEntityType($entityType)->getAttributeCollection(); $attributes = []; foreach ($collection as $attribute) { if (isset($fixture['product_type'])) { $applyTo = $attribute->getApplyTo(); if (!empty($applyTo) && !in_array($fixture['product_type'], $applyTo)) { continue; } } /** @var $attribute \Magento\Eav\Model\Entity\Attribute */ $code = $attribute->getAttributeCode(); $attributes[$code] = ['attribute_code' => $code, 'backend_type' => $attribute->getBackendType(), 'is_required' => $attribute->getIsRequired(), 'default_value' => $attribute->getDefaultValue(), 'input' => $attribute->getFrontendInput()]; } return $attributes; }
/** * Perform actions before object delete * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\StateException * @throws \Magento\Framework\Exception\LocalizedException */ protected function _beforeDelete(\Magento\Framework\Model\AbstractModel $object) { /** @var \Magento\Eav\Api\Data\AttributeSetInterface $object */ $defaultAttributeSetId = $this->eavConfig->getEntityType($object->getEntityTypeId())->getDefaultAttributeSetId(); if ($object->getAttributeSetId() == $defaultAttributeSetId) { throw new \Magento\Framework\Exception\StateException(__('Default attribute set can not be deleted')); } return parent::_beforeDelete($object); }
/** * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\ImportExport\Model\Export\Factory $collectionFactory * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Eav\Model\Config $eavConfig * @param array $data */ public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Eav\Model\Config $eavConfig, array $data = []) { $this->_localeDate = $localeDate; parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data); if (isset($data['entity_type_id'])) { $this->_entityTypeId = $data['entity_type_id']; } else { $this->_entityTypeId = $eavConfig->getEntityType($this->getEntityTypeCode())->getEntityTypeId(); } }
/** * {@inheritdoc} */ public function getAttributes($entityType, $attributeSetId) { /** @var \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet */ $attributeSet = $this->setRepository->get($attributeSetId); $requiredEntityTypeId = $this->eavConfig->getEntityType($entityType)->getId(); if (!$attributeSet->getAttributeSetId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) { throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId); } $attributeCollection = $this->attributeCollection->setAttributeSetFilter($attributeSet->getAttributeSetId())->load(); return $attributeCollection->getItems(); }
/** * {@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); foreach ($attribute->getOptions() as $option) { $this->optionManagement->add($attribute->getAttributeCode(), $option); } return $this->get($attribute->getAttributeCode()); }
/** * Add attribute set filter to collection based on attribute set name and corresponding entity type. * * @param string $attributeSetName * @param string $entityTypeCode * @return void */ public function setAttributeSetFilterBySetName($attributeSetName, $entityTypeCode) { //@codeCoverageIgnoreStart $entityTypeId = $this->eavConfig->getEntityType($entityTypeCode)->getId(); $this->join(['entity_attribute' => $this->getTable('eav_entity_attribute')], 'entity_attribute.attribute_id = main_table.attribute_id'); $this->join(['attribute_set' => $this->getTable('eav_attribute_set')], 'attribute_set.attribute_set_id = entity_attribute.attribute_set_id', []); $this->addFieldToFilter('attribute_set.entity_type_id', $entityTypeId); $this->addFieldToFilter('attribute_set.attribute_set_name', $attributeSetName); $this->setOrder('entity_attribute.sort_order', self::SORT_ORDER_ASC); //@codeCoverageIgnoreEnd }
/** * Prepares values options to be used as select options or hashed array * Result is stored in following keys: * 'value_select_options' - normal select array: array(array('value' => $value, 'label' => $label), ...) * 'value_option' - hashed array: array($value => $label, ...), * * @return $this */ protected function _prepareValueOptions() { // Check that both keys exist. Maybe somehow only one was set not in this routine, but externally. $selectReady = $this->getData('value_select_options'); $hashedReady = $this->getData('value_option'); if ($selectReady && $hashedReady) { return $this; } // Get array of select options. It will be used as source for hashed options $selectOptions = null; if ($this->getAttribute() === 'attribute_set_id') { $entityTypeId = $this->_config->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); $selectOptions = $this->_attrSetCollection->setEntityTypeFilter($entityTypeId)->load()->toOptionArray(); } elseif ($this->getAttribute() === 'type_id') { foreach ($selectReady as $value => $label) { if (is_array($label) && isset($label['value'])) { $selectOptions[] = $label; } else { $selectOptions[] = array('value' => $value, 'label' => $label); } } $selectReady = null; } elseif (is_object($this->getAttributeObject())) { $attributeObject = $this->getAttributeObject(); if ($attributeObject->usesSource()) { if ($attributeObject->getFrontendInput() == 'multiselect') { $addEmptyOption = false; } else { $addEmptyOption = true; } $selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption); } } // Set new values only if we really got them if ($selectOptions !== null) { // Overwrite only not already existing values if (!$selectReady) { $this->setData('value_select_options', $selectOptions); } if (!$hashedReady) { $hashedOptions = array(); foreach ($selectOptions as $option) { if (is_array($option['value'])) { continue; // We cannot use array as index } $hashedOptions[$option['value']] = $option['label']; } $this->setData('value_option', $hashedOptions); } } return $this; }
/** * {@inheritdoc} */ public function create($entityTypeCode, AttributeSetInterface $attributeSet, $skeletonId) { /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */ if ($attributeSet->getId() !== null) { throw InputException::invalidFieldValue('id', $attributeSet->getId()); } if ($skeletonId == 0) { throw InputException::invalidFieldValue('skeletonId', $skeletonId); } // Make sure that skeleton attribute set is valid (try to load it) $this->repository->get($skeletonId); try { $attributeSet->setEntityTypeId($this->eavConfig->getEntityType($entityTypeCode)->getId()); $attributeSet->validate(); } catch (\Exception $exception) { throw new InputException(__($exception->getMessage())); } $this->repository->save($attributeSet); $attributeSet->initFromSkeleton($skeletonId); return $this->repository->save($attributeSet); }
/** * Constructor * * @param string $primaryFieldName * @param string $requestFieldName * @param EavValidationRul $eavValidationRul * @param CustomerCollectionFactory $customerCollectionFactory * @param Config $eavConfig * @param array $meta * @param array $data */ public function __construct($primaryFieldName, $requestFieldName, EavValidationRul $eavValidationRul, CustomerCollectionFactory $customerCollectionFactory, Config $eavConfig, array $meta = [], array $data = []) { $this->primaryFieldName = $primaryFieldName; $this->requestFieldName = $requestFieldName; $this->eavValidationRul = $eavValidationRul; $this->collection = $customerCollectionFactory->create(); $this->collection->addAttributeToSelect('*'); $this->eavConfig = $eavConfig; $this->meta = $meta; $this->meta['customer']['fields'] = $this->getAttributesMeta($this->eavConfig->getEntityType('customer')); $this->meta['address']['fields'] = $this->getAttributesMeta($this->eavConfig->getEntityType('customer_address')); $this->data = $data; }
/** * Join product and type data * * @return $this */ protected function _joinTables() { $entityType = $this->_eavConfig->getEntityType('catalog_product'); $attribute = $this->_eavConfig->getAttribute($entityType->getEntityTypeId(), 'name'); $joinConditionDefault = sprintf("p_d.attribute_id=%d AND p_d.store_id='0' AND main_table.product_id=p_d.entity_id", $attribute->getAttributeId()); $joinCondition = sprintf("p.attribute_id=%d AND p.store_id=main_table.store_id AND main_table.product_id=p.entity_id", $attribute->getAttributeId()); $this->getSelect()->joinLeft(array('p_d' => $attribute->getBackend()->getTable()), $joinConditionDefault, array()); $this->getSelect()->joinLeft(array('p' => $attribute->getBackend()->getTable()), $joinCondition, array('name' => $this->getConnection()->getIfNullSql('p.value', 'p_d.value'))); $this->getSelect()->joinLeft(array('types' => $this->getTable('googleshopping_types')), 'main_table.type_id=types.type_id'); $this->_resourceHelper->prepareColumnsList($this->getSelect()); // avoid column name collision return $this; }
/** * Retrieve loaded attribute by code * * @param string $attributeCode * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Eav\Model\Entity\Attribute */ public function getAttribute($attributeCode) { $attributes = $this->getAttributes(); if (!isset($attributes[$attributeCode])) { $attribute = $this->_attributeFactory->create(); $attribute->loadByCode($this->getEntityTypeId(), $attributeCode); if (!$attribute->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid attribute %1', $attributeCode)); } $entity = $this->_eavConfig->getEntityType($this->getEntityType())->getEntity(); $attribute->setEntity($entity); return $attribute; } return $attributes[$attributeCode]; }
/** * Add set info to attributes * * @param string|Type $entityType * @param array $attributes * @param int $setId * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function addSetInfo($entityType, array $attributes, $setId = null) { $attributeIds = []; $entityType = $this->_eavConfig->getEntityType($entityType); foreach ($attributes as $attribute) { $attribute = $this->_eavConfig->getAttribute($entityType, $attribute); if ($setId && is_array($attribute->getAttributeSetInfo($setId))) { continue; } if (!$attribute->getAttributeId()) { continue; } $attributeIds[] = $attribute->getAttributeId(); } if ($attributeIds) { $setInfo = $this->_getResource()->getSetInfo($attributeIds, $setId); foreach ($attributes as $attribute) { $attribute = $this->_eavConfig->getAttribute($entityType, $attribute); if (!$attribute->getAttributeId()) { continue; } if (!in_array($attribute->getAttributeId(), $attributeIds)) { continue; } if (is_numeric($setId)) { $attributeSetInfo = $attribute->getAttributeSetInfo(); if (!is_array($attributeSetInfo)) { $attributeSetInfo = []; } if (isset($setInfo[$attribute->getAttributeId()][$setId])) { $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId]; } $attribute->setAttributeSetInfo($attributeSetInfo); } else { if (isset($setInfo[$attribute->getAttributeId()])) { $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]); } else { $attribute->setAttributeSetInfo([]); } } } } return $this; }
/** * Loads attribute set by name if attribute with such name exists * Otherwise creates the attribute set with $setName name and return it * * @param string $setName * @return \Magento\Eav\Model\Entity\Attribute\Set * @throws \Exception * @throws \Magento\Framework\Model\Exception */ protected function processAttributeSet($setName) { /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */ $attributeSet = $this->attributeSetFactory->create(); $setCollection = $attributeSet->getResourceCollection()->addFieldToFilter('entity_type_id', $this->getEntityTypeId())->addFieldToFilter('attribute_set_name', $setName)->load(); $attributeSet = $setCollection->fetchItem(); if (!$attributeSet) { $attributeSet = $this->attributeSetFactory->create(); $attributeSet->setEntityTypeId($this->getEntityTypeId()); $attributeSet->setAttributeSetName($setName); $attributeSet->save(); $defaultSetId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getDefaultAttributeSetId(); $attributeSet->initFromSkeleton($defaultSetId); $attributeSet->save(); } return $attributeSet; }
/** * {@inheritdoc} */ public function getAttributeList($attributeSetId) { /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */ $attributeSet = $this->setFactory->create()->load($attributeSetId); $requiredEntityTypeId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); if (!$attributeSet->getId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) { // Attribute set does not exist throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId); } $attributeCollection = $this->attributeCollection->setAttributeSetFilter($attributeSet->getId())->load(); $attributes = array(); /** @var \Magento\Eav\Model\Entity\Attribute $attribute */ foreach ($attributeCollection as $attribute) { $attributes[] = $this->attributeBuilder->setId($attribute->getAttributeId())->setCode($attribute->getAttributeCode())->setFrontendLabel($attribute->getData('frontend_label'))->setDefaultValue($attribute->getDefaultValue())->setIsRequired((bool) $attribute->getData('is_required'))->setIsUserDefined((bool) $attribute->getData('is_user_defined'))->setFrontendInput($attribute->getData('frontend_input'))->create(); } return $attributes; }
/** * @param int $storeId * @param string $entityTypeCode * @param int $entityId * @param string $incrementId * @param string $queueStatus * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ public function build($storeId, $entityTypeCode, $entityId, $incrementId, $queueStatus) { // validating $entityTypeCode if (!in_array($entityTypeCode, [self::ENTITY_TYPE_CODE_INVOICE, self::ENTITY_TYPE_CODE_CREDITMEMO])) { $message = __('When building a queue record an invalid entity_type_code was provided'); $this->avaTaxLogger->error($message, ['invalid_entity_type_code' => $entityTypeCode]); throw new \Exception($message); } // Get Entity Type Details $entityType = $this->eavConfig->getEntityType($entityTypeCode); $this->setStoreId($storeId); $this->setEntityTypeId($entityType->getEntityTypeId()); $this->setEntityTypeCode($entityTypeCode); $this->setEntityId($entityId); $this->setIncrementId($incrementId); $this->setQueueStatus($queueStatus); $this->setAttempts(0); }
/** * {@inheritdoc} */ public function getList($productSku) { $result = array(); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productRepository->get($productSku); $galleryAttribute = $this->attributeFactory->create()->loadByCode($this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY), 'media_gallery'); $container = new \Magento\Framework\Object(array('attribute' => $galleryAttribute)); $gallery = $this->mediaGallery->loadGallery($product, $container); $productImages = $this->getMediaAttributeValues($product); foreach ($gallery as $image) { $this->galleryEntryBuilder->setId($image['value_id']); $this->galleryEntryBuilder->setLabel($image['label_default']); $this->galleryEntryBuilder->setTypes(array_keys($productImages, $image['file'])); $this->galleryEntryBuilder->setDisabled($image['disabled_default']); $this->galleryEntryBuilder->setPosition($image['position_default']); $this->galleryEntryBuilder->setFile($image['file']); $result[] = $this->galleryEntryBuilder->create(); } return $result; }
/** * {@inheritdoc} */ public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { if (!$entityTypeCode) { throw InputException::requiredField('entity_type_code'); } /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection */ $attributeCollection = $this->attributeCollectionFactory->create(); $this->joinProcessor->process($attributeCollection); $attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityTypeCode]); $attributeCollection->join(['entity_type' => $attributeCollection->getTable('eav_entity_type')], 'main_table.entity_type_id = entity_type.entity_type_id', []); $attributeCollection->joinLeft(['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')], 'main_table.attribute_id = eav_entity_attribute.attribute_id', []); $entityType = $this->eavConfig->getEntityType($entityTypeCode); $additionalTable = $entityType->getAdditionalAttributeTable(); if ($additionalTable) { $attributeCollection->join(['additional_table' => $attributeCollection->getTable($additionalTable)], 'main_table.attribute_id = additional_table.attribute_id', []); } //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $attributeCollection); } /** @var SortOrder $sortOrder */ foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) { $attributeCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } $totalCount = $attributeCollection->getSize(); // Group attributes by id to prevent duplicates with different attribute sets $attributeCollection->addAttributeGrouping(); $attributeCollection->setCurPage($searchCriteria->getCurrentPage()); $attributeCollection->setPageSize($searchCriteria->getPageSize()); $attributes = []; /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */ foreach ($attributeCollection as $attribute) { $attributes[] = $this->get($entityTypeCode, $attribute->getAttributeCode()); } $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setItems($attributes); $searchResults->setTotalCount($totalCount); return $searchResults; }
/** * Return collection of same attributes for selected products without unique * * @return \Magento\Eav\Model\Resource\Entity\Attribute\Collection */ public function getAttributes() { if ($this->_attributes === null) { $this->_attributes = $this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getAttributeCollection()->addIsNotUniqueFilter()->setInAllAttributeSetsFilter($this->getProductsSetIds()); if ($this->_excludedAttributes) { $this->_attributes->addFieldToFilter('attribute_code', ['nin' => $this->_excludedAttributes]); } // check product type apply to limitation and remove attributes that impossible to change in mass-update $productTypeIds = $this->getProducts()->getProductTypeIds(); foreach ($this->_attributes as $attribute) { /* @var $attribute \Magento\Catalog\Model\Entity\Attribute */ foreach ($productTypeIds as $productTypeId) { $applyTo = $attribute->getApplyTo(); if (count($applyTo) > 0 && !in_array($productTypeId, $applyTo)) { $this->_attributes->removeItemByKey($attribute->getId()); break; } } } } return $this->_attributes; }