Commit cbd36ff0 authored by alexpott's avatar alexpott

Issue #2229435 by lauriii, joelpittet, Rade: Clean up the way attributes are...

Issue #2229435 by lauriii, joelpittet, Rade: Clean up the way attributes are printed in field.html.twig.
parent baa66d85
......@@ -2027,19 +2027,6 @@ function template_preprocess_field(&$variables, $hook) {
// readers.
$variables['label'] = String::checkPlain($element['#title']);
// We want other preprocess functions and the theme implementation to have
// fast access to the field item render arrays. The item render array keys
// (deltas) should always be numerically indexed starting from 0, and looping
// on those keys is faster than calling
// \Drupal\Core\Render\Element::children() or looping on all keys within
// $element, since that requires traversal of all element properties.
$variables['items'] = array();
$delta = 0;
while (!empty($element[$delta])) {
$variables['items'][$delta] = $element[$delta];
$delta++;
}
// Add default CSS classes. Since there can be many fields rendered on a page,
// save some overhead by calling strtr() directly instead of
// drupal_html_class().
......@@ -2053,6 +2040,7 @@ function template_preprocess_field(&$variables, $hook) {
'field-type-' . $variables['field_type_css'],
'field-label-' . $element['#label_display'],
);
// Add a "clearfix" class to the wrapper since we float the label and the
// field items in field.module.css if the label is inline.
if ($element['#label_display'] == 'inline') {
......@@ -2069,15 +2057,26 @@ function template_preprocess_field(&$variables, $hook) {
$default_attributes = new Attribute;
}
// Modules (e.g., rdf.module) can add field item attributes (to
// $item->_attributes) within hook_entity_prepare_view(). Some field
// formatters move those attributes into some nested formatter-specific
// element in order have them rendered on the desired HTML element (e.g., on
// the <a> element of a field item being rendered as a link). Other field
// formatters leave them within $element['#items'][$delta]['_attributes'] to
// be rendered on the item wrappers provided by field.html.twig.
foreach ($variables['items'] as $delta => $item) {
$variables['item_attributes'][$delta] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : clone($default_attributes);
// We want other preprocess functions and the theme implementation to have
// fast access to the field item render arrays. The item render array keys
// (deltas) should always be numerically indexed starting from 0, and looping
// on those keys is faster than calling element_children() or looping on all
// keys within $element, since that requires traversal of all element
// properties.
$variables['items'] = array();
$delta = 0;
while (!empty($element[$delta])) {
$variables['items'][$delta]['content'] = $element[$delta];
// Modules (e.g., rdf.module) can add field item attributes (to
// $item->_attributes) within hook_entity_prepare_view(). Some field
// formatters move those attributes into some nested formatter-specific
// element in order have them rendered on the desired HTML element (e.g., on
// the <a> element of a field item being rendered as a link). Other field
// formatters leave them within $element['#items'][$delta]['_attributes'] to
// be rendered on the item wrappers provided by field.html.twig.
$variables['items'][$delta]['attributes'] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : clone($default_attributes);
$delta++;
}
}
......
......@@ -841,7 +841,7 @@ function comment_preprocess_field(&$variables) {
$variables['title_attributes']['class'][] = 'title';
// Append additional attributes (eg. RDFa) from the first field item.
$variables['attributes'] += $variables['item_attributes'][0]->storage();
$variables['attributes'] += $variables['items'][0]['attributes']->storage();
// Create separate variables for the comments and comment form.
$variables['comments'] = $element[0]['comments'];
......
......@@ -8,11 +8,17 @@
*
* Available variables:
* - attributes: HTML attributes for the containing span element.
* - items: List of all the field items.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item content.
*
* @see field.html.twig
*
* @ingroup themeable
*/
#}
<span{{ attributes }}>{{ items }}</span>
<span{{ attributes }}>
{%- for item in items -%}
{{ item.content }}
{%- endfor -%}
</span>
......@@ -8,11 +8,17 @@
*
* Available variables:
* - attributes: HTML attributes for the containing span element.
* - items: List of all the field items.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item content.
*
* @see field.html.twig
*
* @ingroup themeable
*/
#}
<span{{ attributes }}>{{ items }}</span>
<span{{ attributes }}>
{%- for item in items -%}
{{ item.content }}
{%- endfor -%}
</span>
......@@ -8,11 +8,17 @@
*
* Available variables:
* - attributes: HTML attributes for the containing span element.
* - items: List of all the field items.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item content.
*
* @see field.html.twig
*
* @ingroup themeable
*/
#}
<span{{ attributes }}>{{ items }}</span>
<span{{ attributes }}>
{%- for item in items -%}
{{ item.content }}
{%- endfor -%}
</span>
......@@ -21,8 +21,9 @@
* - title_attributes: HTML attributes for the title.
* - label: The label for the field.
* - content_attributes: HTML attributes for the content.
* - items: List of all the field items.
* - item_attributes: List of HTML attributes for each item.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item's content.
*
* @see template_preprocess_field()
*
......@@ -31,11 +32,11 @@
#}
<div{{ attributes }}>
{% if not label_hidden %}
<div class="field-label{% if title_attributes.class %} {{ title_attributes.class }}{% endif %}"{{ title_attributes|without('class') }}>{{ label }}:&nbsp;</div>
<div{{ title_attributes.addClass('field-label') }}>{{ label }}:&nbsp;</div>
{% endif %}
<div class="field-items"{{ content_attributes }}>
{% for delta, item in items %}
<div class="field-item"{{ item_attributes[delta] }}>{{ item }}</div>
<div{{ content_attributes.addClass('field-items') }}>
{% for item in items %}
<div{{ item.attributes.addClass('field-item') }}>{{ item.content }}</div>
{% endfor %}
</div>
</div>
......@@ -9,20 +9,21 @@
* - title_attributes: HTML attributes for the label.
* - label: The label for the field.
* - content_attributes: HTML attributes for the content.
* - items: List of all the field items.
* - item_attributes: List of HTML attributes for each item.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item's content.
*
* @see template_preprocess_field()
* @see bartik_preprocess_field()
*/
#}
<div class="{{ attributes.class }} clearfix"{{ attributes|without('class') }}>
<div{{ attributes.addClass('clearfix') }}>
{% if not label_hidden %}
<h3{{ title_attributes }}>{{ label }}: </h3>
{% endif %}
<ul class="links field-items {{ content_attributes.class }}"{{ content_attributes|without('class') }}>
{% for delta, item in items %}
<li class="taxonomy-term-reference-{{ delta }}"{{ item_attributes[delta] }}>{{ item }}</li>
<ul{{ content_attributes.addClass('links', 'field-items') }}>
{% for item in items %}
<li{{ item.attributes }}>{{ item.content }}</li>
{% endfor %}
</ul>
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment