123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\UrlRewrite\Block;
- /**
- * Block for URL rewrites edit page
- */
- class Edit extends \Magento\Backend\Block\Widget\Container
- {
- /**
- * @var \Magento\UrlRewrite\Block\Selector
- */
- private $_selectorBlock;
- /**
- * Part for building some blocks names
- *
- * @var string
- */
- protected $_controller = 'url_rewrite';
- /**
- * Generated buttons html cache
- *
- * @var string
- */
- protected $_buttonsHtml;
- /**
- * Adminhtml data
- *
- * @var \Magento\Backend\Helper\Data
- */
- protected $_adminhtmlData = null;
- /**
- * @var \Magento\UrlRewrite\Model\UrlRewriteFactory
- */
- protected $_rewriteFactory;
- /**
- * @param \Magento\Backend\Block\Widget\Context $context
- * @param \Magento\UrlRewrite\Model\UrlRewriteFactory $rewriteFactory
- * @param \Magento\Backend\Helper\Data $adminhtmlData
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Widget\Context $context,
- \Magento\UrlRewrite\Model\UrlRewriteFactory $rewriteFactory,
- \Magento\Backend\Helper\Data $adminhtmlData,
- array $data = []
- ) {
- $this->_rewriteFactory = $rewriteFactory;
- $this->_adminhtmlData = $adminhtmlData;
- parent::__construct($context, $data);
- }
- /**
- * Prepare URL rewrite editing layout
- *
- * @return $this
- */
- protected function _prepareLayout()
- {
- $this->setTemplate('Magento_UrlRewrite::edit.phtml');
- $this->_addBackButton();
- $this->_prepareLayoutFeatures();
- return parent::_prepareLayout();
- }
- /**
- * Prepare featured blocks for layout of URL rewrite editing
- *
- * @return void
- */
- protected function _prepareLayoutFeatures()
- {
- if ($this->_getUrlRewrite()->getId()) {
- $this->_headerText = __('Edit URL Rewrite');
- } else {
- $this->_headerText = __('Add New URL Rewrite');
- }
- $this->_addUrlRewriteSelectorBlock();
- $this->_addEditFormBlock();
- }
- /**
- * Add child edit form block
- *
- * @return void
- */
- protected function _addEditFormBlock()
- {
- $this->setChild('form', $this->_createEditFormBlock());
- if ($this->_getUrlRewrite()->getId()) {
- $this->_addResetButton();
- $this->_addDeleteButton();
- }
- $this->_addSaveButton();
- }
- /**
- * Add reset button
- *
- * @return void
- */
- protected function _addResetButton()
- {
- $this->addButton(
- 'reset',
- [
- 'label' => __('Reset'),
- 'onclick' => 'location.reload();',
- 'class' => 'scalable',
- 'level' => -1
- ]
- );
- }
- /**
- * Add back button
- *
- * @return void
- */
- protected function _addBackButton()
- {
- $this->addButton(
- 'back',
- [
- 'label' => __('Back'),
- 'onclick' => 'setLocation(\'' . $this->_adminhtmlData->getUrl('adminhtml/*/') . '\')',
- 'class' => 'back',
- 'level' => -1
- ]
- );
- }
- /**
- * Update Back button location link
- *
- * @param string $link
- * @return void
- */
- protected function _updateBackButtonLink($link)
- {
- $this->updateButton('back', 'onclick', 'setLocation(\'' . $link . '\')');
- }
- /**
- * Add delete button
- *
- * @return void
- */
- protected function _addDeleteButton()
- {
- $this->addButton(
- 'delete',
- [
- 'label' => __('Delete'),
- 'onclick' => 'deleteConfirm(' . json_encode(__('Are you sure you want to do this?'))
- . ','
- . json_encode(
- $this->_adminhtmlData->getUrl(
- 'adminhtml/*/delete',
- ['id' => $this->getUrlRewrite()->getId()]
- )
- )
- . ', {data: {}})',
- 'class' => 'scalable delete',
- 'level' => -1
- ]
- );
- }
- /**
- * Add save button
- *
- * @return void
- */
- protected function _addSaveButton()
- {
- $this->addButton(
- 'save',
- [
- 'label' => __('Save'),
- 'class' => 'save primary save-url-rewrite',
- 'level' => -1,
- 'data_attribute' => [
- 'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
- ]
- ]
- );
- }
- /**
- * Creates edit form block
- *
- * @return \Magento\UrlRewrite\Block\Edit\Form
- */
- protected function _createEditFormBlock()
- {
- return $this->getLayout()->createBlock(
- \Magento\UrlRewrite\Block\Edit\Form::class,
- '',
- ['data' => ['url_rewrite' => $this->_getUrlRewrite()]]
- );
- }
- /**
- * Add child URL rewrite selector block
- *
- * @return void
- */
- protected function _addUrlRewriteSelectorBlock()
- {
- $this->setChild('selector', $this->_getSelectorBlock());
- }
- /**
- * Get selector block
- *
- * @return \Magento\UrlRewrite\Block\Selector
- */
- private function _getSelectorBlock()
- {
- if (!$this->_selectorBlock) {
- $this->_selectorBlock = $this->getLayout()->createBlock(\Magento\UrlRewrite\Block\Selector::class);
- }
- return $this->_selectorBlock;
- }
- /**
- * Get container buttons HTML
- *
- * Since buttons are set as children, we remove them as children after generating them
- * not to duplicate them in future
- *
- * @param string|null $area
- * @return string
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function getButtonsHtml($area = null)
- {
- if (null === $this->_buttonsHtml) {
- $this->_buttonsHtml = parent::getButtonsHtml();
- $layout = $this->getLayout();
- foreach ($this->getChildNames() as $name) {
- $alias = $layout->getElementAlias($name);
- if (false !== strpos($alias, '_button')) {
- $layout->unsetChild($this->getNameInLayout(), $alias);
- }
- }
- }
- return $this->_buttonsHtml;
- }
- /**
- * Get or create new instance of URL rewrite
- *
- * @return \Magento\UrlRewrite\Model\UrlRewrite
- */
- protected function _getUrlRewrite()
- {
- if (!$this->hasData('url_rewrite')) {
- $this->setUrlRewrite($this->_rewriteFactory->create());
- }
- return $this->getUrlRewrite();
- }
- }
|