TYPO3  7.6
fluid/Classes/ViewHelpers/Form/SelectViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
59 {
63  protected $tagName = 'select';
64 
68  protected $selectedValue = null;
69 
76  public function initializeArguments()
77  {
78  parent::initializeArguments();
80  $this->registerTagAttribute('multiple', 'string', 'if set, multiple select field');
81  $this->registerTagAttribute('size', 'string', 'Size of input field');
82  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
83  $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box', true);
84  $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
85  $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
86  $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false);
87  $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', false, false);
88  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
89  $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
90  $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
91  }
92 
99  public function render()
100  {
101  $name = $this->getName();
102  if ($this->hasArgument('multiple')) {
103  $name .= '[]';
104  }
105  $this->tag->addAttribute('name', $name);
106  $options = $this->getOptions();
107  if (empty($options)) {
108  $options = array('' => '');
109  }
110  $this->tag->setContent($this->renderOptionTags($options));
112  $this->setErrorClassAttribute();
113  $content = '';
114  // register field name for token generation.
115  // in case it is a multi-select, we need to register the field name
116  // as often as there are elements in the box
117  if ($this->hasArgument('multiple') && $this->arguments['multiple'] !== '') {
118  $content .= $this->renderHiddenFieldForEmptyValue();
119  for ($i = 0; $i < count($options); $i++) {
121  }
122  } else {
124  }
125  $content .= $this->tag->render();
126  return $content;
127  }
128 
135  protected function renderOptionTags($options)
136  {
137  $output = '';
138  if ($this->hasArgument('prependOptionLabel')) {
139  $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
140  $label = $this->arguments['prependOptionLabel'];
141  $output .= $this->renderOptionTag($value, $label, false) . LF;
142  }
143  foreach ($options as $value => $label) {
144  $isSelected = $this->isSelected($value);
145  $output .= $this->renderOptionTag($value, $label, $isSelected) . LF;
146  }
147  return $output;
148  }
149 
155  protected function getOptions()
156  {
157  if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
158  return array();
159  }
160  $options = array();
161  $optionsArgument = $this->arguments['options'];
162  foreach ($optionsArgument as $key => $value) {
163  if (is_object($value) || is_array($value)) {
164  if ($this->hasArgument('optionValueField')) {
165  $key = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
166  if (is_object($key)) {
167  if (method_exists($key, '__toString')) {
168  $key = (string)$key;
169  } else {
170  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Identifying value for object of class "' . get_class($value) . '" was an object.', 1247827428);
171  }
172  }
173  // @todo use $this->persistenceManager->isNewObject() once it is implemented
174  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
175  $key = $this->persistenceManager->getIdentifierByObject($value);
176  } elseif (method_exists($value, '__toString')) {
177  $key = (string)$value;
178  } else {
179  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
180  }
181  if ($this->hasArgument('optionLabelField')) {
182  $value = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
183  if (is_object($value)) {
184  if (method_exists($value, '__toString')) {
185  $value = (string)$value;
186  } else {
187  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.', 1247827553);
188  }
189  }
190  } elseif (method_exists($value, '__toString')) {
191  $value = (string)$value;
192  // @todo use $this->persistenceManager->isNewObject() once it is implemented
193  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
194  $value = $this->persistenceManager->getIdentifierByObject($value);
195  }
196  }
197  $options[$key] = $value;
198  }
199  if ($this->arguments['sortByOptionLabel']) {
200  asort($options, SORT_LOCALE_STRING);
201  }
202  return $options;
203  }
204 
211  protected function isSelected($value)
212  {
213  $selectedValue = $this->getSelectedValue();
214  if ($value === $selectedValue || (string)$value === $selectedValue) {
215  return true;
216  }
217  if ($this->hasArgument('multiple')) {
218  if (is_null($selectedValue) && $this->arguments['selectAllByDefault'] === true) {
219  return true;
220  } elseif (is_array($selectedValue) && in_array($value, $selectedValue)) {
221  return true;
222  }
223  }
224  return false;
225  }
226 
232  protected function getSelectedValue()
233  {
234  $this->setRespectSubmittedDataValue(true);
235  $value = $this->getValueAttribute();
236  if (!is_array($value) && !$value instanceof \Traversable) {
237  return $this->getOptionValueScalar($value);
238  }
239  $selectedValues = array();
240  foreach ($value as $selectedValueElement) {
241  $selectedValues[] = $this->getOptionValueScalar($selectedValueElement);
242  }
243  return $selectedValues;
244  }
245 
252  protected function getOptionValueScalar($valueElement)
253  {
254  if (is_object($valueElement)) {
255  if ($this->hasArgument('optionValueField')) {
256  return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']);
257  } else {
258  // @todo use $this->persistenceManager->isNewObject() once it is implemented
259  if ($this->persistenceManager->getIdentifierByObject($valueElement) !== null) {
260  return $this->persistenceManager->getIdentifierByObject($valueElement);
261  } else {
262  return (string)$valueElement;
263  }
264  }
265  } else {
266  return $valueElement;
267  }
268  }
269 
278  protected function renderOptionTag($value, $label, $isSelected)
279  {
280  $output = '<option value="' . htmlspecialchars($value) . '"';
281  if ($isSelected) {
282  $output .= ' selected="selected"';
283  }
284  $output .= '>' . htmlspecialchars($label) . '</option>';
285  return $output;
286  }
287 }