- Patch #545662 by yched, moshe weitzman: simplify field rendering.

merge-requests/26/head
Dries Buytaert 2009-08-28 06:51:07 +00:00
parent e87bf71e5f
commit 45e14ea6b7
6 changed files with 65 additions and 98 deletions

View File

@ -490,6 +490,7 @@ function field_attach_form($obj_type, $object, &$form, &$form_state, $langcode =
// Add custom weight handling.
list($id, $vid, $bundle) = field_attach_extract_ids($obj_type, $object);
$form['#attached_css'][] = drupal_get_path('module', 'field') . '/theme/field.css';
$form['#pre_render'][] = '_field_extra_weights_pre_render';
$form['#extra_fields'] = field_extra_fields($bundle);
@ -1040,10 +1041,11 @@ function field_attach_query_revisions($field_id, $conditions, $count, &$cursor =
* '#label_display' => the label display mode,
* '#object' => the fieldable object being displayed,
* '#object_type' => the type of the object being displayed,
* '#language' => the language of the field values being displayed,
* '#build_mode' => the build mode,
* '#field_name' => the name of the field,
* '#single' => boolean indicating whether the formatter is single or
* multiple,
* '#formatter_single' => boolean indicating whether the formatter is single or
* multiple,
* 'items' => array(
* // One sub-array per field value, keyed by delta.
* 0 => array(
@ -1088,6 +1090,7 @@ function field_attach_view($obj_type, $object, $build_mode = 'full', $langcode =
// Add custom weight handling.
list($id, $vid, $bundle) = field_attach_extract_ids($obj_type, $object);
$output['#attached_css'][] = drupal_get_path('module', 'field') . '/theme/field.css';
$output['#pre_render'][] = '_field_extra_weights_pre_render';
$output['#extra_fields'] = field_extra_fields($bundle);

View File

@ -47,7 +47,7 @@ function field_default_insert($obj_type, $object, $field, $instance, $langcode,
// assigning it a default value. This way we ensure that only the intended
// languages get a default value. Otherwise we could have default values for
// not yet open languages.
if (empty($object) || !property_exists($object, $field['field_name']) ||
if (empty($object) || !property_exists($object, $field['field_name']) ||
(isset($object->{$field['field_name']}[$langcode]) && count($object->{$field['field_name']}[$langcode]) == 0)) {
$items = field_get_default_value($obj_type, $object, $field, $instance, $langcode);
}
@ -87,8 +87,8 @@ function field_default_view($obj_type, $object, $field, $instance, $langcode, $i
'#access' => field_access('view', $field),
'#label_display' => $label_display,
'#build_mode' => $build_mode,
'#single' => $single,
'#language' => $langcode,
'#formatter_single' => $single,
'items' => array(),
);
@ -103,9 +103,9 @@ function field_default_view($obj_type, $object, $field, $instance, $langcode, $i
// Append formatter information either on each item ('single-value' formatter)
// or at the upper 'items' level ('multiple-value' formatter)
$format_info = $info + array(
'#theme' => $theme,
'#formatter' => $display['type'],
'#settings' => $display['settings'],
'#theme' => $theme,
);
if ($single) {

View File

@ -148,13 +148,6 @@ function field_help($path, $arg) {
}
}
/**
* Implement hook_init().
*/
function field_init() {
drupal_add_css(drupal_get_path('module', 'field') . '/theme/field.css');
}
/**
* Implement hook_theme().
*/
@ -634,13 +627,6 @@ function field_access($op, $field, $account = NULL) {
/**
* Theme preprocess function for field.tpl.php.
*
* The $variables array contains the following arguments:
* - $object
* - $field
* - $items
* - $teaser
* - $page
*
* @see field.tpl.php
*/
function template_preprocess_field(&$variables) {
@ -649,54 +635,33 @@ function template_preprocess_field(&$variables) {
$instance = field_info_instance($element['#field_name'], $bundle);
$field = field_info_field($element['#field_name']);
$variables['object'] = $element['#object'];
$variables['field'] = $field;
$variables['instance'] = $instance;
$variables['items'] = array();
$field_type_css = strtr($field['type'], '_', '-');
$field_name_css = strtr($field['field_name'], '_', '-');
if ($element['#single']) {
// Single value formatter.
foreach (element_children($element['items']) as $delta) {
$variables['items'][$delta] = $element['items'][$delta]['#item'];
$variables['items'][$delta]['view'] = drupal_render_children($element['items'], array($delta));
}
}
else {
// Multiple values formatter.
// We display the 'all items' output as $items[0], as if it was the
// output of a single valued field.
// Raw values are still exposed for all items.
foreach (element_children($element['items']) as $delta) {
$variables['items'][$delta] = $element['items'][$delta]['#item'];
}
$variables['items'][0]['view'] = drupal_render_children($element, array('items'));
}
$variables['build_mode'] = $element['#build_mode'];
$variables['page'] = (bool)menu_get_object();
$field_empty = TRUE;
foreach ($variables['items'] as $delta => $item) {
if (!isset($item['view']) || (empty($item['view']) && (string)$item['view'] !== '0')) {
$variables['items'][$delta]['empty'] = TRUE;
}
else {
$field_empty = FALSE;
$variables['items'][$delta]['empty'] = FALSE;
}
}
// If the formatter is multiple, the template sees only one 'item', which
// will include all values.
$items = $element['#formatter_single'] ? $element['items'] : array($element['items']);
$additions = array(
'object' => $element['#object'],
'field' => $field,
'instance' => $instance,
'build_mode' => $element['#build_mode'],
'items' => $items,
'field_type' => $field['type'],
'field_name' => $field['field_name'],
'field_type_css' => strtr($field['type'], '_', '-'),
'field_name_css' => strtr($field['field_name'], '_', '-'),
'label' => check_plain(t($instance['label'])),
'field_type_css' => $field_type_css,
'field_name_css' => $field_name_css,
'label' => $element['#title'],
'label_display' => $element['#label_display'],
'field_empty' => $field_empty,
'label_hidden' => $element['#label_display'] == 'hidden',
'field_language' => $element['#language'],
'field_translatable' => $field['translatable'],
'classes_array' => array(
'field-name-' . $field_name_css,
'field-type-' . $field_type_css,
'field-label-' . $element['#label_display'],
),
'template_files' => array(
'field',
'field-' . $element['#field_name'],

View File

@ -1,19 +1,15 @@
/* $Id$ */
/* Field display */
.field .field-label,
.field .field-label-inline,
.field .field-label-inline-first {
.field .field-label {
font-weight: bold;
}
.field .field-label-inline,
.field .field-label-inline-first {
display: inline;
}
.field .field-label-inline {
visibility: hidden;
.field-label-inline .field-label,
.field-label-inline .field-items {
float:left;
}
/* Form display */
form .field-multiple-table {
margin: 0;
}

View File

@ -2,50 +2,51 @@
// $Id$
/**
* @file field-field.tpl.php
* @file field.tpl.php
* Default theme implementation to display the value of a field.
*
* Available variables:
* - $items: An array of field values. Use render() to output them.
* - $label: The item label.
* - $label_hidden: Whether the label display is set to 'hidden'.
* - $classes: String of classes that can be used to style contextually through
* CSS. It can be manipulated through the variable $classes_array from
* preprocess functions. The default values can be one or more of the
* following:
* - field-name-[field_name]: The current field name. For example, if the
* field name is "field_description" it would result in
* "field-name-field-description".
* - field-type-[field_type]: The current field type. For example, if the
* field type is "text" it would result in "field-type-text".
* - field-label-[label_display]: The current label position. For example, if the
* label position is "above" it would result in "field-label-above".
*
* Other variables:
* - $object: The object to which the field is attached.
* - $field: The field array.
* - $items: An array of values for each item in the field array.
* - $build_mode: Build mode, e.g. 'full', 'teaser'...
* - $page: Whether this is displayed as a page.
* - $field_name: The field name.
* - $field_type: The field type.
* - $field_name_css: The css-compatible field name.
* - $field_type_css: The css-compatible field type.
* - $label: The item label.
* - $label_display: Position of label display, inline, above, or hidden.
* - $field_empty: Whether the field has any valid value.
* - $field_language: The field language.
* - $field_translatable: Whether the field is translatable or not.
*
* Each $item in $items contains:
* - 'view' - the themed view for that item
* - $label_display: Position of label display, inline, above, or hidden.
* - $classes_array: Array of html class attribute values. It is flattened
* into a string within the variable $classes.
*
* @see template_preprocess_field()
*/
?>
<?php if (!$field_empty) : ?>
<div class="field field-type-<?php print $field_type_css ?> field-<?php print $field_name_css ?>">
<?php if ($label_display == 'above') : ?>
<div class="field-label"><?php print t($label) ?>:&nbsp;</div>
<?php endif;?>
<div class="field-items">
<?php $count = 1;
foreach ($items as $delta => $item) :
if (!$item['empty']) : ?>
<div class="field-item <?php print ($count % 2 ? 'odd' : 'even') ?>">
<?php if ($label_display == 'inline') { ?>
<div class="field-label-inline<?php print($delta ? '' : '-first')?>">
<?php print t($label) ?>:&nbsp;</div>
<?php } ?>
<?php print $item['view'] ?>
</div>
<?php $count++;
endif;
endforeach;?>
<?php if ($items) : ?>
<div class="field <?php print $classes; ?> clearfix">
<?php if (!$label_hidden) : ?>
<div class="field-label"><?php print $label ?>:&nbsp;</div>
<?php endif; ?>
<div class="field-items">
<?php foreach ($items as $delta => $item) : ?>
<div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"><?php print render($item); ?></div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>

View File

@ -521,6 +521,7 @@ function field_test_field_formatter_info() {
return array(
'field_test_default' => array(
'label' => t('Default'),
'description' => t('Default formatter'),
'field types' => array('test_field'),
'settings' => array(
'test_formatter_setting' => 'dummy test string',
@ -530,7 +531,8 @@ function field_test_field_formatter_info() {
),
),
'field_test_multiple' => array(
'label' => t('Default'),
'label' => t('Multiple'),
'description' => t('Multiple formatter'),
'field types' => array('test_field'),
'settings' => array(
'test_formatter_setting_multiple' => 'dummy test string',