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 %} -