TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
SelectViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
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 
96  public function initializeArguments()
97  {
98  parent::initializeArguments();
100  $this->registerTagAttribute('size', 'string', 'Size of input field');
101  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
102  $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.');
103  $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);
104  $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
105  $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
106  $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false);
107  $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', false, false);
108  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
109  $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
110  $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
111  $this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false);
112  }
113 
120  public function render()
121  {
122  $name = $this->getName();
123  if ($this->arguments['multiple']) {
124  $this->tag->addAttribute('multiple', 'multiple');
125  $name .= '[]';
126  }
127  $this->tag->addAttribute('name', $name);
128  $options = $this->getOptions();
129  if (empty($options)) {
130  $options = ['' => ''];
131  }
132 
134  $this->setErrorClassAttribute();
135  $content = '';
136  // register field name for token generation.
137  // in case it is a multi-select, we need to register the field name
138  // as often as there are elements in the box
139  if ($this->arguments['multiple']) {
140  $content .= $this->renderHiddenFieldForEmptyValue();
141  for ($i = 0; $i < count($options); $i++) {
143  }
144  // save the parent field name so that any child f:form.select.option
145  // tag will know to call registerFieldNameForFormTokenGeneration
146  $this->viewHelperVariableContainer->addOrUpdate(
147  static::class,
148  'registerFieldNameForFormTokenGeneration',
149  $name
150  );
151  } else {
153  }
154 
155  $this->viewHelperVariableContainer->addOrUpdate(static::class, 'selectedValue', $this->getSelectedValue());
156  $tagContent = $this->renderOptionTags($options);
157  $childContent = $this->renderChildren();
158  $this->viewHelperVariableContainer->remove(static::class, 'selectedValue');
159  $this->viewHelperVariableContainer->remove(static::class, 'registerFieldNameForFormTokenGeneration');
160  if ($this->arguments['optionsAfterContent']) {
161  $tagContent = $childContent . $tagContent;
162  } else {
163  $tagContent .= $childContent;
164  }
165 
166  $this->tag->setContent($tagContent);
167  $content .= $this->tag->render();
168  return $content;
169  }
170 
177  protected function renderOptionTags($options)
178  {
179  $output = '';
180  if ($this->hasArgument('prependOptionLabel')) {
181  $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
182  $label = $this->arguments['prependOptionLabel'];
183  $output .= $this->renderOptionTag($value, $label, false) . LF;
184  }
185  foreach ($options as $value => $label) {
186  $isSelected = $this->isSelected($value);
187  $output .= $this->renderOptionTag($value, $label, $isSelected) . LF;
188  }
189  return $output;
190  }
191 
197  protected function getOptions()
198  {
199  if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
200  return [];
201  }
202  $options = [];
203  $optionsArgument = $this->arguments['options'];
204  foreach ($optionsArgument as $key => $value) {
205  if (is_object($value) || is_array($value)) {
206  if ($this->hasArgument('optionValueField')) {
207  $key = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
208  if (is_object($key)) {
209  if (method_exists($key, '__toString')) {
210  $key = (string)$key;
211  } else {
212  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Identifying value for object of class "' . get_class($value) . '" was an object.', 1247827428);
213  }
214  }
215  // @todo use $this->persistenceManager->isNewObject() once it is implemented
216  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
217  $key = $this->persistenceManager->getIdentifierByObject($value);
218  } elseif (method_exists($value, '__toString')) {
219  $key = (string)$value;
220  } else {
221  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
222  }
223  if ($this->hasArgument('optionLabelField')) {
224  $value = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
225  if (is_object($value)) {
226  if (method_exists($value, '__toString')) {
227  $value = (string)$value;
228  } else {
229  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);
230  }
231  }
232  } elseif (method_exists($value, '__toString')) {
233  $value = (string)$value;
234  // @todo use $this->persistenceManager->isNewObject() once it is implemented
235  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
236  $value = $this->persistenceManager->getIdentifierByObject($value);
237  }
238  }
239  $options[$key] = $value;
240  }
241  if ($this->arguments['sortByOptionLabel']) {
242  asort($options, SORT_LOCALE_STRING);
243  }
244  return $options;
245  }
246 
253  protected function isSelected($value)
254  {
255  $selectedValue = $this->getSelectedValue();
256  if ($value === $selectedValue || (string)$value === $selectedValue) {
257  return true;
258  }
259  if ($this->hasArgument('multiple')) {
260  if (is_null($selectedValue) && $this->arguments['selectAllByDefault'] === true) {
261  return true;
262  } elseif (is_array($selectedValue) && in_array($value, $selectedValue)) {
263  return true;
264  }
265  }
266  return false;
267  }
268 
274  protected function getSelectedValue()
275  {
276  $this->setRespectSubmittedDataValue(true);
277  $value = $this->getValueAttribute();
278  if (!is_array($value) && !$value instanceof \Traversable) {
279  return $this->getOptionValueScalar($value);
280  }
281  $selectedValues = [];
282  foreach ($value as $selectedValueElement) {
283  $selectedValues[] = $this->getOptionValueScalar($selectedValueElement);
284  }
285  return $selectedValues;
286  }
287 
294  protected function getOptionValueScalar($valueElement)
295  {
296  if (is_object($valueElement)) {
297  if ($this->hasArgument('optionValueField')) {
298  return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']);
299  } else {
300  // @todo use $this->persistenceManager->isNewObject() once it is implemented
301  if ($this->persistenceManager->getIdentifierByObject($valueElement) !== null) {
302  return $this->persistenceManager->getIdentifierByObject($valueElement);
303  } else {
304  return (string)$valueElement;
305  }
306  }
307  } else {
308  return $valueElement;
309  }
310  }
311 
320  protected function renderOptionTag($value, $label, $isSelected)
321  {
322  $output = '<option value="' . htmlspecialchars($value) . '"';
323  if ($isSelected) {
324  $output .= ' selected="selected"';
325  }
326  $output .= '>' . htmlspecialchars($label) . '</option>';
327  return $output;
328  }
329 }
static getPropertyPath($subject, $propertyPath)
registerTagAttribute($name, $type, $description, $required=false, $default=null)