Display field 'prefix', 'content' and 'suffix' separatly

Created on 9 February 2024, 10 months ago

Problem/Motivation

I'd like to display separatly the 3 elements in 3 .

<div content="12" class="field__item">
		<span class="prefix">myprefix</span>
		<span class="content">12</span>
		<span class="suffix">mysuffix</span>
</div>

Is this module able to do that?
In the field.html.twig custom template? Or in the node template ?

💬 Support request
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France webmestre

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @webmestre
  • 🇫🇷France webmestre

    This field template works:

    
    {%
      set classes = [
        'field',
        'field--name-' ~ field_name|clean_class,
        'field--type-' ~ field_type|clean_class,
        'field--label-' ~ label_display,
      ]
    %}
    {%
      set title_classes = [
        'field__label',
        label_display == 'visually_hidden' ? 'visually-hidden' : '',
      ]
    %}
    
    {# get prefix and suffix #}
    {% set prefix = element['#items'].getFieldDefinition().getSetting('prefix') ?? '' %}
    {% set suffix = element['#items'].getFieldDefinition().getSetting('suffix') ?? '' %}
    
    {# display the field #}
    <div{{ attributes.addClass(classes) }}>
      <div{{ title_attributes.addClass(title_classes) }}>{{ element['#title'] }}</div>
      {% if multiple %}
        <div class='field__items'>
      {% endif %}
      {% for item in items %}
        <span>{{ prefix }} </span>
        <span{{ item.attributes.addClass('field__item') }}>{{ item.content }}</span>
        <span> {{ suffix }}</span>
      {% endfor %}
      {% if multiple %}
        </div>
      {% endif %}
    </div>
    
    
Production build 0.71.5 2024