Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Chooser.php
Go to the documentation of this file.
1 <?php
13 
18 {
22  protected $_elementFactory;
23 
27  protected $_jsonEncoder;
28 
35  public function __construct(
36  \Magento\Backend\Block\Template\Context $context,
37  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
38  \Magento\Framework\Data\Form\Element\Factory $elementFactory,
39  array $data = []
40  ) {
41  $this->_jsonEncoder = $jsonEncoder;
42  $this->_elementFactory = $elementFactory;
43  parent::__construct($context, $data);
44  }
45 
51  public function getSourceUrl()
52  {
53  return $this->_getData('source_url');
54  }
55 
61  public function getElement()
62  {
63  return $this->_getData('element');
64  }
65 
71  public function getConfig()
72  {
73  if ($this->_getData('config') instanceof \Magento\Framework\DataObject) {
74  return $this->_getData('config');
75  }
76 
77  $configArray = $this->_getData('config');
78  $config = new \Magento\Framework\DataObject();
79  $this->setConfig($config);
80  if (!is_array($configArray)) {
81  return $this->_getData('config');
82  }
83 
84  // define chooser label
85  if (isset($configArray['label'])) {
86  $config->setData('label', __($configArray['label']));
87  }
88 
89  // chooser control buttons
90  $buttons = ['open' => __('Choose...'), 'close' => __('Close')];
91  if (isset($configArray['button']) && is_array($configArray['button'])) {
92  foreach ($configArray['button'] as $id => $label) {
93  $buttons[$id] = __($label);
94  }
95  }
96  $config->setButtons($buttons);
97 
98  return $this->_getData('config');
99  }
100 
106  public function getUniqId()
107  {
108  return $this->_getData('uniq_id');
109  }
110 
116  public function getFieldsetId()
117  {
118  return $this->_getData('fieldset_id');
119  }
120 
127  public function getHiddenEnabled()
128  {
129  return $this->hasData('hidden_enabled') ? (bool)$this->_getData('hidden_enabled') : true;
130  }
131 
137  protected function _toHtml()
138  {
139  $element = $this->getElement();
140  /* @var $fieldset \Magento\Framework\Data\Form\Element\Fieldset */
141  $fieldset = $element->getForm()->getElement($this->getFieldsetId());
142  $chooserId = $this->getUniqId();
143  $config = $this->getConfig();
144 
145  // add chooser element to fieldset
146  $chooser = $fieldset->addField(
147  'chooser' . $element->getId(),
148  'note',
149  ['label' => $config->getLabel() ? $config->getLabel() : '', 'value_class' => 'value2']
150  );
151  $hiddenHtml = '';
152  if ($this->getHiddenEnabled()) {
153  $hidden = $this->_elementFactory->create('hidden', ['data' => $element->getData()]);
154  $hidden->setId("{$chooserId}value")->setForm($element->getForm());
155  if ($element->getRequired()) {
156  $hidden->addClass('required-entry');
157  }
158  $hiddenHtml = $hidden->getElementHtml();
159  $element->setValue('');
160  }
161 
162  $buttons = $config->getButtons();
163  $chooseButton = $this->getLayout()->createBlock(
164  \Magento\Backend\Block\Widget\Button::class
165  )->setType(
166  'button'
167  )->setId(
168  $chooserId . 'control'
169  )->setClass(
170  'btn-chooser'
171  )->setLabel(
172  $buttons['open']
173  )->setOnclick(
174  $chooserId . '.choose()'
175  )->setDisabled(
176  $element->getReadonly()
177  );
178  $chooser->setData('after_element_html', $hiddenHtml . $chooseButton->toHtml());
179 
180  // render label and chooser scripts
181  $configJson = $this->_jsonEncoder->encode($config->getData());
182  return '
183  <label class="widget-option-label" id="' .
184  $chooserId .
185  'label">' .
186  ($this->getLabel() ? $this->escapeHtml($this->getLabel()) : __(
187  'Not Selected'
188  )) .
189  '</label>
190  <div id="' .
191  $chooserId .
192  'advice-container" class="hidden"></div>
193  <script>
194  require(["prototype", "mage/adminhtml/wysiwyg/widget"], function(){
195  //<![CDATA[
196  (function() {
197  var instantiateChooser = function() {
198  window.' .
199  $chooserId .
200  ' = new WysiwygWidget.chooser(
201  "' .
202  $chooserId .
203  '",
204  "' .
205  $this->getSourceUrl() .
206  '",
207  ' .
208  $configJson .
209  '
210  );
211  if ($("' .
212  $chooserId .
213  'value")) {
214  $("' .
215  $chooserId .
216  'value").advaiceContainer = "' .
217  $chooserId .
218  'advice-container";
219  }
220  }
221 
222  jQuery(instantiateChooser);
223  })();
224  //]]>
225  });
226  </script>
227  ';
228  }
229 }
$config
Definition: fraud_order.php:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$label
Definition: details.phtml:21
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\Data\Form\Element\Factory $elementFactory, array $data=[])
Definition: Chooser.php:35
$element
Definition: element.phtml:12