Index: computed_field.info
===================================================================
--- computed_field.info	(revision 7)
+++ computed_field.info	(working copy)
@@ -1,13 +1,14 @@
 ; $Id$
 name = Computed Field
 description = Allows the user to define computed values in custom content types.
-dependencies = content
+core = 6.x
+dependencies[] = content
 package = CCK
 
 ; version added by drupal.org packaging script on 2006-11-27
 
 ; Information added by drupal.org packaging script on 2007-06-19
-version = "HEAD"
+version = "6.x-1.x-dev"
 project = "computed_field"
 datestamp = "1182211607"
 
Index: computed_field.module
===================================================================
--- computed_field.module	(revision 7)
+++ computed_field.module	(working copy)
@@ -5,7 +5,16 @@
  * Implementation of cck hook_field_info
  */
 function computed_field_field_info() {
-  return array('computed' => array('label' => 'Computed'));
+  return array(
+    'computed' => array(
+      'label' => t('Computed'),
+      'description' => t('Create field data via PHP code.'),
+      'callbacks' => array(
+        'tables' => CONTENT_CALLBACK_DEFAULT,
+        'arguments' => CONTENT_CALLBACK_DEFAULT,
+        ),
+      ),
+    );
 }
 
 /**
@@ -20,55 +29,55 @@
         '#type' => 'textarea',
         '#rows' => 15,
         '#title' => t('Computed Code'),
-        '#description' => t('The variables available to your code are: ') . '<code>&amp;$node, $field, and &amp;$node_field</code>' . t('. To set the value of the field, set ') . '<code>$node_field[0][\'value\']</code>' . t('. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields field_a and field_b: ') . '<code>$node_field[0][\'value\'] = $node->field_a[0][\'value\'] + $node->field_b[0][\'value\'];</code>',
-        '#default_value' => isset($field['code']) ? $field['code'] : (isset($field['widget']['code']) ? $field['widget']['code'] : ''),
+        '#description' => t('The variables available to your code are: ') .'<code>&amp;$node, $field, and &amp;$node_field</code>'. t('. To set the value of the field, set ') .'<code>$node_field[0][\'value\']</code>'. t('. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields field_a and field_b: ') .'<code>$node_field[0][\'value\'] = $node->field_a[0][\'value\'] + $node->field_b[0][\'value\'];</code>',
+        '#default_value' => !empty($field['code']) ? $field['code'] : '$node_field[0][\'value\'] = "";',
       );
       $form['display'] = array(
         '#type' => 'checkbox',
         '#title' => t('Display this field'),
-        '#default_value' => isset($field['code']) ? $field['code'] : (isset($field['widget']['display']) ? $field['widget']['display'] : true),
+        '#default_value' => is_numeric($field['code']) ? $field['code'] : TRUE ,
       );
       $form['display_format'] = array(
         '#type' => 'textarea',
         '#title' => t('Display Format'),
-	'#description' => t('This code should assign a string to the $display variable, which will be printed as the value of the field. The stored value of the field is in $node_field_item[\'value\'].'),
-        '#default_value' => isset($field['display_format']) ? $field['display_format'] : (isset($field['widget']['display_format']) ? $field['widget']['display_format'] : '$display = $node_field_item[\'value\'];'),
+        '#description' => t('This code should assign a string to the $display variable, which will be printed as the value of the field. The stored value of the field is in $node_field_item[\'value\'].  Note: this code has no effect if you use the "Raw Value" formatter option.'),
+        '#default_value' => !empty($field['display_format']) ? $field['display_format'] : '$display = $node_field_item[\'value\'];',
       );
       $form['store'] = array(
         '#type' => 'checkbox',
         '#title' => t('Store using the database settings below'),
-        '#default_value' => isset($field['store']) ? $field['store'] : true,
+        '#default_value' => is_numeric($field['store']) ? $field['store'] : 1 ,
       );
       $form['database'] = array('#type' => 'fieldset', '#title' => t('Database Storage Settings'));
       $form['database']['data_type'] = array(
         '#type' => 'radios',
         '#title' => t('Data Type'),
         '#description' => t('The SQL datatype to store this field in.'),
-        '#default_value' => isset($field['data_type']) ? $field['data_type'] : 'varchar',
+        '#default_value' => !empty($field['data_type']) ? $field['data_type'] : 'varchar',
         '#options' => array('int' => 'int', 'float' => 'float', 'varchar' => 'varchar', 'text' => 'text', 'longtext' => 'longtext'),
-        '#required' => false,
+        '#required' => FALSE,
       );
       $form['database']['data_length'] = array(
         '#type' => 'textfield',
         '#title' => t('Data Length'),
-        '#default_value' => isset($field['data_length']) ? $field['data_length'] : null,
-        '#required' => false,
+        '#default_value' => !empty($field['data_length']) ? $field['data_length'] : NULL,
+        '#required' => FALSE,
       );
       $form['database']['data_default'] = array(
         '#type' => 'textfield',
         '#title' => t('Default Value'),
         '#default_value' => $field['data_default'],
-        '#required' => false,
+        '#required' => FALSE,
       );
-      $form['database']['data_not_null'] = array(
+      $form['database']['data_not_NULL'] = array(
         '#type' => 'checkbox',
         '#title' => t('Not NULL'),
-        '#default_value' => isset($field['data_not_null']) ? $field['data_not_null'] : false,
+        '#default_value' => is_numeric($field['data_not_NULL']) ? $field['data_not_NULL'] : FALSE,
       );
       $form['database']['data_sortable'] = array(
         '#type' => 'checkbox',
         '#title' => t('Sortable'),
-        '#default_value' => isset($field['data_sortable']) ? $field['data_sortable'] : true,
+        '#default_value' => is_numeric($field['data_sortable']) ? $field['data_sortable'] : TRUE,
       );
       return $form;
     case 'validate':
@@ -82,7 +91,7 @@
       }
       break;
     case 'save':
-      return array('code', 'display', 'display_format', 'store', 'data_type', 'data_length', 'data_not_null', 'data_default', 'data_sortable');
+      return array('code', 'display', 'display_format', 'store', 'data_type', 'data_length', 'data_not_NULL', 'data_default', 'data_sortable');
     case 'database columns':
       if ($field['store']) {
         $columns = array('value' => array());
@@ -91,8 +100,8 @@
         if (!($field['data_type'] == 'text' || $field['data_type'] == 'longtext')) {
           $columns['value']['length'] = isset($field['data_length']) ? $field['data_length'] : 32;
         }
-        $columns['value']['not null'] = isset($field['data_not_null']) ? $field['data_not_null'] : true;
-        $columns['value']['sortable'] = isset($field['data_sortable']) ? $field['data_sortable'] : false;
+        $columns['value']['not NULL'] = isset($field['data_not_NULL']) ? $field['data_not_NULL'] : TRUE;
+        $columns['value']['sortable'] = isset($field['data_sortable']) ? $field['data_sortable'] : FALSE;
         if ($field['data_default'] != '')  {
           $columns['value']['default'] = $field['data_default'];
         }
@@ -110,6 +119,17 @@
       return array(
         'view' => CONTENT_CALLBACK_CUSTOM,
       );
+    case 'views data':
+      $allowed_values = content_allowed_values($field);
+      if (count($allowed_values)) {
+        $data = content_views_field_views_data($field);
+        $db_info = content_database_info($field);
+        $table_alias = content_views_tablename($field);
+
+        // Swap the filter handler to the 'in' operator.
+        $data[$table_alias][$field['field_name'] .'_value']['filter']['handler'] = 'views_handler_filter_many_to_one_content';
+        return $data;
+      }
   }
 }
 
@@ -117,10 +137,6 @@
   if (isset($field['code'])) {
     eval($field['code']);
   }
-  // fall back on old widget code if field code hasn't been set
-  elseif (isset($field['widget']['code'])) {
-    eval($field['widget']['code']);
-  }
 }
 
 /**
@@ -161,6 +177,7 @@
     'computed' => array(
       'label' => t('Computed'),
       'field types' => array('computed'),
+      'multiple values' => CONTENT_HANDLE_MODULE,
     ),
   );
 }
@@ -173,7 +190,7 @@
     case 'form':
       $form = array();
 
-      $form[$field['field_name']] = array('#tree' => true);
+      $form[$field['field_name']] = array('#tree' => TRUE);
 
       $form[$field['field_name']][0]['value'] = array(
         '#type' => 'value',
@@ -187,7 +204,7 @@
 /**
  * Implementation of cck hook_view_item (obsolete, retained for backward compatibility with older cck)
  */
