protected function setUp() { parent::setUp(); $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); $this->eavValidationRulesMock = $this->getMockBuilder(EavValidationRules::class)->disableOriginalConstructor()->getMock(); $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass(); $this->groupCollectionFactoryMock = $this->getMockBuilder(GroupCollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->groupCollectionMock = $this->getMockBuilder(GroupCollection::class)->disableOriginalConstructor()->getMock(); $this->attributeMock = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock(); $this->groupMock = $this->getMockBuilder(Group::class)->disableOriginalConstructor()->setMethods(['getAttributeGroupCode'])->getMock(); $this->entityTypeMock = $this->getMockBuilder(EntityType::class)->disableOriginalConstructor()->getMock(); $this->attributeCollectionMock = $this->getMockBuilder(AttributeCollection::class)->disableOriginalConstructor()->getMock(); $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass(); $this->formElementMapperMock = $this->getMockBuilder(FormElementMapper::class)->disableOriginalConstructor()->getMock(); $this->metaPropertiesMapperMock = $this->getMockBuilder(MetaPropertiesMapper::class)->disableOriginalConstructor()->getMock(); $this->groupCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->groupCollectionMock); $this->groupCollectionMock->expects($this->any())->method('setAttributeSetFilter')->willReturnSelf(); $this->groupCollectionMock->expects($this->any())->method('setSortOrder')->willReturnSelf(); $this->groupCollectionMock->expects($this->any())->method('load')->willReturnSelf(); $this->groupCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupMock])); $this->attributeCollectionMock->expects($this->any())->method('addFieldToSelect')->willReturnSelf(); $this->attributeCollectionMock->expects($this->any())->method('load')->willReturnSelf(); $this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityTypeMock); $this->entityTypeMock->expects($this->any())->method('getAttributeCollection')->willReturn($this->attributeCollectionMock); $this->productMock->expects($this->any())->method('getAttributes')->willReturn([$this->attributeMock]); }
/** * Prepare Layout Content * * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _prepareLayout() { $categoryAttributes = $this->getCategory()->getAttributes(); if (!$this->getCategory()->getId()) { foreach ($categoryAttributes as $attribute) { $default = $attribute->getDefaultValue(); if ($default != '') { $this->getCategory()->setData($attribute->getAttributeCode(), $default); } } } $attributeSetId = $this->getCategory()->getDefaultAttributeSetId(); /** @var $groupCollection \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection */ $groupCollection = $this->_collectionFactory->create()->setAttributeSetFilter($attributeSetId)->setSortOrder()->load(); $defaultGroupId = 0; foreach ($groupCollection as $group) { /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */ if ($defaultGroupId == 0 or $group->getIsDefault()) { $defaultGroupId = $group->getId(); } } foreach ($groupCollection as $group) { /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */ $attributes = []; foreach ($categoryAttributes as $attribute) { /* @var $attribute \Magento\Eav\Model\Entity\Attribute */ if ($attribute->isInGroup($attributeSetId, $group->getId())) { $attributes[] = $attribute; } } // do not add grops without attributes if (!$attributes) { continue; } $active = $defaultGroupId == $group->getId(); $block = $this->getLayout()->createBlock($this->getAttributeTabBlock(), $this->getNameInLayout() . '_tab_' . $group->getAttributeGroupName())->setGroup($group)->setAttributes($attributes)->setAddHiddenFields($active)->toHtml(); $this->addTab('group_' . $group->getId(), ['label' => __($group->getAttributeGroupName()), 'content' => $block, 'active' => $active]); } $this->addTab('products', ['label' => __('Category Products'), 'content' => $this->getLayout()->createBlock('Magento\\Catalog\\Block\\Adminhtml\\Category\\Tab\\Product', 'category.product.grid')->toHtml()]); // dispatch event add custom tabs $this->_eventManager->dispatch('adminhtml_catalog_category_tabs', ['tabs' => $this]); /*$this->addTab('features', array( 'label' => __('Feature Products'), 'content' => 'Feature Products' )); */ return parent::_prepareLayout(); }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { $attributeSetId = $this->retrieveAttributeSetIdFromSearchCriteria($searchCriteria); if (!$attributeSetId) { throw InputException::requiredField('attribute_set_id'); } try { $this->setRepository->get($attributeSetId); } catch (\Exception $exception) { throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId); } $collection = $this->groupListFactory->create(); $this->joinProcessor->process($collection); $collection->setAttributeSetFilter($attributeSetId); $collection->setSortOrder(); $searchResult = $this->searchResultsFactory->create(); $searchResult->setSearchCriteria($searchCriteria); $searchResult->setItems($collection->getItems()); $searchResult->setTotalCount($collection->getSize()); return $searchResult; }
protected function setUp() { parent::setUp(); $this->objectManager = new ObjectManager($this); $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); $this->eavValidationRulesMock = $this->getMockBuilder(EavValidationRules::class)->disableOriginalConstructor()->getMock(); $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass(); $this->groupCollectionFactoryMock = $this->getMockBuilder(GroupCollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->groupCollectionMock = $this->getMockBuilder(GroupCollection::class)->disableOriginalConstructor()->getMock(); $this->attributeMock = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock(); $this->groupMock = $this->getMockBuilder(Group::class)->disableOriginalConstructor()->setMethods(['getAttributeGroupCode'])->getMock(); $this->entityTypeMock = $this->getMockBuilder(EntityType::class)->disableOriginalConstructor()->getMock(); $this->attributeCollectionMock = $this->getMockBuilder(AttributeCollection::class)->disableOriginalConstructor()->getMock(); $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass(); $this->formElementMapperMock = $this->getMockBuilder(FormElementMapper::class)->disableOriginalConstructor()->getMock(); $this->metaPropertiesMapperMock = $this->getMockBuilder(MetaPropertiesMapper::class)->disableOriginalConstructor()->getMock(); $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)->disableOriginalConstructor()->getMock(); $this->attributeGroupRepositoryMock = $this->getMockBuilder(ProductAttributeGroupRepositoryInterface::class)->getMockForAbstractClass(); $this->attributeGroupMock = $this->getMockBuilder(AttributeGroupInterface::class)->setMethods(['getAttributeGroupCode', 'getApplyTo'])->getMockForAbstractClass(); $this->attributeRepositoryMock = $this->getMockBuilder(ProductAttributeRepositoryInterface::class)->getMockForAbstractClass(); $this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)->disableOriginalConstructor()->setMethods(['getItems'])->getMock(); $this->sortOrderBuilderMock = $this->getMockBuilder(SortOrderBuilder::class)->disableOriginalConstructor()->getMock(); $this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)->getMockForAbstractClass(); $this->eavAttributeMock = $this->getMockBuilder(Attribute::class)->setMethods(['getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])->disableOriginalConstructor()->getMock(); $this->groupCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->groupCollectionMock); $this->groupCollectionMock->expects($this->any())->method('setAttributeSetFilter')->willReturnSelf(); $this->groupCollectionMock->expects($this->any())->method('setSortOrder')->willReturnSelf(); $this->groupCollectionMock->expects($this->any())->method('load')->willReturnSelf(); $this->groupCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupMock])); $this->attributeCollectionMock->expects($this->any())->method('addFieldToSelect')->willReturnSelf(); $this->attributeCollectionMock->expects($this->any())->method('load')->willReturnSelf(); $this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityTypeMock); $this->entityTypeMock->expects($this->any())->method('getAttributeCollection')->willReturn($this->attributeCollectionMock); $this->productMock->expects($this->any())->method('getAttributes')->willReturn([$this->attributeMock]); $this->storeMock = $this->getMockBuilder(StoreInterface::class)->setMethods(['load', 'getId', 'getConfig', 'getBaseCurrencyCode'])->getMockForAbstractClass(); $this->currencyMock = $this->getMockBuilder(Currency::class)->disableOriginalConstructor()->setMethods(['toCurrency'])->getMock(); $this->currencyLocaleMock = $this->getMockBuilder(CurrencyLocale::class)->disableOriginalConstructor()->setMethods(['getCurrency'])->getMock(); $this->eav = $this->getModel(); $this->objectManager->setBackwardCompatibleProperty($this->eav, 'localeCurrency', $this->currencyLocaleMock); }
/** * @return \Magento\Backend\Model\View\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function execute() { $data = $this->getRequest()->getPostValue(); $resultRedirect = $this->resultRedirectFactory->create(); if ($data) { $setId = $this->getRequest()->getParam('set'); $attributeSet = null; if (!empty($data['new_attribute_set_name'])) { $name = $this->filterManager->stripTags($data['new_attribute_set_name']); $name = trim($name); try { /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */ $attributeSet = $this->buildFactory->create()->setEntityTypeId($this->_entityTypeId)->setSkeletonId($setId)->setName($name)->getAttributeSet(); } catch (AlreadyExistsException $alreadyExists) { $this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $name)); $this->messageManager->setAttributeData($data); return $resultRedirect->setPath('catalog/*/edit', ['_current' => true]); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while saving the attribute.')); } } $redirectBack = $this->getRequest()->getParam('back', false); /* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ $model = $this->attributeFactory->create(); $attributeId = $this->getRequest()->getParam('attribute_id'); $attributeCode = $this->getRequest()->getParam('attribute_code'); $frontendLabel = $this->getRequest()->getParam('frontend_label'); $attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]); if (strlen($this->getRequest()->getParam('attribute_code')) > 0) { $validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']); if (!$validatorAttrCode->isValid($attributeCode)) { $this->messageManager->addError(__('Attribute code "%1" is invalid. Please use only letters (a-z), ' . 'numbers (0-9) or underscore(_) in this field, first character should be a letter.', $attributeCode)); return $resultRedirect->setPath('catalog/*/edit', ['attribute_id' => $attributeId, '_current' => true]); } } $data['attribute_code'] = $attributeCode; //validate frontend_input if (isset($data['frontend_input'])) { /** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator */ $inputType = $this->validatorFactory->create(); if (!$inputType->isValid($data['frontend_input'])) { foreach ($inputType->getMessages() as $message) { $this->messageManager->addError($message); } return $resultRedirect->setPath('catalog/*/edit', ['attribute_id' => $attributeId, '_current' => true]); } } if ($attributeId) { $model->load($attributeId); if (!$model->getId()) { $this->messageManager->addError(__('This attribute no longer exists.')); return $resultRedirect->setPath('catalog/*/'); } // entity type check if ($model->getEntityTypeId() != $this->_entityTypeId) { $this->messageManager->addError(__('We can\'t update the attribute.')); $this->_session->setAttributeData($data); return $resultRedirect->setPath('catalog/*/'); } $data['attribute_code'] = $model->getAttributeCode(); $data['is_user_defined'] = $model->getIsUserDefined(); $data['frontend_input'] = $model->getFrontendInput(); } else { /** * @todo add to helper and specify all relations for properties */ $data['source_model'] = $this->productHelper->getAttributeSourceModelByInputType($data['frontend_input']); $data['backend_model'] = $this->productHelper->getAttributeBackendModelByInputType($data['frontend_input']); } $data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []]; if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) { $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']); } $defaultValueField = $model->getDefaultValueByInput($data['frontend_input']); if ($defaultValueField) { $data['default_value'] = $this->getRequest()->getParam($defaultValueField); } if (!$model->getIsUserDefined() && $model->getId()) { // Unset attribute field for system attributes unset($data['apply_to']); } $model->addData($data); if (!$attributeId) { $model->setEntityTypeId($this->_entityTypeId); $model->setIsUserDefined(1); } $groupCode = $this->getRequest()->getParam('group'); if ($setId && $groupCode) { // For creating product attribute on product page we need specify attribute set and group $attributeSetId = $attributeSet ? $attributeSet->getId() : $setId; $groupCollection = $attributeSet ? $attributeSet->getGroups() : $this->groupCollectionFactory->create()->setAttributeSetFilter($attributeSetId)->load(); foreach ($groupCollection as $group) { if ($group->getAttributeGroupCode() == $groupCode) { $attributeGroupId = $group->getAttributeGroupId(); break; } } $model->setAttributeSetId($attributeSetId); $model->setAttributeGroupId($attributeGroupId); } try { $model->save(); $this->messageManager->addSuccess(__('You saved the product attribute.')); $this->_attributeLabelCache->clean(); $this->_session->setAttributeData(false); if ($this->getRequest()->getParam('popup')) { $requestParams = ['attributeId' => $this->getRequest()->getParam('product'), 'attribute' => $model->getId(), '_current' => true, 'product_tab' => $this->getRequest()->getParam('product_tab')]; if (!is_null($attributeSet)) { $requestParams['new_attribute_set_id'] = $attributeSet->getId(); } $resultRedirect->setPath('catalog/product/addAttribute', $requestParams); } elseif ($redirectBack) { $resultRedirect->setPath('catalog/*/edit', ['attribute_id' => $model->getId(), '_current' => true]); } else { $resultRedirect->setPath('catalog/*/'); } return $resultRedirect; } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); $this->_session->setAttributeData($data); return $resultRedirect->setPath('catalog/*/edit', ['attribute_id' => $attributeId, '_current' => true]); } } return $resultRedirect->setPath('catalog/*/'); }
/** * Gets attribute group collection factory * * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection */ public function getAttributeGroupCollectionFactory() { return $this->attrGroupCollectionFactory->create(); }
/** * @param int $attributeSetId * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection */ public function getGroupCollection($attributeSetId) { return $this->_collectionFactory->create()->setAttributeSetFilter($attributeSetId)->setSortOrder()->load(); }
/** * @return $this */ public function loadAttributeGroups() { if ($this->_attributeGroupsById) { return $this; } $attributeSetCollection = $this->_groupCollectionFactory->create()->load(); $this->_attributeGroupsById = []; $this->_attributeGroupsByName = []; foreach ($attributeSetCollection as $id => $attributeGroup) { $attributeSetId = $attributeGroup->getAttributeSetId(); $name = $attributeGroup->getAttributeGroupName(); $this->_attributeGroupsById[$attributeSetId][$id] = $name; $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id; } return $this; }