Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Multiselect.php
Go to the documentation of this file.
1 <?php
13 
15 
17 {
24  public function __construct(
25  Factory $factoryElement,
26  CollectionFactory $factoryCollection,
27  Escaper $escaper,
28  $data = []
29  ) {
30  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
31  $this->setType('select');
32  $this->setExtType('multiple');
33  $this->setSize(10);
34  }
35 
41  public function getName()
42  {
43  $name = parent::getName();
44  if (strpos($name, '[]') === false) {
45  $name .= '[]';
46  }
47  return $name;
48  }
49 
55  public function getElementHtml()
56  {
57  $this->addClass('select multiselect admin__control-multiselect');
58  $html = '';
59  if ($this->getCanBeEmpty()) {
60  $html .= '
61  <input type="hidden" id="' . $this->getHtmlId() . '_hidden" name="' . parent::getName() . '" value="" />
62  ';
63  }
64  if (!empty($this->_data['disabled'])) {
65  $html .= '<input type="hidden" name="' . parent::getName() . '_disabled" value="" />';
66  }
67 
68  $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
69  $this->getHtmlAttributes()
70  ) . $this->_getUiId() . ' multiple="multiple">' . "\n";
71 
72  $value = $this->getValue();
73  if (!is_array($value)) {
74  $value = explode(',', $value);
75  }
76 
77  $values = $this->getValues();
78  if ($values) {
79  foreach ($values as $option) {
80  if (is_array($option['value'])) {
81  $html .= '<optgroup label="' . $option['label'] . '">' . "\n";
82  foreach ($option['value'] as $groupItem) {
83  $html .= $this->_optionToHtml($groupItem, $value);
84  }
85  $html .= '</optgroup>' . "\n";
86  } else {
87  $html .= $this->_optionToHtml($option, $value);
88  }
89  }
90  }
91 
92  $html .= '</select>' . "\n";
93  $html .= $this->getAfterElementHtml();
94 
95  return $html;
96  }
97 
103  public function getHtmlAttributes()
104  {
105  return [
106  'title',
107  'class',
108  'style',
109  'onclick',
110  'onchange',
111  'disabled',
112  'size',
113  'tabindex',
114  'data-form-part',
115  'data-role',
116  'data-action'
117  ];
118  }
119 
125  public function getDefaultHtml()
126  {
127  $result = $this->getNoSpan() === true ? '' : '<span class="field-row">' . "\n";
128  $result .= $this->getLabelHtml();
129  $result .= $this->getElementHtml();
130 
131  if ($this->getSelectAll() && $this->getDeselectAll()) {
132  $result .= '<a href="#" onclick="return ' .
133  $this->getJsObjectName() .
134  '.selectAll()">' .
135  $this->getSelectAll() .
136  '</a> <span class="separator">&nbsp;|&nbsp;</span>';
137  $result .= '<a href="#" onclick="return ' .
138  $this->getJsObjectName() .
139  '.deselectAll()">' .
140  $this->getDeselectAll() .
141  '</a>';
142  }
143 
144  $result .= $this->getNoSpan() === true ? '' : '</span>' . "\n";
145 
146  $result .= '<script type="text/javascript">' . "\n";
147  $result .= ' var ' . $this->getJsObjectName() . ' = {' . "\n";
148  $result .= ' selectAll: function() { ' . "\n";
149  $result .= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
150  $result .= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
151  $result .= ' sel.options[i].selected = true; ' . "\n";
152  $result .= ' } ' . "\n";
153  $result .= ' return false; ' . "\n";
154  $result .= ' },' . "\n";
155  $result .= ' deselectAll: function() {' . "\n";
156  $result .= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
157  $result .= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
158  $result .= ' sel.options[i].selected = false; ' . "\n";
159  $result .= ' } ' . "\n";
160  $result .= ' return false; ' . "\n";
161  $result .= ' }' . "\n";
162  $result .= ' }' . "\n";
163  $result .= "\n" . '</script>';
164 
165  return $result;
166  }
167 
173  public function getJsObjectName()
174  {
175  return $this->getHtmlId() . 'ElementControl';
176  }
177 
183  protected function _optionToHtml($option, $selected)
184  {
185  $html = '<option value="' . $this->_escape($option['value']) . '"';
186  $html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
187  $html .= isset($option['style']) ? 'style="' . $option['style'] . '"' : '';
188  if (in_array((string)$option['value'], $selected)) {
189  $html .= ' selected="selected"';
190  }
191  $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";
192  return $html;
193  }
194 }
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, $data=[])
Definition: Multiselect.php:24
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
serialize($attributes=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
if(!isset($_GET['name'])) $name
Definition: log.php:14