diff --git a/core/includes/theme.inc b/core/includes/theme.inc index b3b3c16..3937b9c 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1509,34 +1509,29 @@ function template_preprocess_field(&$variables, $hook) { // readers. $variables['label'] = SafeMarkup::checkPlain($element['#title']); + // Check if there are multiple field items. + $variables['multiple'] = FALSE; + if (isset($element['#items']) && is_callable($element['#items'], 'getFieldDefition') && is_callable($element['#items']->getFieldDefinition(), 'isMultiple')) { + $variables['multiple'] = $element['items']->getFieldDefinition()->isMultiple(); + } + static $default_attributes; if (!isset($default_attributes)) { $default_attributes = new Attribute; } - // Helper variables for the field template to make it easier to read. - // @todo this needs better documentation ;) - // We want to know how we want to wrap a field and when to merge in css - // classes, so a field dont have any divitis and is wrapped in the smallest - // amount possible. - // If theres multiple fields, and no label, then the outter wrapper should - // have a class attached to define that theres multiple items (.field__items) - // if theres multiples and the label isnt hidden and content_attributes have - // been set ... - // Do the field contains multiple items. - if ( count($element['#items']) > 1) { - $variables['multiple_fields'] = TRUE; - } - if ( count($element['#items']) > 1 and $element['#label_display'] != 'hidden' ) { - $variables['multiple_items_wrapper'] = TRUE; + // Merge attributes when a multiple-value field has a hidden label. + if ($element['#label_display'] == 'hidden' && $variables['multiple']) { + $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables['content_attributes']); } - // Check if theres multiple fields, the label is hidden and content_attributes is empty. - if ( count($element['#items']) > 1 and $element['#label_display'] == 'hidden' and empty($variables['content_attributes']) ) { - $variables['multiple_hiddenlabel_no_content_attributes'] = TRUE; + // Merge attributes when a single-value field has a hidden label. + if ($element['#label_display'] == 'hidden' && !$variables['multiple'] && is_object($element['#items'][0])) { + $variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], (array) $element['#items'][0]->_attributes); + $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables['content_attributes']); } - // Check if theres multiple fields or a visible label or content_attributes is not empty. - if ( count($element['#items']) > 1 or $element['#label_display'] != 'hidden' or !empty($variables['content_attributes']) ) { - $variables['multiple_visiblelabel_content_attributes'] = TRUE; + // Merge attributes when a single-value field has a visible label. + if ($element['#label_display'] != 'hidden' && !$variables['multiple'] && is_object($element['#items'][0])) { + $element['#items'][0]->_attributes = NestedArray::mergeDeep($variables['content_attributes'], (array) $element['#items'][0]->_attributes); } // We want other preprocess functions and the theme implementation to have diff --git a/core/modules/system/templates/field.html.twig b/core/modules/system/templates/field.html.twig index 9a1fbf4..10f21e0 100644 --- a/core/modules/system/templates/field.html.twig +++ b/core/modules/system/templates/field.html.twig @@ -25,7 +25,7 @@ * - title_attributes: HTML attributes for the title. * - label: The label for the field. * - content_attributes: HTML attributes for the content. - * - multiple_fields: Whether theres is multiple field items. + * - 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. @@ -39,34 +39,29 @@ */ #} -{% if outerwrapper_field_items %} - -{% elseif outerwrapper %} - -{% endif %} - - {% if not label_hidden %} - {{ label }}: +{% if label_hidden %} + {% if multiple %} + + {% for item in items %} + {{ item.content }} + {% endfor %} + + {% else %} + {% for item in items %} + {{ item.content }} + {% endfor %} {% endif %} - - {% if multiple_items_wrapper %} +{% else %} + + {{ label }}: + {% if multiple %} - {% endif %} - - {# Item #} - {% for item in items %} - {% if (multiple_fields or not label_hidden or item.attributes.storage is not empty ) %} - {{ item.content }} - {% else %} - {{ item.content }} - {% endif %} - {% endfor %} - {# /Item #} - - {% if multiple_items_wrapper %} + {% endif %} + {% for item in items %} + {{ item.content }} + {% endfor %} + {% if multiple %} - {% endif %} - -{% if (outerwrapper_field_items or outerwrapper) %} - + {% endif %} + {% endif %} diff --git a/core/themes/classy/templates/field/field.html.twig b/core/themes/classy/templates/field/field.html.twig index 9507de9..953546f 100644 --- a/core/themes/classy/templates/field/field.html.twig +++ b/core/themes/classy/templates/field/field.html.twig @@ -25,7 +25,7 @@ * - title_attributes: HTML attributes for the title. * - label: The label for the field. * - content_attributes: HTML attributes for the content. - * - multiple_fields: Whether theres is multiple field items. + * - 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. @@ -52,41 +52,30 @@ label_display == 'visually_hidden' ? 'visually-hidden', ] %} -{# Test which outer wrapper is needed, depending on the field content. #} -{# {% if (multiple_fields and label_hidden and content_attributes.storage is empty) %} #} - {# {% set outerwrapper_field_items = true %} #} -{# {% elseif (multiple_fields or not label_hidden or content_attributes.storage is not empty) %} #} - {# {% set outerwrapper = true %} #} -{# {% endif %} #} -{% if multiple_hiddenlabel_no_content_attributes %} - -{% elseif multiple_visiblelabel_content_attributes %} - -{% endif %} - - {% if not label_hidden %} - {{ label }}: - {% endif %} - - {% if multiple_items_wrapper %} - +{% if label_hidden %} + {% if multiple %} + + {% for item in items %} + {{ item.content }} + {% endfor %} + + {% else %} + {% for item in items %} + {{ item.content }} + {% endfor %} {% endif %} - - {# Item #} - {% for item in items %} - {% if (multiple_fields or not label_hidden or item.attributes.storage is not empty ) %} +{% else %} + + {{ label }}: + {% if multiple %} + + {% endif %} + {% for item in items %} {{ item.content }} - {% else %} - {{ item.content }} + {% endfor %} + {% if multiple %} + {% endif %} - {% endfor %} - {# /Item #} - - {% if multiple_items_wrapper %} - - {% endif %} - -{% if (multiple_hiddenlabel_no_content_attributes or multiple_visiblelabel_content_attributes) %} - + {% endif %}