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 
13 class Form extends \Magento\Backend\Block\Widget\Form\Generic
14 {
18  protected $_systemStore;
19 
27  public function __construct(
28  \Magento\Backend\Block\Template\Context $context,
29  \Magento\Framework\Registry $registry,
30  \Magento\Framework\Data\FormFactory $formFactory,
31  \Magento\Store\Model\System\Store $systemStore,
32  array $data = []
33  ) {
34  $this->_systemStore = $systemStore;
35  parent::__construct($context, $registry, $formFactory, $data);
36  }
37 
43  protected function _construct()
44  {
45  parent::_construct();
46  $this->setId('sitemap_form');
47  $this->setTitle(__('Sitemap Information'));
48  }
49 
53  protected function _prepareForm()
54  {
55  $model = $this->_coreRegistry->registry('sitemap_sitemap');
56 
58  $form = $this->_formFactory->create(
59  ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
60  );
61 
62  $fieldset = $form->addFieldset('add_sitemap_form', ['legend' => __('Sitemap')]);
63 
64  if ($model->getId()) {
65  $fieldset->addField('sitemap_id', 'hidden', ['name' => 'sitemap_id']);
66  }
67 
68  $fieldset->addField(
69  'sitemap_filename',
70  'text',
71  [
72  'label' => __('Filename'),
73  'name' => 'sitemap_filename',
74  'required' => true,
75  'note' => __('example: sitemap.xml'),
76  'value' => $model->getSitemapFilename()
77  ]
78  );
79 
80  $fieldset->addField(
81  'sitemap_path',
82  'text',
83  [
84  'label' => __('Path'),
85  'name' => 'sitemap_path',
86  'required' => true,
87  'note' => __('example: "/sitemap/" or "/" for base path (path must be writeable)'),
88  'value' => $model->getSitemapPath()
89  ]
90  );
91 
92  if (!$this->_storeManager->hasSingleStore()) {
93  $field = $fieldset->addField(
94  'store_id',
95  'select',
96  [
97  'label' => __('Store View'),
98  'title' => __('Store View'),
99  'name' => 'store_id',
100  'required' => true,
101  'value' => $model->getStoreId(),
102  'values' => $this->_systemStore->getStoreValuesForForm()
103  ]
104  );
105  $renderer = $this->getLayout()->createBlock(
106  \Magento\Backend\Block\Store\Switcher\Form\Renderer\Fieldset\Element::class
107  );
108  $field->setRenderer($renderer);
109  } else {
110  $fieldset->addField(
111  'store_id',
112  'hidden',
113  ['name' => 'store_id', 'value' => $this->_storeManager->getStore(true)->getId()]
114  );
115  $model->setStoreId($this->_storeManager->getStore(true)->getId());
116  }
117 
118  $form->setValues($model->getData());
119  $form->setUseContainer(true);
120  $this->setForm($form);
121 
122  return parent::_prepareForm();
123  }
124 }
getData($key='', $index=null)
Definition: DataObject.php:119
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Store\Model\System\Store $systemStore, array $data=[])
Definition: Form.php:27