Commit 13242b5a authored by alexpott's avatar alexpott

Issue #2201693 by plopesc, e0ipso, swentel: Field output supporting code...

Issue #2201693 by plopesc, e0ipso, swentel: Field output supporting code should move out of field.module.
parent 9dc59dda
This diff is collapsed.
/* Field display */
.field .field-label {
font-weight: bold;
}
.field-label-inline .field-label,
.field-label-inline .field-items {
float:left; /*LTR*/
padding-right: 0.5em;
}
[dir="rtl"] .field-label-inline .field-label,
[dir="rtl"] .field-label-inline .field-items {
float: right;
padding-left: 0.5em;
}
/* Form display */
form .field-edit-link {
margin: 0 0.3em;
}
form .field-multiple-table {
margin: 0;
}
form .field-multiple-table .field-multiple-drag {
width: 30px;
padding-right: 0; /*LTR*/
}
[dir="rtl"] form .field-multiple-table .field-multiple-drag {
padding-left: 0;
}
form .field-multiple-table .field-multiple-drag .tabledrag-handle {
padding-right: .5em; /*LTR*/
}
[dir="rtl"] form .field-multiple-table .field-multiple-drag .tabledrag-handle {
padding-left: .5em;
}
form .field-add-more-submit {
margin: .5em 0 0;
}
......@@ -6,109 +6,6 @@
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Render\Element;
/**
* Prepares variables for individual form element templates.
*
* Default template: field-multiple-value-form.html.twig.
*
* Combines multiple values into a table with drag-n-drop reordering.
*
* @param array $variables
* An associative array containing:
* - element: A render element representing the form element.
*/
function template_preprocess_field_multiple_value_form(&$variables) {
$element = $variables['element'];
$variables['multiple'] = $element['#cardinality_multiple'];
if ($variables['multiple']) {
$table_id = drupal_html_id($element['#field_name'] . '_values');
$order_class = $element['#field_name'] . '-delta-order';
$header = array(
array(
'data' => array(
'#prefix' => '<h4 class="label">',
'title' => array(
'#markup' => t($element['#title']),
),
'#suffix' => '</h4>',
),
'colspan' => 2,
'class' => array('field-label'),
),
t('Order', array(), array('context' => 'Sort order')),
);
if (!empty($element['#required'])) {
$header[0]['data']['required'] = array(
'#theme' => 'form_required_marker',
'#element' => $element,
);
}
$rows = array();
// Sort items according to '_weight' (needed when the form comes back after
// preview or failed validation).
$items = array();
$variables['button'] = array();
foreach (Element::children($element) as $key) {
if ($key === 'add_more') {
$variables['button'] = &$element[$key];
}
else {
$items[] = &$element[$key];
}
}
usort($items, '_field_sort_items_value_helper');
// Add the items as table rows.
foreach ($items as $item) {
$item['_weight']['#attributes']['class'] = array($order_class);
// Remove weight form element from item render array so it can be rendered
// in a separate table column.
$delta_element = $item['_weight'];
unset($item['_weight']);
$cells = array(
array('data' => '', 'class' => array('field-multiple-drag')),
array('data' => $item),
array('data' => $delta_element, 'class' => array('delta-order')),
);
$rows[] = array(
'data' => $cells,
'class' => array('draggable'),
);
}
$variables['table'] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array(
'id' => $table_id,
'class' => array('field-multiple-table'),
),
'#tabledrag' => array(
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => $order_class,
),
),
);
$variables['description'] = $element['#description'];
}
else {
$variables['elements'] = array();
foreach (Element::children($element) as $key) {
$variables['elements'][] = $element[$key];
}
}
}
/**
* Callback for usort() within theme_field_multiple_value_form().
......
......@@ -5,13 +5,9 @@
*/
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Template\Attribute;
use Drupal\entity\Entity\EntityViewDisplay;
use Drupal\field\Field;
/*
......@@ -130,23 +126,6 @@ function field_help($path, $arg) {
}
}
/**
* Implements hook_theme().
*/
function field_theme() {
return array(
'field' => array(
'render element' => 'element',
'template' => 'field',
),
'field_multiple_value_form' => array(
'file' => 'field.form.inc',
'render element' => 'element',
'template' => 'field-multiple-value-form',
),
);
}
/**
* Implements hook_cron().
*/
......@@ -343,87 +322,6 @@ function field_theme_suggestions_field(array $variables) {
return $suggestions;
}
/**
* Prepares variables for field templates.
*
* Default template: field.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: A render element representing the field.
* - attributes: A string containing the attributes for the wrapping div.
* - title_attributes: A string containing the attributes for the title.
* - content_attributes: A string containing the attributes for the content's
* div.
*/
function template_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
$variables['label_hidden'] = ($element['#label_display'] == 'hidden');
// Always set the field label - allow themes to decide whether to display it.
// In addition the label should be rendered but hidden to support screen
// 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 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().
$variables['entity_type_css'] = strtr($element['#entity_type'], '_', '-');
$variables['field_name_css'] = strtr($element['#field_name'], '_', '-');
$variables['field_type_css'] = strtr($element['#field_type'], '_', '-');
$variables['attributes']['class'] = array(
'field',
'field-' . $variables['entity_type_css'] . '--' . $variables['field_name_css'],
'field-name-' . $variables['field_name_css'],
'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') {
$variables['attributes']['class'][] = 'clearfix';
}
static $default_attributes;
if (!isset($default_attributes)) {
$default_attributes = new Attribute;
}
// The default theme implementation for fields is a function.
// template_preprocess() (which initializes the attributes, title_attributes,
// and content_attributes arrays) does not run for theme function
// implementations. Additionally, Attribute objects for the three variables
// below only get instantiated for template file implementations, and we need
// Attribute objects for printing in both theme functions and template files.
// For best performance, we only instantiate Attribute objects when needed.
$variables['attributes'] = isset($variables['attributes']) ? new Attribute($variables['attributes']) : clone $default_attributes;
$variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone($default_attributes);
$variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone($default_attributes);
// 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);
}
}
/**
* @} End of "defgroup field".
*/
......
......@@ -611,3 +611,42 @@ table tr.error {
.messages--error p.error {
color: #a51b00;
}
/* Field display */
.field .field-label {
font-weight: bold;
}
.field-label-inline .field-label,
.field-label-inline .field-items {
float:left; /*LTR*/
padding-right: 0.5em;
}
[dir="rtl"] .field-label-inline .field-label,
[dir="rtl"] .field-label-inline .field-items {
float: right;
padding-left: 0.5em;
}
/* Form display */
form .field-edit-link {
margin: 0 0.3em;
}
form .field-multiple-table {
margin: 0;
}
form .field-multiple-table .field-multiple-drag {
width: 30px;
padding-right: 0; /*LTR*/
}
[dir="rtl"] form .field-multiple-table .field-multiple-drag {
padding-left: 0;
}
form .field-multiple-table .field-multiple-drag .tabledrag-handle {
padding-right: .5em; /*LTR*/
}
[dir="rtl"] form .field-multiple-table .field-multiple-drag .tabledrag-handle {
padding-left: .5em;
}
form .field-add-more-submit {
margin: .5em 0 0;
}
......@@ -900,6 +900,22 @@ function system_theme_suggestions_region(array $variables) {
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function system_theme_suggestions_field(array $variables) {
$suggestions = array();
$element = $variables['element'];
$suggestions[] = 'field__' . $element['#field_type'];
$suggestions[] = 'field__' . $element['#field_name'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#bundle'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'];
return $suggestions;
}
/**
* Implements hook_stream_wrappers().
*/
......
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