Skip to content

Commit

Permalink
Added method to EavSetup to remove attribute from a group
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stollr committed Feb 11, 2025
1 parent b3c98cd commit 3aeb3b9
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/code/Magento/Eav/Setup/EavSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,43 @@ public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId,
return $this;
}

/**
* Remove an attribute from a group and an attribute set.
*
* @return $this
*/
public function removeAttributeFromGroup(int|string $entityTypeId, int|string $setId, int|string $groupId, int|string $attributeId): static
{
$entityTypeId = $this->getEntityTypeId($entityTypeId);
$setId = $this->getAttributeSetId($entityTypeId, $setId);
$groupId = $this->getAttributeGroupId($entityTypeId, $setId, $groupId);
$attributeId = $this->getAttributeId($entityTypeId, $attributeId);

$table = $this->setup->getTable('eav_entity_attribute');
$select = $this->setup->getConnection()->select()->from($table)
->where('entity_type_id = :entity_type_id')
->where('attribute_set_id = :attribute_set_id')
->where('attribute_group_id = :attribute_group_id')
->where('attribute_id = :attribute_id');

$row = $this->setup->getConnection()->fetchRow($select, [
'entity_type_id' => $entityTypeId,
'attribute_set_id' => $setId,
'attribute_group_id' => $groupId,
'attribute_id' => $attributeId,
]);

if (false === $row) {
return $this;
}

$this->setup->getConnection()->delete($table, [
'entity_attribute_id = ?' => $row['entity_attribute_id'],
]);

return $this;
}

/******************* BULK INSTALL *****************/

/**
Expand Down
85 changes: 85 additions & 0 deletions dev/tests/integration/testsuite/Magento/Eav/Setup/EavSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Eav\Setup;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\TestFramework\Fixture\AppIsolation;

/**
Expand Down Expand Up @@ -123,6 +124,90 @@ public static function addInvalidAttributeThrowExceptionDataProvider()
];
}

/**
* Verify that addAttributeToGroup adds the attribute to the specified group and its attribute set.
*/
#[AppIsolation(true)]
public function testAddAttributeToGroup(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var ModuleDataSetupInterface $setup */
$setup = $objectManager->create(ModuleDataSetupInterface::class);

$attributeData = $this->getAttributeData();
$uniqueAttributeName = 'db24abf125674bceabbbd9977bfc4ada';
$uniqueGroupName = '64a9ed905ca74cbd86533600a3604d43';
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData);
$this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName);

$entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
$groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName);
$attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName);
$select = $setup->getConnection()->select()
->from($setup->getTable('eav_entity_attribute'))
->where('entity_type_id = ?', $entityTypeId)
->where('attribute_set_id = ?', $setId)
->where('attribute_group_id = ?', $groupId)
->where('attribute_id = ?', $attributeId);

// Make sure that the attribute is not assigned to the group already.
$row = $select->query()->fetch();
$this->assertFalse($row);

// The actual action
$this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);

$row = $select->query()->fetch();
$this->assertIsArray($row);
$this->assertSame($entityTypeId, $row['entity_type_id']);
$this->assertSame($setId, $row['attribute_set_id']);
$this->assertSame($groupId, $row['attribute_group_id']);
$this->assertSame($attributeId, $row['attribute_id']);
}

/**
* Verify that testRemoveAttributeFromGroup removes the attribute from the specified group and attribute set.
*/
#[AppIsolation(true)]
public function testRemoveAttributeFromGroup(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var ModuleDataSetupInterface $setup */
$setup = $objectManager->create(ModuleDataSetupInterface::class);

$attributeData = $this->getAttributeData();
$uniqueAttributeName = 'e0db51820df24b6fb6a181571aab8823';
$uniqueGroupName = 'c6771ccb1ab549378a87ecd6cb1d1352';
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData);
$this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName);

$entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
$groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName);
$attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName);
$this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);

$select = $setup->getConnection()->select()
->from($setup->getTable('eav_entity_attribute'))
->where('entity_type_id = ?', $entityTypeId)
->where('attribute_set_id = ?', $setId)
->where('attribute_group_id = ?', $groupId)
->where('attribute_id = ?', $attributeId);

// Make sure that the attribute is assigned to the group.
$row = $select->query()->fetch();
$this->assertIsArray($row);
$this->assertNotEmpty($row['entity_attribute_id']);

// The actual action
$this->eavSetup->removeAttributeFromGroup($entityTypeId, $setId, $groupId, $attributeId);

// Make sure that the attribute was removed from the group
$row = $select->query()->fetch();
$this->assertFalse($row);
}

/**
* Get simple attribute data.
*/
Expand Down

0 comments on commit 3aeb3b9

Please sign in to comment.