I want to pass an array and a form to a template file, using this code.
public function buildForm(array $form, FormStateInterface $form_state) {
$form['job_locations'] = $job_locations;
$form['#theme'] = 'search_form';
$form['#method'] = 'get';
// Omissis
return $form;
}
The value of $job_locations
is the following.
Array(
[0] => Array(
[city] => Singapore
[count] => 5
)
[1] => Array(
[city] => Toronto
[count] => 3
)
)
The content of the template file is the following.
{% for location in form.job_locations %}
{% if location.city %}
<li data-original-index="0">
<a tabindex="0" class="location-list-item" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
<span class="text">{{ location.city }}</span>
</a>
</li>
{% endif %}
{% endfor %}
Everything is shown correctly, but the code I am using causes these errors.
User error: "count" is an invalid render array key in Drupal\Core\Render\Element::children()
User error: "city" is an invalid render array key in Drupal\Core\Render\Element::children()
How can I avoid to get these errors?