#90181TagBuilder does not support boolean attributes without values
Description
TagBuilder::addAttribute() currently cannot add boolean attributes without a value, like:
<input disabled>
To add such an attribute with the TagBuilder, you would currently be required to write something like:
$this->tag->addAttribute('disabled', 'disabled');
which results in:
<input disabled="disabled">
Which is valid, but unnecessary.
Reviews 1
Related issues 3
History 9
-
Claus Due commented <span data-processor="relativetime" data-value="2020-02-15T20:46:48+00:00" > 02/15/20, 20:46 </span>
You may like https://forge.typo3.org/issues/90386 and https://review.typo3.org/c/Packages/TYPO3.CMS/+/63264 as substitutes for needing to change API or PHP code for this purpose.
-
Christian Eßl commented <span data-processor="relativetime" data-value="2020-02-15T20:47:46+00:00" > 02/15/20, 20:47 </span>
Thanks, did not notice this feature. This solves the problem.
-
Georg Ringer changed Related to #90386 <span data-processor="relativetime" data-value="2020-03-02T14:55:39+00:00" > 03/02/20, 14:55 </span>
-
Georg Ringer commented <span data-processor="relativetime" data-value="2020-03-02T14:58:00+00:00" > 03/02/20, 14:58 </span>
As the mentioned patch is still heavily WIP I would propose to hardcode those values which are boolean
"allowfullscreen", "allowpaymentrequest", "async", "autofocus", "autoplay", "checked", "controls", "default", "defer", "disabled", "formnovalidate", "hidden", "ismap", "itemscope", "loop", "multiple", "muted", "nomodule", "novalidate", "open", "readonly", "required", "reversed", "selected", "typemustmatch" -
Claus Due commented and changed status
NewAccepted <span data-processor="relativetime" data-value="2020-03-07T10:26:11+00:00" > 03/07/20, 10:26 </span>A hardcoded list of attributes is not advisable and would require a change in Fluid, but would be rejected there – arguing that not all XML-like output formats require or are even compatible with XHTML boolean attributes.
Suggested approach in TYPO3 CMS:
- Do not declare more ViewHelper arguments on tag-based ViewHelpers as strictly “boolean” and do not implement the special logic that would be required to assign the attribute with a string value if TRUE. This makes the API of said ViewHelpers different from standard XHTML which introduces possible confusion (why do I need to pass TRUE in Fluid when I need to pass a string value in XHTML?)
- Instead, adjust ViewHelpers which require this type of logic, to use “ignoreEmptyAttributes” on TagBuilder to prevent rendering attributes when their value is an empty string.
- Possibly also remove the argument definition and implement “handleAdditionalArguments” to pass undeclared arguments (or only a specific list of such arguments) directly as tag attributes (this might be considered a FEATURE and as such it might be skipped for v10 LTS)
Alternative for Fluid library:
- Feel free to open a pull request that changes default value of “ignoreEmptyAttributes” on TagBuilder to TRUE.
- But be aware that this would considered breaking, meaning it would only be implemented in Fluid 3.x branches.
-
Georg Ringer changed Related to #90871 <span data-processor="relativetime" data-value="2020-03-28T19:31:02+00:00" > 03/28/20, 19:31 </span>
-
Česlav Przywara changed Related to #94927 <span data-processor="relativetime" data-value="2021-08-18T14:03:27+00:00" > 08/18/21, 14:03 </span>
-
Georg Ringer commented and changed status
AcceptedClosed <span data-processor="relativetime" data-value="2026-10-06T04:44:16+00:00" > 10/06/26, 04:44 </span>closing the feature request to cleanup forge a bit. what is working is using
addAttribute('disabled', true)
Have you checked/confirmed if using `ignoreEmptyAttributes` on TagBuilder makes the boolean attribute “remove itself” as it should?