We are making a new website from an old Drupal 7 project. For each item within a view, I need to check if a file exists in order to enable or disable a link. In Drupal 7 we used file_exists() within the PHP template but we can’t do that anymore with Twig.
I have opted for using the template_preprocess_views_view_unformatted(&$variables) function but I don’t succeed in setting the variable properly.
function bootstrap5_preprocess_views_view_unformatted(&$variables) { $variables['#cache']['max-age'] = 0; $id_view = $variables['view']->id(); // echo($id_view); switch ($id_view) { case 'recent_incidents_block': foreach ($variables['rows'] as $key => &$value) { // $value['content']['#row']->file_exists= "test"; $variables['rows'][$key]['file_exists'] = "test"; } break; } }
Then in the view template:
{% for row in rows %} {% set file_exists = row.file_exists.value %}
But nothing gets printed with {{file_exists}}