Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Eav\Model;
8 
13 
18 {
22  protected $setRepository;
23 
30 
34  protected $eavConfig;
35 
39  protected $entityTypeFactory;
40 
44  protected $groupRepository;
45 
50 
54  protected $attributeResource;
55 
59  private $attributeCollectionFactory;
60 
73  public function __construct(
76  \Magento\Eav\Model\Config $eavConfig,
77  \Magento\Eav\Model\ConfigFactory $entityTypeFactory,
78  \Magento\Eav\Api\AttributeGroupRepositoryInterface $groupRepository,
81  \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory $attributeCollectionFactory = null
82  ) {
83  $this->setRepository = $setRepository;
84  $this->attributeCollection = $attributeCollection;
85  $this->eavConfig = $eavConfig;
86  $this->entityTypeFactory = $entityTypeFactory;
87  $this->groupRepository = $groupRepository;
88  $this->attributeRepository = $attributeRepository;
89  $this->attributeResource = $attributeResource;
90  $this->attributeCollectionFactory = $attributeCollectionFactory ?: ObjectManager::getInstance()
91  ->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class);
92  }
93 
98  {
99  try {
100  $attributeSet = $this->setRepository->get($attributeSetId);
101  } catch (NoSuchEntityException $ex) {
102  throw new NoSuchEntityException(
103  __(
104  'The AttributeSet with a "%1" ID doesn\'t exist. Verify the attributeSet and try again.',
106  )
107  );
108  }
109 
110  $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
111  if ($setEntityType->getEntityTypeCode() != $entityTypeCode) {
112  throw new InputException(__('The attribute set ID is incorrect. Verify the ID and try again.'));
113  }
114 
115  //Check if group exists. If not - expected exception
116  $attributeGroup = $this->groupRepository->get($attributeGroupId);
117 
118  if ($attributeGroup->getAttributeSetId() != $attributeSetId) {
119  throw new InputException(__('The attribute group doesn\'t belong to the attribute set.'));
120  }
121 
123  $attribute = $this->attributeRepository->get($entityTypeCode, $attributeCode);
124 
125  $this->attributeResource->saveInSetIncluding(
126  $attribute,
127  $attribute->getAttributeId(),
130  $sortOrder
131  );
132  $attribute->setAttributeSetId($attributeSetId);
133  return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
134  }
135 
139  public function unassign($attributeSetId, $attributeCode)
140  {
141  try {
142  $attributeSet = $this->setRepository->get($attributeSetId);
143  } catch (NoSuchEntityException $e) {
144  throw new NoSuchEntityException(
145  __('The "%1" attribute set wasn\'t found. Verify and try again.', $attributeSetId)
146  );
147  }
148  $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
149 
151  $attribute = $this->attributeRepository->get($setEntityType->getEntityTypeCode(), $attributeCode);
152 
153  // Check if attribute is in set
154  $attribute->setAttributeSetId($attributeSet->getAttributeSetId());
155  $attribute->loadEntityAttributeIdBySet();
156 
157  if (!$attribute->getEntityAttributeId()) {
158  throw new InputException(
159  __(
160  'The "%1" attribute wasn\'t found in the "%2" attribute set. Enter the attribute and try again.',
163  )
164  );
165  }
166  if (!$attribute->getIsUserDefined()) {
167  throw new StateException(__("The system attribute can't be deleted."));
168  }
169  $attribute->deleteEntity();
170  return true;
171  }
172 
176  public function getAttributes($entityType, $attributeSetId)
177  {
179  $attributeSet = $this->setRepository->get($attributeSetId);
180  $requiredEntityTypeId = $this->eavConfig->getEntityType($entityType)->getId();
181  if (!$attributeSet->getAttributeSetId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) {
182  throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
183  }
184  $attributeCollection = $this->attributeCollectionFactory->create();
185  $attributeCollection->setAttributeSetFilter($attributeSet->getAttributeSetId())->load();
186 
187  return $attributeCollection->getItems();
188  }
189 }
getAttributes($entityTypeCode, $attributeSetId)
$attributeGroupId
__()
Definition: __.php:13
unassign($attributeSetId, $attributeCode)
$attributeCode
Definition: extend.phtml:12
assign($entityTypeCode, $attributeSetId, $attributeGroupId, $attributeCode, $sortOrder)
__construct(\Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributeCollection, \Magento\Eav\Model\Config $eavConfig, \Magento\Eav\Model\ConfigFactory $entityTypeFactory, \Magento\Eav\Api\AttributeGroupRepositoryInterface $groupRepository, \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository, \Magento\Eav\Model\ResourceModel\Entity\Attribute $attributeResource, \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory $attributeCollectionFactory=null)