Problem/Motivation
Twig's set tag can be used to set variables within the context of a template. This can be useful in many cases, for example passing arguments to t().
Without set tag:
<p class="submitted">{{ 'by !author on !date' | t({'!author': author, '!date': created}) }}</p>
With set tag:
{% set args = {'!author': author, '!date': created} %}
<p class="submitted">{{ 'by !author on !date' | t(args) }}</p>
The following snippet will demonstrate the bug if pasted into any Twig template:
{% set args = {'!author': 'foo', '!date': 'bar'} %}
{{ 'by !author on !date' | t(args) }}
That will result in an error like this:Warning: strtr(): The second argument is not an array in format_string() (line 1594 of core/includes/bootstrap.inc).
You can also debug right in the template and see that any array is converted into a TwigReference object. The first example is an indexed array, the second is an associative array. Strings come back fine.
{% set array = ['baz', 'bar'] %}
{% set hash = {'foo': 'bar'} %}
{% set string = "string" %}
<pre>{{ dump(array) }}</pre>
<pre>{{ dump(hash) }}</pre>
<pre>{{ dump(string) }}</pre>
object(Drupal\Core\Template\TwigReference)#626 (2) {
["writableRef":protected]=>
&array(2) {
[0]=>
string(3) "baz"
[1]=>
string(3) "bar"
}
["storage":"ArrayObject":private]=>
array(2) {
[0]=>
string(3) "baz"
[1]=>
string(3) "bar"
}
}
object(Drupal\Core\Template\TwigReference)#604 (2) {
["writableRef":protected]=>
&array(1) {
["foo"]=>
string(3) "bar"
}
["storage":"ArrayObject":private]=>
array(1) {
["foo"]=>
string(3) "bar"
}
}
string(6) "string"
Proposed resolution
TBD
Remaining tasks
- Test needs to be written to demonstrate this bug.
- A fix for this bug needs to be determined and written.
User interface changes
n/a
API changes
TBD