Form.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Group\Edit;
  7. use Magento\Customer\Controller\RegistryConstants;
  8. /**
  9. * Adminhtml customer groups edit form
  10. */
  11. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  12. {
  13. /**
  14. * @var \Magento\Tax\Model\TaxClass\Source\Customer
  15. */
  16. protected $_taxCustomer;
  17. /**
  18. * @var \Magento\Tax\Helper\Data
  19. */
  20. protected $_taxHelper;
  21. /**
  22. * @var \Magento\Customer\Api\GroupRepositoryInterface
  23. */
  24. protected $_groupRepository;
  25. /**
  26. * @var \Magento\Customer\Api\Data\GroupInterfaceFactory
  27. */
  28. protected $groupDataFactory;
  29. /**
  30. * @param \Magento\Backend\Block\Template\Context $context
  31. * @param \Magento\Framework\Registry $registry
  32. * @param \Magento\Framework\Data\FormFactory $formFactory
  33. * @param \Magento\Tax\Model\TaxClass\Source\Customer $taxCustomer
  34. * @param \Magento\Tax\Helper\Data $taxHelper
  35. * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
  36. * @param \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory
  37. * @param array $data
  38. */
  39. public function __construct(
  40. \Magento\Backend\Block\Template\Context $context,
  41. \Magento\Framework\Registry $registry,
  42. \Magento\Framework\Data\FormFactory $formFactory,
  43. \Magento\Tax\Model\TaxClass\Source\Customer $taxCustomer,
  44. \Magento\Tax\Helper\Data $taxHelper,
  45. \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
  46. \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory,
  47. array $data = []
  48. ) {
  49. $this->_taxCustomer = $taxCustomer;
  50. $this->_taxHelper = $taxHelper;
  51. $this->_groupRepository = $groupRepository;
  52. $this->groupDataFactory = $groupDataFactory;
  53. parent::__construct($context, $registry, $formFactory, $data);
  54. }
  55. /**
  56. * Prepare form for render
  57. *
  58. * @return void
  59. */
  60. protected function _prepareLayout()
  61. {
  62. parent::_prepareLayout();
  63. /** @var \Magento\Framework\Data\Form $form */
  64. $form = $this->_formFactory->create();
  65. $groupId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_GROUP_ID);
  66. /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
  67. if ($groupId === null) {
  68. $customerGroup = $this->groupDataFactory->create();
  69. $defaultCustomerTaxClass = $this->_taxHelper->getDefaultCustomerTaxClass();
  70. } else {
  71. $customerGroup = $this->_groupRepository->getById($groupId);
  72. $defaultCustomerTaxClass = $customerGroup->getTaxClassId();
  73. }
  74. $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Group Information')]);
  75. $validateClass = sprintf(
  76. 'required-entry validate-length maximum-length-%d',
  77. \Magento\Customer\Model\GroupManagement::GROUP_CODE_MAX_LENGTH
  78. );
  79. $name = $fieldset->addField(
  80. 'customer_group_code',
  81. 'text',
  82. [
  83. 'name' => 'code',
  84. 'label' => __('Group Name'),
  85. 'title' => __('Group Name'),
  86. 'note' => __(
  87. 'Maximum length must be less then %1 characters.',
  88. \Magento\Customer\Model\GroupManagement::GROUP_CODE_MAX_LENGTH
  89. ),
  90. 'class' => $validateClass,
  91. 'required' => true
  92. ]
  93. );
  94. if ($customerGroup->getId() == 0 && $customerGroup->getCode()) {
  95. $name->setDisabled(true);
  96. }
  97. $fieldset->addField(
  98. 'tax_class_id',
  99. 'select',
  100. [
  101. 'name' => 'tax_class',
  102. 'label' => __('Tax Class'),
  103. 'title' => __('Tax Class'),
  104. 'class' => 'required-entry',
  105. 'required' => true,
  106. 'values' => $this->_taxCustomer->toOptionArray(),
  107. ]
  108. );
  109. if ($customerGroup->getId() !== null) {
  110. // If edit add id
  111. $form->addField('id', 'hidden', ['name' => 'id', 'value' => $customerGroup->getId()]);
  112. }
  113. if ($this->_backendSession->getCustomerGroupData()) {
  114. $form->addValues($this->_backendSession->getCustomerGroupData());
  115. $this->_backendSession->setCustomerGroupData(null);
  116. } else {
  117. // TODO: need to figure out how the DATA can work with forms
  118. $form->addValues(
  119. [
  120. 'id' => $customerGroup->getId(),
  121. 'customer_group_code' => $customerGroup->getCode(),
  122. 'tax_class_id' => $defaultCustomerTaxClass,
  123. ]
  124. );
  125. }
  126. $form->setUseContainer(true);
  127. $form->setId('edit_form');
  128. $form->setAction($this->getUrl('customer/*/save'));
  129. $form->setMethod('post');
  130. $this->setForm($form);
  131. }
  132. }