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(&$variables, $hook) {
$element = $variables['element'];
if ($element['#field_name'] === 'field_genre') {
$element[0]['#prefix'] = '<div class="project-info">';
}
if ($element['#field_name'] === 'field_duration') {
$element[0]['#suffix'] = '</div>';
}
}
$element = &$variables['element'];
'#type' => 'textfield'
or'#type' => 'select'
or whatever, that's the element that needs the prefix/suffix$variables['element']
, because in preprocess the fields are already copied to$variables['items']
, see template_preprocess_field().div.field__item
- I'd like to render it outside thediv.field
which is 2 levels above. Is it possible?