I have to filter out some view results programmatically because the filter is more complicated than what the build-in filter can handle. Now in the template file I've created for this view, it must display 3 items regardless of if 3 view result rows exist, if a view result row doesn't exist it displays a placeholder.
I've been able to filter out the rows by creating a temp array and setting $view->result to that, however, after resetting the array keys to be a normal order (0, 1, 2, ...) the template file is unable to access those...
Here's what my custom filter does
function MY_MODULE_views_post_execute(ViewExecutable $view) {
switch($view->id()) {
case 'my_custom_view': {
$temp = [];
$temp_index = 0;
foreach($view->result as $key => $value) {
if (<custom_filter>) {
$temp[$temp_index] = $value;
$temp_index++;
}
}
$view->result = $temp;
$view->save();
break;
}
}
}
Now in the template, I do something like:
{% for i in 0..2 %}
<div class="col-12 col-lg-4">
<div class="match-card">
{% if rows[i] is defined %}
{{ rows[i].content }}
{% else %}
<div class="empty-match">
{{ "Place Holder"|t }}
</div>
{% endif %}
</div>
</div>
{% endfor %}I've printed out `$view->result` after setting it to $temp and it would show 3 different rows with the expected data. However, in the template rows 0, 1 and 2 all exist, but 1 and 2 have empty fields. All fields for row 1 and 2 are empty even though in the hook_views_post_execute they exist...
Now if I just unset the rows I want to filter out and loop through an unspecified number of rows, the fields exist like I expect.
It seems to be if I change the key for a row in `$view->result` that particular row exists but with empty fields.