TYPO3 CMS  TYPO3_8-7
SelectViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
79 {
83  protected $tagName = 'select';
84 
88  protected $selectedValue = null;
89 
95  public function initializeArguments()
96  {
97  parent::initializeArguments();
99  $this->registerTagAttribute('size', 'string', 'Size of input field');
100  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
101  $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box. Can be combined with or replaced by child f:form.select.* nodes.');
102  $this->registerArgument('optionsAfterContent', 'boolean', 'If true, places auto-generated option tags after those rendered in the tag content. If false, automatic options come first.', false, false);
103  $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
104  $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
105  $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false);
106  $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', false, false);
107  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
108  $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
109  $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
110  $this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false);
111  }
112 
119  public function render()
120  {
121  $name = $this->getName();
122  if ($this->arguments['multiple']) {
123  $this->tag->addAttribute('multiple', 'multiple');
124  $name .= '[]';
125  }
126  $this->tag->addAttribute('name', $name);
127  $options = $this->getOptions();
128 
130  $this->setErrorClassAttribute();
131  $content = '';
132  // register field name for token generation.
133  // in case it is a multi-select, we need to register the field name
134  // as often as there are elements in the box
135  if ($this->arguments['multiple']) {
136  $content .= $this->renderHiddenFieldForEmptyValue();
137  for ($i = 0; $i < count($options); $i++) {
139  }
140  // save the parent field name so that any child f:form.select.option
141  // tag will know to call registerFieldNameForFormTokenGeneration
142  $this->viewHelperVariableContainer->addOrUpdate(
143  static::class,
144  'registerFieldNameForFormTokenGeneration',
145  $name
146  );
147  } else {
149  }
150 
151  $this->viewHelperVariableContainer->addOrUpdate(static::class, 'selectedValue', $this->getSelectedValue());
152  $prependContent = $this->renderPrependOptionTag();
153  $tagContent = $this->renderOptionTags($options);
154  $childContent = $this->renderChildren();
155  $this->viewHelperVariableContainer->remove(static::class, 'selectedValue');
156  $this->viewHelperVariableContainer->remove(static::class, 'registerFieldNameForFormTokenGeneration');
157  if ($this->arguments['optionsAfterContent']) {
158  $tagContent = $childContent . $tagContent;
159  } else {
160  $tagContent .= $childContent;
161  }
162  $tagContent = $prependContent . $tagContent;
163 
164  $this->tag->forceClosingTag(true);
165  $this->tag->setContent($tagContent);
166  $content .= $this->tag->render();
167  return $content;
168  }
169 
175  protected function renderPrependOptionTag()
176  {
177  $output = '';
178  if ($this->hasArgument('prependOptionLabel')) {
179  $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
180  $label = $this->arguments['prependOptionLabel'];
181  $output .= $this->renderOptionTag($value, $label, false) . LF;
182  }
183  return $output;
184  }
185 
192  protected function renderOptionTags($options)
193  {
194  $output = '';
195  foreach ($options as $value => $label) {
196  $isSelected = $this->isSelected($value);
197  $output .= $this->renderOptionTag($value, $label, $isSelected) . LF;
198  }
199  return $output;
200  }
201 
207  protected function getOptions()
208  {
209  if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
210  return [];
211  }
212  $options = [];
213  $optionsArgument = $this->arguments['options'];
214  foreach ($optionsArgument as $key => $value) {
215  if (is_object($value) || is_array($value)) {
216  if ($this->hasArgument('optionValueField')) {
217  $key = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
218  if (is_object($key)) {
219  if (method_exists($key, '__toString')) {
220  $key = (string)$key;
221  } else {
222  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Identifying value for object of class "' . get_class($value) . '" was an object.', 1247827428);
223  }
224  }
225  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
226  // @todo use $this->persistenceManager->isNewObject() once it is implemented
227  $key = $this->persistenceManager->getIdentifierByObject($value);
228  } elseif (method_exists($value, '__toString')) {
229  $key = (string)$value;
230  } else {
231  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
232  }
233  if ($this->hasArgument('optionLabelField')) {
234  $value = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
235  if (is_object($value)) {
236  if (method_exists($value, '__toString')) {
237  $value = (string)$value;
238  } else {
239  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);
240  }
241  }
242  } elseif (method_exists($value, '__toString')) {
243  $value = (string)$value;
244  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
245  // @todo use $this->persistenceManager->isNewObject() once it is implemented
246  $value = $this->persistenceManager->getIdentifierByObject($value);
247  }
248  }
249  $options[$key] = $value;
250  }
251  if ($this->arguments['sortByOptionLabel']) {
252  asort($options, SORT_LOCALE_STRING);
253  }
254  return $options;
255  }
256 
263  protected function isSelected($value)
264  {
265  $selectedValue = $this->getSelectedValue();
266  if ($value === $selectedValue || (string)$value === $selectedValue) {
267  return true;
268  }
269  if ($this->hasArgument('multiple')) {
270  if (is_null($selectedValue) && $this->arguments['selectAllByDefault'] === true) {
271  return true;
272  }
273  if (is_array($selectedValue) && in_array($value, $selectedValue)) {
274  return true;
275  }
276  }
277  return false;
278  }
279 
285  protected function getSelectedValue()
286  {
287  $this->setRespectSubmittedDataValue(true);
288  $value = $this->getValueAttribute();
289  if (!is_array($value) && !$value instanceof \Traversable) {
290  return $this->getOptionValueScalar($value);
291  }
292  $selectedValues = [];
293  foreach ($value as $selectedValueElement) {
294  $selectedValues[] = $this->getOptionValueScalar($selectedValueElement);
295  }
296  return $selectedValues;
297  }
298 
305  protected function getOptionValueScalar($valueElement)
306  {
307  if (is_object($valueElement)) {
308  if ($this->hasArgument('optionValueField')) {
309  return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']);
310  }
311  // @todo use $this->persistenceManager->isNewObject() once it is implemented
312  if ($this->persistenceManager->getIdentifierByObject($valueElement) !== null) {
313  return $this->persistenceManager->getIdentifierByObject($valueElement);
314  }
315  return (string)$valueElement;
316  }
317  return $valueElement;
318  }
319 
328  protected function renderOptionTag($value, $label, $isSelected)
329  {
330  $output = '<option value="' . htmlspecialchars($value) . '"';
331  if ($isSelected) {
332  $output .= ' selected="selected"';
333  }
334  $output .= '>' . htmlspecialchars($label) . '</option>';
335  return $output;
336  }
337 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)
static getPropertyPath($subject, $propertyPath)