1
votes

Hello: I've scoured through a number of documents on the Drupal site and twig documentation and am stuck. I am getting the error:

User error: "foo" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core\lib\Drupal\Core\Render\Element.php). User error: "bar" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core\lib\Drupal\Core\Render\Element.php).

I am trying to pass an array into the twig template variable:

$array = array(
        "foo" => "bar",
        "bar" => "foo",
    );

$element = array(
'#theme' => 'event_formatter',
'#events_result' => $array,
);

And the configuration in the .module file looks like:

    return array(
    'event_formatter' => array(
        'variables' => array('events_result' => []),
        'template' => 'event-formatter',
    ),

Passing a string variable seems fine.

Thanks!

1
seems to me its a drupal problem though. Passing any variable to twig can be doneDarkBee

1 Answers

0
votes

Are you returning $element?

Here's an example from one of my modules:

#ChallengeController

$challengeService = $this->challengeService;

return [
  '#theme'   => 'my_module_challenge_list',
  '#challenges' => $challengeService->getTrainerChallenges()
];

then

#my_module.module
  'my_module_challenge_list' => array(
      'variables' => array('challenges' => NULL),
    ),