Example #1
0
 /**
  * Add attribute to attribute set and group
  *
  * @param int $attributeSetId
  * @param Data\Eav\AttributeSet\Attribute $data
  * @return int
  * @throws \Magento\Framework\Exception\InputException
  */
 public function addAttribute($attributeSetId, \Magento\Catalog\Service\V1\Data\Eav\AttributeSet\Attribute $data)
 {
     $attributeSet = $this->setFactory->create()->load($attributeSetId);
     if (!$attributeSet->getId()) {
         throw new InputException('Attribute set does not exist');
     }
     $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
     if ($setEntityType->getEntityTypeCode() != \Magento\Catalog\Model\Product::ENTITY) {
         throw new InputException('Wrong attribute set id provided');
     }
     if (!$this->groupFactory->create()->load($data->getAttributeGroupId())->getId()) {
         throw new InputException('Attribute group does not exist');
     }
     $attribute = $this->attributeFactory->create();
     if (!$attribute->load($data->getAttributeId())->getId()) {
         throw new InputException('Attribute does not exist');
     }
     $attribute->setId($data->getAttributeId());
     $attribute->setEntityTypeId($setEntityType->getId());
     $attribute->setAttributeSetId($attributeSetId);
     $attribute->setAttributeGroupId($data->getAttributeGroupId());
     $attribute->setSortOrder($data->getSortOrder());
     $this->attributeResource->saveInSetIncluding($attribute);
     return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
 }
Example #2
0
 /**
  * Retrieve Attribute Set Group Tree as JSON format
  *
  * @return string
  */
 public function getGroupTreeJson()
 {
     $items = [];
     $setId = $this->_getSetId();
     /* @var $groups \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection */
     $groups = $this->_groupFactory->create()->getResourceCollection()->setAttributeSetFilter($setId)->setSortOrder()->load();
     /* @var $node \Magento\Eav\Model\Entity\Attribute\Group */
     foreach ($groups as $node) {
         $item = [];
         $item['text'] = $node->getAttributeGroupName();
         $item['id'] = $node->getAttributeGroupId();
         $item['cls'] = 'folder';
         $item['allowDrop'] = true;
         $item['allowDrag'] = true;
         $nodeChildren = $this->_collectionFactory->create()->setAttributeGroupFilter($node->getId())->addVisibleFilter()->load();
         if ($nodeChildren->getSize() > 0) {
             $item['children'] = [];
             foreach ($nodeChildren->getItems() as $child) {
                 $item['children'][] = $this->attributeMapper->map($child);
             }
         }
         $items[] = $item;
     }
     return $this->_jsonEncoder->encode($items);
 }
 /**
  * {@inheritdoc}
  */
 public function get($groupId)
 {
     /** @var \Magento\Eav\Model\Entity\Attribute\Group $group */
     $group = $this->groupFactory->create();
     $this->groupResource->load($group, $groupId);
     if (!$group->getId()) {
         throw new NoSuchEntityException(__('Group with id "%1" does not exist.', $groupId));
     }
     return $group;
 }
 /**
  * @param array $group
  * @return Group
  */
 private function initGroupModel($group)
 {
     $modelGroup = $this->_attrGroupFactory->create();
     $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)->setAttributeGroupName($group[1])->setAttributeSetId($this->getId())->setSortOrder($group[2]);
     if ($modelGroup->getId()) {
         $group = $this->_attrGroupFactory->create()->load($modelGroup->getId());
         if ($group->getId()) {
             $modelGroup->setAttributeGroupCode($group->getAttributeGroupCode());
         }
     }
     return $modelGroup;
 }
Example #5
0
 /**
  * Collect data for save
  *
  * @param array $data
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function organizeData($data)
 {
     $modelGroupArray = [];
     $modelAttributeArray = [];
     $attributeIds = [];
     if ($data['attributes']) {
         $ids = [];
         foreach ($data['attributes'] as $attribute) {
             $ids[] = $attribute[0];
         }
         $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids);
     }
     if ($data['groups']) {
         foreach ($data['groups'] as $group) {
             $modelGroup = $this->_attrGroupFactory->create();
             $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)->setAttributeGroupName($group[1])->setAttributeSetId($this->getId())->setSortOrder($group[2]);
             if ($data['attributes']) {
                 foreach ($data['attributes'] as $attribute) {
                     if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) {
                         $modelAttribute = $this->_attributeFactory->create();
                         $modelAttribute->setId($attribute[0])->setAttributeGroupId($attribute[1])->setAttributeSetId($this->getId())->setEntityTypeId($this->getEntityTypeId())->setSortOrder($attribute[2]);
                         $modelAttributeArray[] = $modelAttribute;
                     }
                 }
                 $modelGroup->setAttributes($modelAttributeArray);
                 $modelAttributeArray = [];
             }
             $modelGroupArray[] = $modelGroup;
         }
         $this->setGroups($modelGroupArray);
     }
     if ($data['not_attributes']) {
         $modelAttributeArray = [];
         foreach ($data['not_attributes'] as $attributeId) {
             $modelAttribute = $this->_attributeFactory->create();
             $modelAttribute->setEntityAttributeId($attributeId);
             $modelAttributeArray[] = $modelAttribute;
         }
         $this->setRemoveAttributes($modelAttributeArray);
     }
     if ($data['removeGroups']) {
         $modelGroupArray = [];
         foreach ($data['removeGroups'] as $groupId) {
             $modelGroup = $this->_attrGroupFactory->create();
             $modelGroup->setId($groupId);
             $modelGroupArray[] = $modelGroup;
         }
         $this->setRemoveGroups($modelGroupArray);
     }
     $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId());
     return $this;
 }
 /**
  * @return \Magento\Eav\Model\Entity\Attribute\Group
  */
 protected function getAttributeGroupModel()
 {
     return $this->attributeGroupFactory->create();
 }