Chooser.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * WYSIWYG widget options form
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Widget\Block\Adminhtml\Widget;
  12. /**
  13. * Chooser widget block.
  14. */
  15. class Chooser extends \Magento\Backend\Block\Template
  16. {
  17. /**
  18. * @var \Magento\Framework\Data\Form\Element\Factory
  19. */
  20. protected $_elementFactory;
  21. /**
  22. * @var \Magento\Framework\Json\EncoderInterface
  23. */
  24. protected $_jsonEncoder;
  25. /**
  26. * @param \Magento\Backend\Block\Template\Context $context
  27. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  28. * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
  29. * @param array $data
  30. */
  31. public function __construct(
  32. \Magento\Backend\Block\Template\Context $context,
  33. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  34. \Magento\Framework\Data\Form\Element\Factory $elementFactory,
  35. array $data = []
  36. ) {
  37. $this->_jsonEncoder = $jsonEncoder;
  38. $this->_elementFactory = $elementFactory;
  39. parent::__construct($context, $data);
  40. }
  41. /**
  42. * Chooser source URL getter
  43. *
  44. * @return string
  45. */
  46. public function getSourceUrl()
  47. {
  48. return $this->_getData('source_url');
  49. }
  50. /**
  51. * Chooser form element getter
  52. *
  53. * @return \Magento\Framework\Data\Form\Element\AbstractElement
  54. */
  55. public function getElement()
  56. {
  57. return $this->_getData('element');
  58. }
  59. /**
  60. * Convert Array config to Object
  61. *
  62. * @return \Magento\Framework\DataObject
  63. */
  64. public function getConfig()
  65. {
  66. if ($this->_getData('config') instanceof \Magento\Framework\DataObject) {
  67. return $this->_getData('config');
  68. }
  69. $configArray = $this->_getData('config');
  70. $config = new \Magento\Framework\DataObject();
  71. $this->setConfig($config);
  72. if (!is_array($configArray)) {
  73. return $this->_getData('config');
  74. }
  75. // define chooser label
  76. if (isset($configArray['label'])) {
  77. $config->setData('label', __($configArray['label']));
  78. }
  79. // chooser control buttons
  80. $buttons = ['open' => __('Choose...'), 'close' => __('Close')];
  81. if (isset($configArray['button']) && is_array($configArray['button'])) {
  82. foreach ($configArray['button'] as $id => $label) {
  83. $buttons[$id] = __($label);
  84. }
  85. }
  86. $config->setButtons($buttons);
  87. return $this->_getData('config');
  88. }
  89. /**
  90. * Unique identifier for block that uses Chooser
  91. *
  92. * @return string
  93. */
  94. public function getUniqId()
  95. {
  96. return $this->_getData('uniq_id');
  97. }
  98. /**
  99. * Form element fieldset id getter for working with form in chooser
  100. *
  101. * @return string
  102. */
  103. public function getFieldsetId()
  104. {
  105. return $this->_getData('fieldset_id');
  106. }
  107. /**
  108. * Flag to indicate include hidden field before chooser or not
  109. *
  110. * @return bool
  111. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  112. */
  113. public function getHiddenEnabled()
  114. {
  115. return $this->hasData('hidden_enabled') ? (bool)$this->_getData('hidden_enabled') : true;
  116. }
  117. /**
  118. * Return chooser HTML and init scripts
  119. *
  120. * @return string
  121. */
  122. protected function _toHtml()
  123. {
  124. $element = $this->getElement();
  125. /* @var $fieldset \Magento\Framework\Data\Form\Element\Fieldset */
  126. $fieldset = $element->getForm()->getElement($this->getFieldsetId());
  127. $chooserId = $this->getUniqId();
  128. $config = $this->getConfig();
  129. // add chooser element to fieldset
  130. $chooser = $fieldset->addField(
  131. 'chooser' . $element->getId(),
  132. 'note',
  133. ['label' => $config->getLabel() ? $config->getLabel() : '', 'value_class' => 'value2']
  134. );
  135. $hiddenHtml = '';
  136. if ($this->getHiddenEnabled()) {
  137. $hidden = $this->_elementFactory->create('hidden', ['data' => $element->getData()]);
  138. $hidden->setId("{$chooserId}value")->setForm($element->getForm());
  139. if ($element->getRequired()) {
  140. $hidden->addClass('required-entry');
  141. }
  142. $hiddenHtml = $hidden->getElementHtml();
  143. $element->setValue('');
  144. }
  145. $buttons = $config->getButtons();
  146. $chooseButton = $this->getLayout()->createBlock(
  147. \Magento\Backend\Block\Widget\Button::class
  148. )->setType(
  149. 'button'
  150. )->setId(
  151. $chooserId . 'control'
  152. )->setClass(
  153. 'btn-chooser'
  154. )->setLabel(
  155. $buttons['open']
  156. )->setOnclick(
  157. $chooserId . '.choose()'
  158. )->setDisabled(
  159. $element->getReadonly()
  160. );
  161. $chooser->setData('after_element_html', $hiddenHtml . $chooseButton->toHtml());
  162. // render label and chooser scripts
  163. $configJson = $this->_jsonEncoder->encode($config->getData());
  164. return '
  165. <label class="widget-option-label" id="' .
  166. $chooserId .
  167. 'label">' .
  168. ($this->getLabel() ? $this->escapeHtml($this->getLabel()) : __(
  169. 'Not Selected'
  170. )) .
  171. '</label>
  172. <div id="' .
  173. $chooserId .
  174. 'advice-container" class="hidden"></div>
  175. <script>
  176. require(["prototype", "mage/adminhtml/wysiwyg/widget"], function(){
  177. //<![CDATA[
  178. (function() {
  179. var instantiateChooser = function() {
  180. window.' .
  181. $chooserId .
  182. ' = new WysiwygWidget.chooser(
  183. "' .
  184. $chooserId .
  185. '",
  186. "' .
  187. $this->getSourceUrl() .
  188. '",
  189. ' .
  190. $configJson .
  191. '
  192. );
  193. if ($("' .
  194. $chooserId .
  195. 'value")) {
  196. $("' .
  197. $chooserId .
  198. 'value").advaiceContainer = "' .
  199. $chooserId .
  200. 'advice-container";
  201. }
  202. }
  203. jQuery(instantiateChooser);
  204. })();
  205. //]]>
  206. });
  207. </script>
  208. ';
  209. }
  210. }