Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
 - replaced some of the deprecated calls
 - performed DRY optimizations

Signed-off-by: Tomash Khamlai <[email protected]>
  • Loading branch information
TomashKhamlai committed Oct 21, 2019
1 parent caafd17 commit 8fdf247
Showing 1 changed file with 32 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,37 @@
* See COPYING.txt for license details.
*/

/** @var \Magento\Customer\Model\Attribute $model1 */
$model1 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
$model1->setName(
'custom_attribute1'
)->setEntityTypeId(
2
)->setAttributeSetId(
2
)->setAttributeGroupId(
1
)->setFrontendInput(
'text'
)->setFrontendLabel(
'custom_attribute_frontend_label'
)->setIsUserDefined(
1
);
$model1->save();

/** @var \Magento\Customer\Model\Attribute $model2 */
$model2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
$model2->setName(
'custom_attribute2'
)->setEntityTypeId(
2
)->setAttributeSetId(
2
)->setAttributeGroupId(
1
)->setFrontendInput(
'text'
)->setFrontendLabel(
'custom_attribute_frontend_label'
)->setIsUserDefined(
1
);
$model2->save();
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var \Magento\Customer\Model\AttributeFactory $attributeFactory */
$attributeFactory = $objectManager->create(\Magento\Customer\Model\AttributeFactory::class);

/** @var \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository */
$attributeRepository = $objectManager->create(\Magento\Eav\Api\AttributeRepositoryInterface::class);

/** @var \Magento\Customer\Setup\CustomerSetup $setupResource */
$setupResource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Customer\Setup\CustomerSetup::class
);

$data1 = [['form_code' => 'customer_address_edit', 'attribute_id' => $model1->getAttributeId()]];
$setupResource->getSetup()->getConnection()->insertMultiple(
$setupResource->getSetup()->getTable('customer_form_attribute'),
$data1
);

$data2 = [['form_code' => 'customer_address_edit', 'attribute_id' => $model2->getAttributeId()]];
$setupResource->getSetup()->getConnection()->insertMultiple(
$setupResource->getSetup()->getTable('customer_form_attribute'),
$data2
);
$setupResource = $objectManager->create(\Magento\Customer\Setup\CustomerSetup::class);

$attributeNames = ['custom_attribute1', 'custom_attribute2'];
foreach ($attributeNames as $attributeName) {
/** @var \Magento\Customer\Model\Attribute $attribute */
$attribute = $attributeFactory->create();

$attribute->setName($attributeName)
->setEntityTypeId(2)
->setAttributeSetId(2)
->setAttributeGroupId(1)
->setFrontendInput('text')
->setFrontendLabel('custom_attribute_frontend_label')
->setIsUserDefined(true);

$attributeRepository->save($attribute);

$setupResource->getSetup()
->getConnection()
->insertMultiple(
$setupResource->getSetup()->getTable('customer_form_attribute'),
[['form_code' => 'customer_address_edit', 'attribute_id' => $attribute->getAttributeId()]]
);
}

0 comments on commit 8fdf247

Please sign in to comment.