Actions
Feature #87994
closedSelectViewHelper render optgroups
Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Fluid
Target version:
-
Start date:
2019-03-25
Due date:
% Done:
0%
Estimated time:
PHP Version:
7.2
Tags:
Complexity:
Sprint Focus:
Description
I was looking for an option to create optgroups from ext:form .yaml configuration. Options are rendered by Form/SelectViewHelper. A part from getOptions() on line 222:
$optionsArgument = $this->arguments['options'];
foreach ($optionsArgument as $key => $value) {
if (is_object($value) || is_array($value)) {
An inner array is allowed but later there's no case to work with it and an exception is thrown on line 238:
throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
And get_class will return an empty string so the exception is not even useful for this case.
Example of form .yaml to reproduce:
renderables:
-
properties:
options:
group1:
value1: label1
value2: label2
group2:
value3: label3
value4: label4
type: SingleSelect
identifier: singleselect-1
I would suggest an additional check:
if (is_array($value)) {
foreach ($value as $innerKey => $innerValue) {
if (!strlen($innerKey) || !strlen($innerValue)) {
throw new \InvalidArgumentException(
'The argument "options" was registered with type "string", but is of type "' .
gettype($innerValue) . '" in view helper "' . get_class($this) . '".',
1256475113
);
}
}
}
And replace renderOptionTags() to create an optgroup tag:
protected function renderOptionTags($options)
{
$output = '';
foreach ($options as $value => $label) {
if (is_array($label)) {
// $value is an optgroup name
// $label is an array of inner options
$output .= '<optgroup label="' . htmlspecialchars($value) .'">';
$output .= $this->renderOptionTags($label);
$output .= '</optgroup>';
} else {
$isSelected = $this->isSelected($value);
$output .= $this->renderOptionTag($value, $label, $isSelected) . LF;
}
}
return $output;
}
Updated by Claus Due over 5 years ago
- Status changed from New to Resolved
See f:form.select.optgroup and f:form.select.option https://github.com/TYPO3/TYPO3.CMS/tree/master/typo3/sysext/fluid/Classes/ViewHelpers/Form/Select
Actions