The Render API overview contains an example for how to use placeholders:
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...
However, I think this example is incorrect on two counts:
1. '#markup'
is an array of a single string, rather than just a single string.
2. 'placeholders'
is an array of strings, rather than an array of renderable arrays.
It's difficult to devise a straightforward test to be sure that this is a mistake rather than something misinterpreted, but I've been running at the command line:
drush php-eval '$renderable = ["#markup" => ... ]; print \Drupal::service("renderer")->renderRoot($renderable);'
Respectively, the above two points raise the following two warnings:
1. "strlen() expects parameter 1 to be string, array given Unicode.php:686"; printed string is "Array"
2. "Illegal string offset '#create_placeholder' Renderer.php:163" and "Illegal string offset '#cache' Renderer.php:256"; then fatal error "Cannot use string offset as an array in /var/www/web/core/lib/Drupal/Core/Render/Renderer.php on line 256"
The following markup, on the other hand, works fine and generates the expected string "quux":
$renderable = [
"#markup" => "@foo",
"#attached" => [
"placeholders" => [
"@foo" => ["#markup" => "quux"],
]
]
];
Marking this as priority:major as either way it's quite misleading to have this in the worked examples documentation.
Comment | File | Size | Author |
---|---|---|---|
#8 | 2848367-8-render-placeholder-docs-wrong.patch | 962 bytes | mlncn |
Comments
Comment #4
kunalkursija CreditAttribution: kunalkursija at Axelerant commented@jp.stacey Agree with you on:
I tried what is mentioned in the documentation for the placeholder as well as what @jp.stacey has given as an example, Where the later worked without any errors.
Comment #7
mlncn CreditAttribution: mlncn at Agaric for Drutopia, Find It Cambridge, Cambridge, Massachusetts Family Policy Council commentedYes the incorrect documentation has been ruining what should have been a triumphant first release of this module to frustrating debugging, dead-end searching, and finally finding this issue. Thank you jp.stacey!
So making the placeholders strings, as alleged to be allowed, simply brings disaster.
Something like this:
Results in:
(minus an extra hundred lines of backtrace)
Whereas ensuring that each string is a render array:
works.
Comment #8
mlncn CreditAttribution: mlncn at Agaric for Drutopia, Find It Cambridge, Cambridge, Massachusetts Family Policy Council commentedHere's a patch.
Can we backport critical documentation fixes like this to at least 8.2, since for whatever reason that is what the documentation search defaults to?
Comment #10
capysara CreditAttribution: capysara at Bounteous commentedThe patch in #8 still applies cleanly to 8.9.
Comment #12
JamesOakleyJust found this issue having been struggling with why I couldn't get the example to work at https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api....
Thanks
Comment #13
alexpottCommitted 323a808 and pushed to 9.1.x. Thanks!
Will backport once the 9.0.x and 8.9.x branches are open again.
Comment #17
alexpottBackported to 8.9.x and 9.0.x
Comment #18
alexpott