Edit.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Config edit page
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Config\Block\System\Config;
  12. use \Magento\Framework\App\ObjectManager;
  13. use \Magento\Framework\Serialize\Serializer\Json;
  14. /**
  15. * @api
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * @since 100.0.2
  18. */
  19. class Edit extends \Magento\Backend\Block\Widget
  20. {
  21. const DEFAULT_SECTION_BLOCK = \Magento\Config\Block\System\Config\Form::class;
  22. /**
  23. * Form block class name
  24. *
  25. * @var string
  26. */
  27. protected $_formBlockName;
  28. /**
  29. * Block template File
  30. *
  31. * @var string
  32. */
  33. protected $_template = 'Magento_Config::system/config/edit.phtml';
  34. /**
  35. * Configuration structure
  36. *
  37. * @var \Magento\Config\Model\Config\Structure
  38. */
  39. protected $_configStructure;
  40. /**
  41. * @var \Magento\Framework\Serialize\Serializer\Json
  42. */
  43. private $jsonSerializer;
  44. /**
  45. * @param \Magento\Backend\Block\Template\Context $context
  46. * @param \Magento\Config\Model\Config\Structure $configStructure
  47. * @param array $data
  48. * @param Json|null $jsonSerializer
  49. */
  50. public function __construct(
  51. \Magento\Backend\Block\Template\Context $context,
  52. \Magento\Config\Model\Config\Structure $configStructure,
  53. array $data = [],
  54. Json $jsonSerializer = null
  55. ) {
  56. $this->_configStructure = $configStructure;
  57. $this->jsonSerializer = $jsonSerializer ?: ObjectManager::getInstance()->get(Json::class);
  58. parent::__construct($context, $data);
  59. }
  60. /**
  61. * Prepare layout object
  62. *
  63. * @return \Magento\Framework\View\Element\AbstractBlock
  64. */
  65. protected function _prepareLayout()
  66. {
  67. /** @var $section \Magento\Config\Model\Config\Structure\Element\Section */
  68. $section = $this->_configStructure->getElement($this->getRequest()->getParam('section'));
  69. $this->_formBlockName = $section->getFrontendModel();
  70. if (empty($this->_formBlockName)) {
  71. $this->_formBlockName = self::DEFAULT_SECTION_BLOCK;
  72. }
  73. $this->setTitle($section->getLabel());
  74. $this->setHeaderCss($section->getHeaderCss());
  75. $this->getToolbar()->addChild(
  76. 'save_button',
  77. \Magento\Backend\Block\Widget\Button::class,
  78. [
  79. 'id' => 'save',
  80. 'label' => __('Save Config'),
  81. 'class' => 'save primary',
  82. 'data_attribute' => [
  83. 'mage-init' => ['button' => ['event' => 'save', 'target' => '#config-edit-form']],
  84. ]
  85. ]
  86. );
  87. $block = $this->getLayout()->createBlock($this->_formBlockName);
  88. $this->setChild('form', $block);
  89. return parent::_prepareLayout();
  90. }
  91. /**
  92. * Retrieve rendered save buttons
  93. *
  94. * @return string
  95. */
  96. public function getSaveButtonHtml()
  97. {
  98. return $this->getChildHtml('save_button');
  99. }
  100. /**
  101. * Retrieve config save url
  102. *
  103. * @return string
  104. */
  105. public function getSaveUrl()
  106. {
  107. return $this->getUrl('*/system_config/save', ['_current' => true]);
  108. }
  109. /**
  110. * @return string
  111. * @since 101.1.0
  112. */
  113. public function getConfigSearchParamsJson()
  114. {
  115. $params = [];
  116. if ($this->getRequest()->getParam('section')) {
  117. $params['section'] = $this->getRequest()->getParam('section');
  118. }
  119. if ($this->getRequest()->getParam('group')) {
  120. $params['group'] = $this->getRequest()->getParam('group');
  121. }
  122. if ($this->getRequest()->getParam('field')) {
  123. $params['field'] = $this->getRequest()->getParam('field');
  124. }
  125. return $this->jsonSerializer->serialize($params);
  126. }
  127. }