From 00ad7e8e032f94c85874b2ee168db8864248cce2 Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Thu, 13 Aug 2009 01:50:00 +0000
Subject: [PATCH] #529756 by yched: Allow fields to be weighted per context.

---
 modules/field/field.crud.inc    | 25 +++++++++++++------------
 modules/field/field.default.inc |  2 +-
 modules/field/field.form.inc    |  3 +--
 modules/field/field.install     |  5 -----
 modules/field/field.test        |  6 ++++--
 5 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/modules/field/field.crud.inc b/modules/field/field.crud.inc
index dc3da56fcef7..08afcb695dd7 100644
--- a/modules/field/field.crud.inc
+++ b/modules/field/field.crud.inc
@@ -97,11 +97,6 @@
  *     A human-readable description for the field when used with this
  *     bundle. For example, the description will be the help text of
  *     Form API elements for this instance.
- * - weight (float)
- *     The order in which the field should be sorted relative
- *     to other fields when used with this bundle. The weight affects
- *     ordering in both forms (see field_attach_form()) and rendered output
- *     (see field_attach_view()).
  * - required (integer)
  *     TRUE if a value for this field is required when used with this
  *     bundle, FALSE otherwise. Currently, required-ness is only enforced
@@ -124,15 +119,18 @@
  *     - type (string)
  *         The type of the widget, such as text_textfield. Widget types
  *         are defined by modules that implement hook_field_widget_info().
+ *     - settings (array)
+ *         A sub-array of key/value pairs of widget-type-specific settings.
+ *         Each field widget type module defines and documents its own
+ *         widget settings.
+ *     - weight (float)
+ *         The weight of the widget relative to the other elements in object
+ *         edit forms.
  *     - module (string, read-only)
  *         The name of the module that implements the widget type.
  *     - active (integer, read-only)
  *         TRUE if the module that implements the widget type is currently
  *         enabled, FALSE otherwise.
- *     - settings (array)
- *         A sub-array of key/value pairs of widget-type-specific settings.
- *         Each field widget type module defines and documents its own
- *         widget settings.
  * - display (array)
  *     A sub-array of key/value pairs identifying build modes and the way the
  *     field values should be displayed in each build mode.
@@ -147,6 +145,9 @@
  *         - settings (array)
  *             A sub-array of key/value pairs of display options specific to
  *             the formatter.
+ *        - weight (float)
+ *            The weight of the field relative to the other object components
+ *            displayed in this build mode.
  *         - module (string, read-only)
  *             The name of the module which implements the display formatter.
  *     - teaser
@@ -530,6 +531,7 @@ function _field_write_instance($instance, $update = FALSE) {
     // TODO: what if no 'default_widget' specified ?
     'type' => $field_type['default_widget'],
     'settings' => array(),
+    'weight' => 0,
   );
   // Check widget module.
   $widget_type = field_info_widget_types($instance['widget']['type']);
@@ -550,6 +552,7 @@ function _field_write_instance($instance, $update = FALSE) {
       // TODO: what if no 'default_formatter' specified ?
       'type' => $field_type['default_formatter'],
       'settings' => array(),
+      'weight' => 0,
     );
     $formatter_type = field_info_formatter_types($instance['display'][$build_mode]['type']);
     // TODO : 'hidden' will raise PHP warnings.
