Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Form.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class Form extends \Magento\Backend\Block\Widget\Form\Generic
14 {
21 
26 
37  public function __construct(
38  \Magento\Backend\Block\Template\Context $context,
39  \Magento\Framework\Registry $registry,
40  \Magento\Framework\Data\FormFactory $formFactory,
42  \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
43  array $data = []
44  ) {
45  $this->_customerRepository = $customerRepository;
46  $this->_extensibleDataObjectConverter = $extensibleDataObjectConverter;
47  parent::__construct($context, $registry, $formFactory, $data);
48  }
49 
55  protected function _prepareForm()
56  {
58  $form = $this->_formFactory->create(
59  [
60  'data' => [
61  'id' => 'edit_form',
62  'action' => $this->getUrl('customer/*/save'),
63  'method' => 'post',
64  'enctype' => 'multipart/form-data',
65  ],
66  ]
67  );
68 
69  $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
70 
71  if ($customerId) {
72  $form->addField('id', 'hidden', ['name' => 'customer_id']);
73  $customer = $this->_customerRepository->getById($customerId);
74  $form->setValues(
75  $this->_extensibleDataObjectConverter->toFlatArray(
76  $customer,
77  [],
78  \Magento\Customer\Api\Data\CustomerInterface::class
79  )
80  )->addValues(
81  ['customer_id' => $customerId]
82  );
83  }
84 
85  $form->setUseContainer(true);
86  $this->setForm($form);
87  return parent::_prepareForm();
88  }
89 }
$customer
Definition: customers.php:11
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter, array $data=[])
Definition: Form.php:37
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112