0

I want to print "your_variable" (array) in my twig template, but i have errors.

I have .theme in my theme :

function MYTHEME_preprocess_views_view_fields(&$vars) {
   $tempstore = \Drupal::service('user.private_tempstore')->get('evenementrimouski');
   $some_data = $tempstore->get('nid');
   $vars['your_variable'][] = array(3,4,5,6);
}

My "views-view-field" twig :

{% for  value in your_variable %}
    {{ value }}
{% endfor %}

The code work but i have errors :

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

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

Someone have idea to resolve this problem ?

I use Drupal 8

1
  • Try $vars['your_variable'] = array(3,4,5,6); ? You are looping over your_variable that is an array containing your array(3,4,5,6)
    – guilhom
    Commented Jul 4, 2017 at 17:26

3 Answers 3

3

Remove the brackets after the ['your_variable'] part as your variable holds only 1 array:

function MYTHEME_preprocess_views_view_fields(&$vars) {
   $tempstore = \Drupal::service('user.private_tempstore')->get('evenementrimouski');
   $some_data = $tempstore->get('nid');
   $vars['your_variable'] = [3,4,5,6];
}

Also use the short array syntax. It's part of the new coding standards: https://www.drupal.org/node/2135291

0

To answer your question, the error you are seeing is because $vars['your_variable'] is an array containing an array. var_dump($vars) would output the following.

array(1) {
  ["your_variable"]=>
  array(1) {
    [0]=>
    array(4) {
      [0]=>
      int(3)
      [1]=>
      int(4)
      [2]=>
      int(5)
      [3]=>
      int(6)
    }
  }
}

In that case, the array is expected to be a render array. That is way you get the error you see.

I assume you aren't showing the code you are using, though, since defining a literal array in a preprocess function to print it with Twig doesn't make much sense. At least, you could do that directly in Twig.

{% set your_variable = [3, 4, 5, 6] %}
{% for  value in your_variable %}
    {{ value }}
{% endfor %}

Does $tempstore->get('nid') returns an array of integers that you need to assign to $vars['your_variable'], and print it with Twig? Just use the following code.

function MYTHEME_preprocess_views_view_fields(&$vars) {
   $tempstore = \Drupal::service('user.private_tempstore')->get('evenementrimouski');
   $vars['your_variable'] = $tempstore->get('nid');
}

Are you creating an array of integers by merging different arrays of integers? Merge $vars['your_variable'] with the other arrays.

// Before you initialize $vars['your_variable'] to an empty array.
// $vars['your_variable'] = [];
$vars['your_variable'] = array_merge($vars['your_variable'], $your_other_array_of_integers);

Are you adding integers to $vars['your_variable'], which will be an array of integers?

// Before you initialize $vars['your_variable'] to an empty array.
// $vars['your_variable'] = [];
$vars['your_variable'][] = $your_integer;
0

Often is a situation that you would need to iterate an array by keys, so in this context, be sure to create the multidimensional array ( keep the appended [] ) and then use a key to iterate, such as:

{% for key,collection in collections %}
  <div>
    {{ collection[key] }}
  </div>
{%  endfor %}

And the preprocess function would look something very similar as written in the original question where 'your_variable' in this case would be collections, and the collections array would be populated using a for/foreach within that function such as:

 function mytheme_preprocess_node(&$vars) { 
   $collection = $vars['content']['field_story']; 
   $item_count = $collection['#items']->count(); 
   for ($i=0 ; $i < $item_count ; $i++) { 
     $vars['story_title'][] = $collection[$i]['#title']; 
   } 
 }

...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.