{#
/**
 * @file
 * Theme override for a field.
 *
 * To override output, copy the "field.html.twig" from the templates directory
 * to your theme's directory and customize it, just like customizing other
 * Drupal templates such as page.html.twig or node.html.twig.
 *
 * Instead of overriding the theming for all fields, you can also just override
 * theming for a subset of fields using
 * @link themeable Theme hook suggestions. @endlink For example,
 * here are some theme hook suggestions that can be used for a field_foo field
 * on an article node type:
 * - field--node--field-foo--article.html.twig
 * - field--node--field-foo.html.twig
 * - field--node--article.html.twig
 * - field--field-foo.html.twig
 * - field--text-with-summary.html.twig
 * - field.html.twig
 *
 * Available variables:
 * - attributes: HTML attributes for the containing element.
 * - label_hidden: Whether to show the field label or not.
 * - title_attributes: HTML attributes for the title.
 * - label: The label for the field.
 * - multiple: TRUE if a field can contain multiple items.
 * - items: List of all the field items. Each item contains:
 *   - attributes: List of HTML attributes for each item.
 *   - content: The field item's content.
 * - entity_type: The entity type to which the field belongs.
 * - field_name: The name of the field.
 * - field_type: The type of the field.
 * - label_display: The display settings for the label.
 *
 *
 * @see template_preprocess_field()
 */
#}

{% set bem_field_block = bem_field_block|default('field') %}

{% set bem_modifiers = [
  field_name ? 'name-' ~ field_name|clean_class,
  field_type ? 'type-' ~ field_type|clean_class,
  label_display ? 'label-' ~ label_display,
] %}

{% set bem_field_modifiers = bem_field_modifiers|default([]) %}
{% set bem_field_modifiers = bem_field_modifiers|merge(bem_modifiers) %}

{% set bem_field_extra = bem_field_extra|default([])|merge([
  field_name ? field_name|clean_class,
]) %}

<div{{ attributes.addClass(bem(bem_field_block, bem_field_element, bem_field_modifiers, bem_field_extra)) }}>
  {% if not label_hidden %}
    {% if label %}
      {% set title_classes = [
        'field__label'
      ] %}

      {% set label_id = false %}
      {% set heading_display = true %}
      {% set field_heading_level = field_heading_level|default(3) %}

      {% if label_display == 'visually_hidden' %}
        {% set title_classes = title_classes|merge(['visually-hidden']) %}
        {% set heading_display = false %}
        {# if label exists and is visually hidden, need an id for aria-labelledby #}
        {% if heading_id is defined and heading_id %}
          {% set label_id = heading_id ~ '_label' %}
        {% else %}
          {% set label_id = field_name|clean_class ~ '_label' %}
        {% endif %}
      {% endif %}

      {{ include("@rocketship-theme-starter-atoms/02-text/00-headings/_heading.twig", {
        "heading_level": field_heading_level,
        "heading": label,
        "heading_bem_extra": title_classes,
        "heading_id": label_id,
        "heading_display": heading_display,
      }, with_context = false) }}
    {% endif %}
  {% endif %}

  {# just always render the items wrapper, it's easier for theming #}
  {% block field_content %}
    {% if multiple and items|length > 0 %}
      <div class="field__items field__items--name-{{field_name|clean_class}}">
        {% block field_content_items %}
          {% for item in items %}
            {#
              if an inline template is used (e.g. by the title field's heading formatter), we need to check for content
              otherwise that template's html will be rendered even if there is no actual content in it.
            #}
            {% if item.content['#template'] and item.content['#type'] and item.content['#type'] == 'inline_template' %}
              {% if item.content|render|striptags|length %}
                {{ include("@rocketship-theme-starter-atoms/00-field/_field-item.twig", {
                  bem_fielditem_block: bem_field_block,
                  bem_fielditem_element: bem_fielditem_element|default('item'),
                  bem_fielditem_modifiers: bem_field_modifiers,
                  content: item.content,
                }, with_context = false) }}
              {% endif %}
              {# for normal fields, just render as usual #}
            {% else %}
              {{ include("@rocketship-theme-starter-atoms/00-field/_field-item.twig", {
                bem_fielditem_block: bem_field_block,
                bem_fielditem_element: bem_fielditem_element|default('item'),
                bem_fielditem_modifiers: bem_field_modifiers,
                content: item.content,
              }, with_context = false) }}
            {% endif %}
          {% endfor %}
        {% endblock field_content_items %}
      </div>
    {% else %}
      {% for item in items %}
        {#
          If an inline template is used (e.g. by the title field's heading formatter), we need to check for content
          otherwise that template's html will be rendered even if there is no actual content in it.
        #}
        {% if item.content['#template'] and item.content['#type'] and item.content['#type'] == 'inline_template' %}
          {% if item.content|render|striptags|length %}
            {{ item.content }}
          {% endif %}
          {# for normal fields, just render as usual #}
        {% else %}
          {{ item.content }}
        {% endif %}
      {% endfor %}
    {% endif %}
  {% endblock field_content %}
</div>
