Skip to content

Commit 9cd7680

Browse files
committed
Added method to EavSetup to remove attribute from a group
The EavSetup class provides methods to add EAV attributes, groups and sets. There are also corresponding methods to remove those records. But there is no opposite to `addAttributeToGroup`. This commit adds a `removeAttributeFromGroup` method, which fixes this lack. This addition addresses magento/community-features#346
1 parent 9ee875a commit 9cd7680

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

app/code/Magento/Eav/Setup/EavSetup.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,43 @@ public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId,
13471347
return $this;
13481348
}
13491349

1350+
/**
1351+
* Remove an attribute from a group and an attribute set.
1352+
*
1353+
* @return $this
1354+
*/
1355+
public function removeAttributeFromGroup(int|string $entityTypeId, int|string $setId, int|string $groupId, int|string $attributeId): static
1356+
{
1357+
$entityTypeId = $this->getEntityTypeId($entityTypeId);
1358+
$setId = $this->getAttributeSetId($entityTypeId, $setId);
1359+
$groupId = $this->getAttributeGroupId($entityTypeId, $setId, $groupId);
1360+
$attributeId = $this->getAttributeId($entityTypeId, $attributeId);
1361+
1362+
$table = $this->setup->getTable('eav_entity_attribute');
1363+
$select = $this->setup->getConnection()->select()->from($table)
1364+
->where('entity_type_id = :entity_type_id')
1365+
->where('attribute_set_id = :attribute_set_id')
1366+
->where('attribute_group_id = :attribute_group_id')
1367+
->where('attribute_id = :attribute_id');
1368+
1369+
$row = $this->setup->getConnection()->fetchRow($select, [
1370+
'entity_type_id' => $entityTypeId,
1371+
'attribute_set_id' => $setId,
1372+
'attribute_group_id' => $groupId,
1373+
'attribute_id' => $attributeId,
1374+
]);
1375+
1376+
if (false === $row) {
1377+
return $this;
1378+
}
1379+
1380+
$this->setup->getConnection()->delete($table, [
1381+
'entity_attribute_id = ?' => $row['entity_attribute_id'],
1382+
]);
1383+
1384+
return $this;
1385+
}
1386+
13501387
/******************* BULK INSTALL *****************/
13511388

13521389
/**

dev/tests/integration/testsuite/Magento/Eav/Setup/EavSetupTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Eav\Setup;
77

8+
use Magento\Framework\Setup\ModuleDataSetupInterface;
89
use Magento\TestFramework\Fixture\AppIsolation;
910

1011
/**
@@ -121,6 +122,90 @@ public static function addInvalidAttributeThrowExceptionDataProvider()
121122
];
122123
}
123124

125+
/**
126+
* Verify that addAttributeToGroup adds the attribute to the specified group and its attribute set.
127+
*/
128+
#[AppIsolation(true)]
129+
public function testAddAttributeToGroup(): void
130+
{
131+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
132+
/** @var ModuleDataSetupInterface $setup */
133+
$setup = $objectManager->create(ModuleDataSetupInterface::class);
134+
135+
$attributeData = $this->getAttributeData();
136+
$uniqueAttributeName = 'db24abf125674bceabbbd9977bfc4ada';
137+
$uniqueGroupName = '64a9ed905ca74cbd86533600a3604d43';
138+
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData);
139+
$this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName);
140+
141+
$entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
142+
$setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
143+
$groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName);
144+
$attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName);
145+
$select = $setup->getConnection()->select()
146+
->from($setup->getTable('eav_entity_attribute'))
147+
->where('entity_type_id = ?', $entityTypeId)
148+
->where('attribute_set_id = ?', $setId)
149+
->where('attribute_group_id = ?', $groupId)
150+
->where('attribute_id = ?', $attributeId);
151+
152+
// Make sure that the attribute is not assigned to the group already.
153+
$row = $select->query()->fetch();
154+
$this->assertFalse($row);
155+
156+
// The actual action
157+
$this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);
158+
159+
$row = $select->query()->fetch();
160+
$this->assertIsArray($row);
161+
$this->assertSame($entityTypeId, $row['entity_type_id']);
162+
$this->assertSame($setId, $row['attribute_set_id']);
163+
$this->assertSame($groupId, $row['attribute_group_id']);
164+
$this->assertSame($attributeId, $row['attribute_id']);
165+
}
166+
167+
/**
168+
* Verify that testRemoveAttributeFromGroup removes the attribute from the specified group and attribute set.
169+
*/
170+
#[AppIsolation(true)]
171+
public function testRemoveAttributeFromGroup(): void
172+
{
173+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
174+
/** @var ModuleDataSetupInterface $setup */
175+
$setup = $objectManager->create(ModuleDataSetupInterface::class);
176+
177+
$attributeData = $this->getAttributeData();
178+
$uniqueAttributeName = 'e0db51820df24b6fb6a181571aab8823';
179+
$uniqueGroupName = 'c6771ccb1ab549378a87ecd6cb1d1352';
180+
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData);
181+
$this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName);
182+
183+
$entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
184+
$setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
185+
$groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName);
186+
$attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName);
187+
$this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);
188+
189+
$select = $setup->getConnection()->select()
190+
->from($setup->getTable('eav_entity_attribute'))
191+
->where('entity_type_id = ?', $entityTypeId)
192+
->where('attribute_set_id = ?', $setId)
193+
->where('attribute_group_id = ?', $groupId)
194+
->where('attribute_id = ?', $attributeId);
195+
196+
// Make sure that the attribute is assigned to the group.
197+
$row = $select->query()->fetch();
198+
$this->assertIsArray($row);
199+
$this->assertNotEmpty($row['entity_attribute_id']);
200+
201+
// The actual action
202+
$this->eavSetup->removeAttributeFromGroup($entityTypeId, $setId, $groupId, $attributeId);
203+
204+
// Make sure that the attribute was removed from the group
205+
$row = $select->query()->fetch();
206+
$this->assertFalse($row);
207+
}
208+
124209
/**
125210
* Get simple attribute data.
126211
*/

0 commit comments

Comments
 (0)