-function computed_field_view_item($field, $node_field_item, $node = null) {
+function computed_field_view_item($field, $node_field_item, $node = NULL) {
   global $base_url;
   if ($field['display']) {
     $display = '';
@@ -197,6 +214,26 @@
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function computed_field_theme() {
+  return array(
+    'computed_field_formatter_default' => array(
+      'arguments' => array('element' => NULL),
+    ),
+    'computed_field_formatter_plain' => array(
+      'arguments' => array('element' => NULL),
+    ),
+    'computed_field_formatter_markup' => array(
+      'arguments' => array('element' => NULL),
+    ),
+    'computed_field_formatter_raw_value' => array(
+      'arguments' => array('element' => NULL),
+    ),
+  );
+}
+
+/**
  * Implementation of cck hook_field_formatter_info()
  */
 function computed_field_field_formatter_info() {
@@ -213,30 +250,82 @@
       'label' => t('Markup'),
       'field types' => array('computed'),
     ),
+    'computed_value' => array(
+      'label' => t('Computed Value'),
+      'field types' => array('computed'),
+    ),
   );
 }
 
 /**
- * Implementation of cck hook_field_formatter():
+ * Theme function for 'default' text field formatter.
  */
-function computed_field_field_formatter($field, $item, $formatter, $node) {
-  // backward compatibility with computed_field_view_item
-  global $base_url;
-  $node_field_item = $item;
+function theme_computed_field_formatter_default($element) {
+  $field = content_fields($element['#field_name']);
+  // For "some" backwards compatibility
+  $node_field_item['value'] = $element['#item']['value'];
+  eval($field['display_format']);
+  return $display;
+}
 
-  $display = '';
+/**
+ * Theme function for 'plain' text field formatter.
+ */
+function theme_computed_field_formatter_plain($element) {
+  $field = content_fields($element['#field_name']);
+  // For "some" backwards compatibility
+  $node_field_item['value'] = $element['#item']['value'];
+  eval($field['display_format']);
+  return check_plain($display);
+}
 
-  if ($field['display']) {
-    eval($field['display_format']);
+/**
+ * Theme function for 'markup' text field formatter.
+ */
+function theme_computed_field_formatter_markup($element) {
+  $field = content_fields($element['#field_name']);
+  // For "some" backwards compatibility
+  $node_field_item['value'] = $element['#item']['value'];
+  eval($field['display_format']);
+  return check_markup($display);
+}
+
+/**
+ * Theme function for 'computed_value' text field formatter.
+ */
+function theme_computed_field_formatter_computed_value($element) {
+  return $element['#item']['value'];
+}
+
+/**
+ * Implementation of cck hook_content_is_empty().
+ */
+function computed_field_content_is_empty() {
+  return FALSE;
+}
+
+/**
+ * Implementation of hook_token_list().
+ */
+function computed_field_token_list($type = 'all') {
+  if ($type == 'field' || $type == 'all') {
+    $tokens = array();
+
+    $tokens['computed_field']['raw']       = t("Raw, unfiltered text.");
+    $tokens['computed_field']['formatted'] = t("Formatted and filtered text.");
+
+    return $tokens;
   }
+}
 
-  switch ($formatter) {
-    case 'plain':
-      return check_plain($display);
-    case 'markup':
-      return check_markup($display);    
-    case 'default':
-    default:
-      return $display;
+/**
+ * Implementation of hook_token_values().
+ */
+function computed_field_token_values($type, $object = NULL, $options = array()) {
+  if ($type == 'field') {
+    $item = $object[0];
+    $tokens['raw']  = $item['value'];
+    $tokens['formatted'] = $item['view'];
+    return $tokens;
   }
 }