@@ -561,7 +564,7 @@ function _field_write_instance($instance, $update = FALSE) {
   // not have its own column and is not automatically populated when the
   // instance is read.
   $data = $instance;
-  unset($data['id'], $data['field_id'], $data['field_name'], $data['bundle'], $data['widget']['type'], $data['weight'], $data['deleted']);
+  unset($data['id'], $data['field_id'], $data['field_name'], $data['bundle'], $data['widget']['type'], $data['deleted']);
 
   $record = array(
     'field_id' => $instance['field_id'],
@@ -570,7 +573,6 @@ function _field_write_instance($instance, $update = FALSE) {
     'widget_type' => $instance['widget']['type'],
     'widget_module' => $widget_module,
     'widget_active' => $widget_active,
-    'weight' => $instance['weight'],
     'data' => $data,
     'deleted' => $instance['deleted'],
   );
@@ -653,7 +655,6 @@ function field_read_instances($params = array(), $include_additional = array())
     $instance['field_id'] = $record['field_id'];
     $instance['field_name'] = $record['field_name'];
     $instance['bundle'] = $record['bundle'];
-    $instance['weight'] = $record['weight'];
     $instance['deleted'] = $record['deleted'];
     $instance['widget']['type'] = $record['widget_type'];
     $instance['widget']['module'] = $record['widget_module'];
diff --git a/modules/field/field.default.inc b/modules/field/field.default.inc
index 71906c6259cb..927571e84c2a 100644
--- a/modules/field/field.default.inc
+++ b/modules/field/field.default.inc
@@ -79,7 +79,7 @@ function field_default_view($obj_type, $object, $field, $instance, $items, $buil
 
     $element = $info + array(
       '#theme' => 'field',
-      '#weight' => $instance['weight'],
+      '#weight' => $display['weight'],
       '#title' => check_plain(t($instance['label'])),
       '#access' => field_access('view', $field),
       '#label_display' => $label_display,
diff --git a/modules/field/field.form.inc b/modules/field/field.form.inc
index 7b4e88536d25..20998c349ef6 100644
--- a/modules/field/field.form.inc
+++ b/modules/field/field.form.inc
@@ -90,7 +90,7 @@ function field_default_form($obj_type, $object, $field, $instance, $items, &$for
     $defaults = array(
       '#field_name' => $field['field_name'],
       '#tree' => TRUE,
-      '#weight' => $instance['weight'],
+      '#weight' => $instance['widget']['weight'],
     );
 
     $addition[$field['field_name']] = array_merge($form_element, $defaults);
@@ -188,7 +188,6 @@ function field_multiple_value_form($field, $instance, $items, &$form, &$form_sta
         '#type' => 'submit',
         '#name' => $field_name . '_add_more',
         '#value' => t('Add another item'),
-        '#weight' => $instance['weight'] + $max + 1,
         // Submit callback for disabled JavaScript.
         '#submit' => array('field_add_more_submit'),
         '#ahah' => array(
diff --git a/modules/field/field.install b/modules/field/field.install
index d19c8f1653b0..2d3b3750eccb 100644
--- a/modules/field/field.install
+++ b/modules/field/field.install
@@ -116,11 +116,6 @@ function field_schema() {
         'not null' => TRUE,
         'serialize' => TRUE,
       ),
-      'weight' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'default' => 0,
-      ),
       'deleted' => array(
         'type' => 'int',
         'size' => 'tiny',
diff --git a/modules/field/field.test b/modules/field/field.test
index e2a4674d852c..fce0fd8c273a 100644
--- a/modules/field/field.test
+++ b/modules/field/field.test
@@ -1709,21 +1709,23 @@ class FieldInstanceCrudTestCase extends DrupalWebTestCase {
     // Check that basic changes are saved.
     $instance = field_read_instance($this->instance_definition['field_name'], $this->instance_definition['bundle']);
     $instance['required'] = !$instance['required'];
-    $instance['weight']++;
     $instance['label'] = $this->randomName();
     $instance['description'] = $this->randomName();
     $instance['settings']['test_instance_setting'] = $this->randomName();
     $instance['widget']['settings']['test_widget_setting'] =$this->randomName();
+    $instance['widget']['weight']++;
     $instance['display']['full']['settings']['test_formatter_setting'] = $this->randomName();
+    $instance['display']['full']['weight']++;
     field_update_instance($instance);
 
     $instance_new = field_read_instance($this->instance_definition['field_name'], $this->instance_definition['bundle']);
     $this->assertEqual($instance['required'], $instance_new['required'], t('"required" change is saved'));
-    $this->assertEqual($instance['weight'], $instance_new['weight'], t('"weight" change is saved'));
     $this->assertEqual($instance['label'], $instance_new['label'], t('"label" change is saved'));
     $this->assertEqual($instance['description'], $instance_new['description'], t('"description" change is saved'));
     $this->assertEqual($instance['widget']['settings']['test_widget_setting'], $instance_new['widget']['settings']['test_widget_setting'], t('Widget setting change is saved'));
+    $this->assertEqual($instance['widget']['weight'], $instance_new['widget']['weight'], t('Widget weight change is saved'));
     $this->assertEqual($instance['display']['full']['settings']['test_formatter_setting'], $instance_new['display']['full']['settings']['test_formatter_setting'], t('Formatter setting change is saved'));
+    $this->assertEqual($instance['display']['full']['weight'], $instance_new['display']['full']['weight'], t('Widget weight change is saved'));
 
     // Check that changing widget and formatter types updates the default settings.
     $instance = field_read_instance($this->instance_definition['field_name'], $this->instance_definition['bundle']);
-- 
GitLab