Project

General

Profile

Actions

Feature #87994

closed

SelectViewHelper render optgroups

Added by Grigory Rechkin about 5 years ago. Updated almost 5 years ago.

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;
}

Actions #1

Updated by Claus Due about 5 years ago

  • Status changed from New to Resolved
Actions #2

Updated by Benni Mack almost 5 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF