Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Editablemultiselect.php
Go to the documentation of this file.
1 <?php
15 
17 
18 class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multiselect
19 {
23  private $serializer;
24 
34  public function __construct(
35  Factory $factoryElement,
36  CollectionFactory $factoryCollection,
37  Escaper $escaper,
38  array $data = [],
39  \Magento\Framework\Serialize\Serializer\Json $serializer = null
40  ) {
41  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
42  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
43  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
44  }
45 
51  const DEFAULT_ELEMENT_JS_CLASS = 'EditableMultiselect';
52 
59  public function getElementHtml()
60  {
61  $html = parent::getElementHtml();
62 
63  $selectConfig = $this->getData('select_config');
64  if ($this->getData('disabled')) {
65  $selectConfig['is_entity_editable'] = false;
66  }
67 
68  $elementJsClass = self::DEFAULT_ELEMENT_JS_CLASS;
69  if ($this->getData('element_js_class')) {
70  $elementJsClass = $this->getData('element_js_class');
71  }
72 
73  $selectConfigJson = $this->serializer->serialize($selectConfig);
74  $jsObjectName = $this->getJsObjectName();
75 
76  // TODO: TaxRateEditableMultiselect should be moved to a static .js module.
77  $html .= "
78  <script type='text/javascript'>
79  require([
80  'jquery',
81  'jquery/ui'
82  ], function( $ ){
83 
84  function isResolved(){
85  return typeof window['{$elementJsClass}'] !== 'undefined';
86  }
87 
88  function init(){
89  var {$jsObjectName} = new {$elementJsClass}({$selectConfigJson});
90 
91  {$jsObjectName}.init();
92  }
93 
94  function check( tries, delay ){
95  if( isResolved() ){
96  init();
97  }
98  else if( tries-- ){
99  setTimeout( check.bind(this, tries, delay), delay);
100  }
101  else{
102  console.warn( 'Unable to resolve dependency: {$elementJsClass}' );
103  }
104  }
105 
106  check(8, 500);
107 
108  });
109  </script>";
110  return $html;
111  }
112 
120  protected function _optionToHtml($option, $selected)
121  {
122  $html = '<option value="' . $this->_escape($option['value']) . '"';
123  $html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
124  $html .= isset($option['style']) ? 'style="' . $option['style'] . '"' : '';
125  if (in_array((string)$option['value'], $selected)) {
126  $html .= ' selected="selected"';
127  }
128 
129  if ($this->getData('disabled')) {
130  // if element is disabled then no data modification is allowed
131  $html .= ' disabled="disabled" data-is-removable="no" data-is-editable="no"';
132  }
133 
134  $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";
135  return $html;
136  }
137 }
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)