Add prefix / suffix to field by using template_preprocess_field I remember in Drupal 7, this was possible. I need to add some HTML before a field and I don't want to edit it in the Twig file. Is it somehow possible to add prefixes / suffixes by using the template_preprocess_field function?

I already tried something like that, but with no success:

function MYTHEME_preprocess_field(&amp;$variables, $hook) {
    $element = $variables['element'];
    if ($element['#field_name'] === 'field_genre') {
        $element[0]['#prefix'] = '&lt;div class="project-info"&gt;';
    }
    if ($element['#field_name'] === 'field_duration') {
        $element[0]['#suffix'] = '&lt;/div&gt;';
    }
}
to prefix a paragraph field, this needs to be put into mytheme.theme .

function mytheme_preprocess_paragraph(&amp;$vars) {
  if (isset($vars['elements']['#paragraph']) &amp;&amp; ($vars['elements']['#paragraph']-&gt;getType() == 'hp_dean')){
    //kint($vars);
    $vars['content']['field_hp_dean_title']['#prefix'] = '&lt;div class="cont-wrap"&gt;';
    $vars['content']['field_hp_dean_more']['#suffix'] = '&lt;/div&gt;';
  }
}


you can adjust this to your specific hook, like hook_preprocess_node and similar .