Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function getList($attributeSetId)
 {
     if (!$this->attributeSetFactory->create()->load($attributeSetId)->getId()) {
         throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
     }
     $collection = $this->groupListFactory->create();
     $collection->setAttributeSetFilter($attributeSetId);
     $collection->setSortOrder();
     $groups = array();
     /** @var $group \Magento\Eav\Model\Entity\Attribute\Group */
     foreach ($collection->getItems() as $group) {
         $this->groupBuilder->setId($group->getId())->setName($group->getAttributeGroupName());
         $groups[] = $this->groupBuilder->create();
     }
     return $groups;
 }
Пример #2
0
 /**
  * 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\Resource\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();
 }
Пример #3
0
 /**
  * {@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();
     $collection->setAttributeSetFilter($attributeSetId);
     $collection->setSortOrder();
     $searchResult = $this->searchResultsFactory->create();
     $searchResult->setSearchCriteria($searchCriteria);
     $searchResult->setItems($collection->getItems());
     $searchResult->setTotalCount($collection->getSize());
     return $searchResult;
 }
Пример #4
0
 /**
  * @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\Resource\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/*/');
 }
Пример #5
0
 /**
  * @return \Magento\Eav\Model\Resource\Entity\Attribute\Group\Collection
  */
 public function getAttributeGroupCollectionFactory()
 {
     return $this->_attrGroupCollectionFactory->create();
 }
Пример #6
0
 /**
  * @param int $attributeSetId
  * @return \Magento\Eav\Model\Resource\Entity\Attribute\Group\Collection
  */
 public function getGroupCollection($attributeSetId)
 {
     return $this->_collectionFactory->create()->setAttributeSetFilter($attributeSetId)->setSortOrder()->load();
 }
Пример #7
0
 /**
  * @return $this
  */
 public function loadAttributeGroups()
 {
     if ($this->_attributeGroupsById) {
         return $this;
     }
     $attributeSetCollection = $this->_groupCollectionFactory->create()->load();
     $this->_attributeGroupsById = array();
     $this->_attributeGroupsByName = array();
     foreach ($attributeSetCollection as $id => $attributeGroup) {
         $attributeSetId = $attributeGroup->getAttributeSetId();
         $name = $attributeGroup->getAttributeGroupName();
         $this->_attributeGroupsById[$attributeSetId][$id] = $name;
         $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
     }
     return $this;
 }
Пример #8
0
 /**
  * @return $this
  */
 protected function _prepareLayout()
 {
     $product = $this->getProduct();
     if (!($setId = $product->getAttributeSetId())) {
         $setId = $this->getRequest()->getParam('set', null);
     }
     if ($setId) {
         $groupCollection = $this->_collectionFactory->create()->setAttributeSetFilter($setId)->setSortOrder()->load();
         $tabAttributesBlock = $this->getLayout()->createBlock($this->getAttributeTabBlock(), $this->getNameInLayout() . '_attributes_tab');
         $advancedGroups = array();
         foreach ($groupCollection as $group) {
             /** @var $group \Magento\Eav\Model\Entity\Attribute\Group*/
             $attributes = $product->getAttributes($group->getId(), true);
             foreach ($attributes as $key => $attribute) {
                 $applyTo = $attribute->getApplyTo();
                 if (!$attribute->getIsVisible() || !empty($applyTo) && !in_array($product->getTypeId(), $applyTo)) {
                     unset($attributes[$key]);
                 }
             }
             if ($attributes) {
                 $tabData = array('label' => __($group->getAttributeGroupName()), 'content' => $this->_translateHtml($tabAttributesBlock->setGroup($group)->setGroupAttributes($attributes)->toHtml()), 'class' => 'user-defined', 'group_code' => $group->getTabGroupCode() ?: self::BASIC_TAB_GROUP_CODE);
                 if ($group->getAttributeGroupCode() === 'recurring-payment') {
                     $tabData['parent_tab'] = 'advanced-pricing';
                 }
                 if ($tabData['group_code'] === self::BASIC_TAB_GROUP_CODE) {
                     $this->addTab($group->getAttributeGroupCode(), $tabData);
                 } else {
                     $advancedGroups[$group->getAttributeGroupCode()] = $tabData;
                 }
             }
         }
         /* Don't display website tab for single mode */
         if (!$this->_storeManager->isSingleStoreMode()) {
             $this->addTab('websites', array('label' => __('Websites'), 'content' => $this->_translateHtml($this->getLayout()->createBlock('Magento\\Catalog\\Block\\Adminhtml\\Product\\Edit\\Tab\\Websites')->toHtml()), 'group_code' => self::BASIC_TAB_GROUP_CODE));
         }
         if (isset($advancedGroups['advanced-pricing'])) {
             $this->addTab('advanced-pricing', $advancedGroups['advanced-pricing']);
             unset($advancedGroups['advanced-pricing']);
         }
         if ($this->_moduleManager->isEnabled('Magento_CatalogInventory')) {
             $this->addTab('advanced-inventory', array('label' => __('Advanced Inventory'), 'content' => $this->_translateHtml($this->getLayout()->createBlock('Magento\\Catalog\\Block\\Adminhtml\\Product\\Edit\\Tab\\Inventory')->toHtml()), 'group_code' => self::ADVANCED_TAB_GROUP_CODE));
         }
         /**
          * Do not change this tab id
          * @see \Magento\Bundle\Block\Adminhtml\Catalog\Product\Edit\Tabs
          */
         if ($this->getChildBlock('customer_options')) {
             $this->addTab('customer_options', 'customer_options');
             $this->getChildBlock('customer_options')->setGroupCode(self::ADVANCED_TAB_GROUP_CODE);
         }
         $this->addTab('related', array('label' => __('Related Products'), 'url' => $this->getUrl('catalog/*/related', array('_current' => true)), 'class' => 'ajax', 'group_code' => self::ADVANCED_TAB_GROUP_CODE));
         $this->addTab('upsell', array('label' => __('Up-sells'), 'url' => $this->getUrl('catalog/*/upsell', array('_current' => true)), 'class' => 'ajax', 'group_code' => self::ADVANCED_TAB_GROUP_CODE));
         $this->addTab('crosssell', array('label' => __('Cross-sells'), 'url' => $this->getUrl('catalog/*/crosssell', array('_current' => true)), 'class' => 'ajax', 'group_code' => self::ADVANCED_TAB_GROUP_CODE));
         if (isset($advancedGroups['design'])) {
             $this->addTab('design', $advancedGroups['design']);
             unset($advancedGroups['design']);
         }
         if ($this->getChildBlock('product-alerts')) {
             $this->addTab('product-alerts', 'product-alerts');
             $this->getChildBlock('product-alerts')->setGroupCode(self::ADVANCED_TAB_GROUP_CODE);
         }
         if (isset($advancedGroups['autosettings'])) {
             $this->addTab('autosettings', $advancedGroups['autosettings']);
             unset($advancedGroups['autosettings']);
         }
         foreach ($advancedGroups as $groupCode => $group) {
             $this->addTab($groupCode, $group);
         }
     }
     return parent::_prepareLayout();
 }