Merge on ds-field-expert on item attributes accumulates

Created on 27 December 2024, 2 months ago

The ds-field-expert mergeAttribute accumulates the values in field_item_wrapper_attributes.

In my use case, I added a preprocess for ds_field_expert on each item.attributes another class node_status_unpublished to set whether the node is published or unpublished.

With the current code, I will have rows that contain both node_status_unpublished and node_status_unpublished. My expectation is that on every row iteration, field_item_wrapper_attributes "resets".

/**
 * Implements hook_preprocess_field().
 */
function mymodule_preprocess_field__ds_field_expert(array &$variables) {
  if ($variables['field_type'] == 'entity_reference') {
    foreach ($variables['items'] as $index => $item) {
      $classStatus = 'published'; // Add logic here for class status;
      $variables['items'][$index]['attributes']->addClass("node_status_" . $classStatus);
    }
  }
}

// On ds-field-expert.html.twig
{% macro content(items, settings, field_item_wrapper_attributes, content_attributes) %}
  {%- for item in items -%}
    {%- if settings.fi -%}
      {%- if settings['fi-def-at'] -%}
        {# This line is problematic as the field_item_wrapper_attributes accumulates at each iteration #}
        {# In my use case, some rows will both contain node_status_published and node_status_unpublished #}
        <{{ settings['fi-el'] }} {{- field_item_wrapper_attributes.mergeAttributes(item.attributes) }}>
      {%- else -%}
        <{{ settings['fi-el'] }} {{- field_item_wrapper_attributes }}>
      {%- endif %}
      {{- item.content -}}
      </{{ settings['fi-el'] }}>
    {% else %}
      {{ item.content }}
    {% endif %}
  {% endfor %}
{% endmacro %} 
🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇵🇭Philippines bryanmanalo

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

Comments & Activities

Production build 0.71.5 2024