Comment's are defining a TwigReference Object in the template and it's getting translated through t() which passes it's args to format_string and needs an array.

Warning: strtr() [function.strtr]: The second argument is not an array in format_string() (line 1563 of core/includes/bootstrap.inc).

format_string('by !author on !date', Object)
t('by !author on !date', Object)
__TwigTemplate_d9c5ed3b99d9a3bc5231873f2a8462bc->doDisplay(Array, Array)
Twig_Template->displayWithErrorHandling(Array, Array)
Twig_Template->display(Array)
Twig_Template->render(Array)
twig_render_template('core/themes/stark/templates/node/node.html.twig', Array)
theme('node', Array)
drupal_render(Array)
drupal_render(Array)
drupal_render(Array)
drupal_render(Array)
drupal_render(Array)
render(Array)
twig_render(Object)
__TwigTemplate_596c0f2c055a0217f4af771691495974->doDisplay(Array, Array)
Twig_Template->displayWithErrorHandling(Array, Array)
Twig_Template->display(Array)
Twig_Template->render(Array)
twig_render_template('core/themes/stark/templates/system/page.html.twig', Array)
theme('page', Array)
drupal_render(Array)
drupal_render_page(Array)
Drupal\Core\EventSubscriber\ViewSubscriber->onHtml(Object)
Drupal\Core\EventSubscriber\ViewSubscriber->onView(Object)
call_user_func(Array, Object)
Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'kernel.view', Object)
Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.view', Object)
Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\HttpKernel->handle(Object, 1, 1)
Symfony\Component\HttpKernel\Kernel->handle(Object)

Comments

joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new448 bytes

Thinking just casting the TwigReference in the t() function to an array since it implements ArrayObject

Thoughts?

dasjo’s picture

Status: Needs review » Needs work
StatusFileSize
new474 bytes

i have updated the patch to use the interface ArrayAccess instead of ArrayObject.

the problem here seems to be array type hinting being semi-compatible with objects that implement ArrayAccess.
we can see the semi-compatibility by the fact that the TwigReference gets passed through just fine, until it hits strtr().

a grep in core for "array $args" reveals that currently 38 functions declare this kind of type hinting, as format_string does.

we might want to deal with this is in core directly, as it might not be a twig issue on its own.
e.g: "How to deal with ArrayAccess when using array type hinting?"

maybe related #1590150: Add ArrayAccess support to drupal_render()

dasjo’s picture

Issue tags: +drupal-at-twig

tagging for sprint

joelpittet’s picture

Yeah your patch make things cleaner, nice work. And I agree, it should go in core.

zakiya’s picture

Assigned: Unassigned » zakiya

Worked For me. I committed this to the front-end branch and will submit a patch for Drupal 8 HEAD.

alexrayu’s picture

It supposed to be an array always, so can just have $args = (array) $args;
Why is it marked "needs work"?

joelpittet’s picture

Priority: Normal » Major
Status: Needs work » Needs review

@alexrayu, there may be a better solution up the call stack but I agree with you, this should really be taken care of and the solution at #2 has my vote because it explicit to what it's fixing.

Straight casting may be too much of a blanket solution and may hide other problems of incorrect objects passed by accident.

Contrived example:

$test = new stdClass();
$test->water = 'hi';
$test->extra = 'read';
$test->data = 'test';

var_dump($test);
var_dump((array)$test);

Changing this to needs review and bumping priority. #2 ftw?

star-szr’s picture

star-szr’s picture

Status: Needs review » Closed (duplicate)
jwilson3’s picture

Issue tags: -drupal-at-twig

remove old tags (this one was only used on two ancient issues).

Project: » Lost & found issues

This issue’s project has disappeared. Most likely, it was a sandbox project, which can be deleted by its maintainer. See the Lost & found issues project page for more details. (The missing project ID was 1750250)