Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Edit.php
Go to the documentation of this file.
1 <?php
7 
11 class Edit extends \Magento\Backend\Block\Widget\Container
12 {
16  private $_selectorBlock;
17 
23  protected $_controller = 'url_rewrite';
24 
30  protected $_buttonsHtml;
31 
37  protected $_adminhtmlData = null;
38 
42  protected $_rewriteFactory;
43 
50  public function __construct(
51  \Magento\Backend\Block\Widget\Context $context,
52  \Magento\UrlRewrite\Model\UrlRewriteFactory $rewriteFactory,
53  \Magento\Backend\Helper\Data $adminhtmlData,
54  array $data = []
55  ) {
56  $this->_rewriteFactory = $rewriteFactory;
57  $this->_adminhtmlData = $adminhtmlData;
58  parent::__construct($context, $data);
59  }
60 
66  protected function _prepareLayout()
67  {
68  $this->setTemplate('Magento_UrlRewrite::edit.phtml');
69 
70  $this->_addBackButton();
71  $this->_prepareLayoutFeatures();
72 
73  return parent::_prepareLayout();
74  }
75 
81  protected function _prepareLayoutFeatures()
82  {
83  if ($this->_getUrlRewrite()->getId()) {
84  $this->_headerText = __('Edit URL Rewrite');
85  } else {
86  $this->_headerText = __('Add New URL Rewrite');
87  }
88 
90  $this->_addEditFormBlock();
91  }
92 
98  protected function _addEditFormBlock()
99  {
100  $this->setChild('form', $this->_createEditFormBlock());
101 
102  if ($this->_getUrlRewrite()->getId()) {
103  $this->_addResetButton();
104  $this->_addDeleteButton();
105  }
106 
107  $this->_addSaveButton();
108  }
109 
115  protected function _addResetButton()
116  {
117  $this->addButton(
118  'reset',
119  [
120  'label' => __('Reset'),
121  'onclick' => 'location.reload();',
122  'class' => 'scalable',
123  'level' => -1
124  ]
125  );
126  }
127 
133  protected function _addBackButton()
134  {
135  $this->addButton(
136  'back',
137  [
138  'label' => __('Back'),
139  'onclick' => 'setLocation(\'' . $this->_adminhtmlData->getUrl('adminhtml/*/') . '\')',
140  'class' => 'back',
141  'level' => -1
142  ]
143  );
144  }
145 
152  protected function _updateBackButtonLink($link)
153  {
154  $this->updateButton('back', 'onclick', 'setLocation(\'' . $link . '\')');
155  }
156 
162  protected function _addDeleteButton()
163  {
164  $this->addButton(
165  'delete',
166  [
167  'label' => __('Delete'),
168  'onclick' => 'deleteConfirm(' . json_encode(__('Are you sure you want to do this?'))
169  . ','
170  . json_encode(
171  $this->_adminhtmlData->getUrl(
172  'adminhtml/*/delete',
173  ['id' => $this->getUrlRewrite()->getId()]
174  )
175  )
176  . ', {data: {}})',
177  'class' => 'scalable delete',
178  'level' => -1
179  ]
180  );
181  }
182 
188  protected function _addSaveButton()
189  {
190  $this->addButton(
191  'save',
192  [
193  'label' => __('Save'),
194  'class' => 'save primary save-url-rewrite',
195  'level' => -1,
196  'data_attribute' => [
197  'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
198  ]
199  ]
200  );
201  }
202 
208  protected function _createEditFormBlock()
209  {
210  return $this->getLayout()->createBlock(
211  \Magento\UrlRewrite\Block\Edit\Form::class,
212  '',
213  ['data' => ['url_rewrite' => $this->_getUrlRewrite()]]
214  );
215  }
216 
222  protected function _addUrlRewriteSelectorBlock()
223  {
224  $this->setChild('selector', $this->_getSelectorBlock());
225  }
226 
232  private function _getSelectorBlock()
233  {
234  if (!$this->_selectorBlock) {
235  $this->_selectorBlock = $this->getLayout()->createBlock(\Magento\UrlRewrite\Block\Selector::class);
236  }
237  return $this->_selectorBlock;
238  }
239 
250  public function getButtonsHtml($area = null)
251  {
252  if (null === $this->_buttonsHtml) {
253  $this->_buttonsHtml = parent::getButtonsHtml();
254  $layout = $this->getLayout();
255  foreach ($this->getChildNames() as $name) {
256  $alias = $layout->getElementAlias($name);
257  if (false !== strpos($alias, '_button')) {
258  $layout->unsetChild($this->getNameInLayout(), $alias);
259  }
260  }
261  }
262  return $this->_buttonsHtml;
263  }
264 
270  protected function _getUrlRewrite()
271  {
272  if (!$this->hasData('url_rewrite')) {
273  $this->setUrlRewrite($this->_rewriteFactory->create());
274  }
275  return $this->getUrlRewrite();
276  }
277 }
__construct(\Magento\Backend\Block\Widget\Context $context, \Magento\UrlRewrite\Model\UrlRewriteFactory $rewriteFactory, \Magento\Backend\Helper\Data $adminhtmlData, array $data=[])
Definition: Edit.php:50
__()
Definition: __.php:13
addButton($buttonId, $data, $level=0, $sortOrder=0, $region='toolbar')
Definition: Container.php:85