` tags inside a ``. * * ``` * * Option one * * Grouped option one * Grouped option two * * > * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form-select-option * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form-select */ final class OptionViewHelper extends AbstractFormFieldViewHelper { /** * @var string */ protected $tagName = 'option'; public function initializeArguments(): void { $this->registerArgument('selected', 'boolean', 'If set, overrides automatic detection of selected state for this option.'); $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.'); $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.'); $this->registerArgument('value', 'mixed', 'Value to be inserted in HTML tag - must be convertible to string!'); } public function render(): string { $childContent = $this->renderChildren(); $this->tag->setContent((string)$childContent); $value = $this->arguments['value'] ?? $childContent; if ($this->arguments['selected'] ?? $this->isValueSelected((string)$value)) { $this->tag->addAttribute('selected', 'selected'); } $this->tag->addAttribute('value', (string)$value); $parentRequestedFormTokenFieldName = $this->renderingContext->getViewHelperVariableContainer()->get( SelectViewHelper::class, 'registerFieldNameForFormTokenGeneration' ); if ($parentRequestedFormTokenFieldName) { // parent (select field) has requested this option must add one more // entry in the token generation registry for one additional potential // value of the field. Happens when "multiple" is true on parent. $this->registerFieldNameForFormTokenGeneration($parentRequestedFormTokenFieldName); } return $this->tag->render(); } private function isValueSelected(string $value): bool { $selectedValue = $this->renderingContext->getViewHelperVariableContainer()->get(SelectViewHelper::class, 'selectedValue'); if (is_array($selectedValue)) { return in_array($value, array_map(strval(...), $selectedValue), true); } if ($selectedValue instanceof \Iterator) { return in_array($value, array_map(strval(...), iterator_to_array($selectedValue)), true); } return $value === (string)$selectedValue; } }