Feature: #110769 - Allow boolean and arbitrary values in HTML attributes 

See forge#110769

Description 

\TYPO3\CMS\Core\Utility\GeneralUtility::implodeAttributes() now has an optional fourth parameter $convertValues . When enabled, values are converted for HTML5 output:

  • true renders an attribute without a value, for example defer instead of defer="defer"
  • false and null omit the attribute
  • objects implementing \Stringable are cast to string
  • other arrays and objects are JSON-encoded and escaped for the attribute
  • all other values are escaped with htmlspecialchars()

Example:

GeneralUtility::implodeAttributes([
    'src' => '/file.js',
    'defer' => true,
    'nomodule' => false,
    'data-config' => ['a' => 1],
], true, false, true);
// src="/file.js" defer data-config="{"a":1}"
Copied!

The option is used when rendering script, link and style tags in \TYPO3\CMS\Core\Page\PageRenderer , \TYPO3\CMS\Core\Page\AssetRenderer and \TYPO3\CMS\Core\Page\JavaScriptRenderer , as well as for the <meta> tags of the meta tag managers, the <html> tag configured via config.htmlTag.attributes and the <video> and <audio> tags.

Impact 

Extension authors can pass boolean values and arrays as tag attributes, for example via tagAttributes of the PageRenderer or the additionalAttributes option of the video and audio renderers.

The default behavior of implodeAttributes() is unchanged.

In the video and audio renderers, true in additionalAttributes now renders an attribute without a value ( playsinline ) instead of playsinline="1" , and false removes the attribute, including default ones such as controls .

Enumerated attributes such as aria-hidden or draggable must still be passed as the strings 'true' or 'false' , since omitting them is not the same as setting them to "false". Attributes without a value are not XML-compliant.