Synch HEAD with latest Bazaar code, mostly changes to get allowed values working.
authorKaren Stevenson
Sat, 31 Jan 2009 02:55:39 +0000 (02:55 +0000)
committerKaren Stevenson
Sat, 31 Jan 2009 02:55:39 +0000 (02:55 +0000)
cck.module
includes/cck.admin.inc
includes/cck.list.inc

index e426fa6..8d53053 100644 (file)
@@ -587,14 +587,26 @@ function cck_serialized_settings() {
  * 
  * CCK uses the 'cck_field_settings' table to store custom settings
  * not used by core.
+ * 
+ * Field settings will have no $instance nor a db bundle column.
  */
-function cck_field_get_setting($setting, $setting_type, $field, $instance) {
-  $value = db_select('cck_field_settings', 'fs')->fields('fs', array('setting_option'))
-    ->condition('fs.setting', $setting)
-    ->condition('fs.setting_type', $setting_type)
-    ->condition('fs.field_name', $field['field_name'])
-    ->condition('fs.bundle', $instance['bundle'])
-    ->execute()->fetchField();
+function cck_field_get_setting($setting, $setting_type, $field, $instance = NULL) {
+  if ($setting_type == 'field' || empty($instance)) {
+    $value = db_select('cck_field_settings', 'fs')->fields('fs', array('setting_option'))
+      ->condition('fs.setting', $setting)
+      ->condition('fs.setting_type', $setting_type)
+      ->condition('fs.field_name', $field['field_name'])
+      ->execute()->fetchField();    
+  }
+  else {
+    $value = db_select('cck_field_settings', 'fs')->fields('fs', array('setting_option'))
+      ->condition('fs.setting', $setting)
+      ->condition('fs.setting_type', $setting_type)
+      ->condition('fs.field_name', $field['field_name'])
+      ->condition('fs.bundle', $instance['bundle'])
+      ->execute()->fetchField();
+  }
+  
   if (in_array($setting, cck_serialized_settings())) {
     $value = unserialize($value);
   }
@@ -606,22 +618,34 @@ function cck_field_get_setting($setting, $setting_type, $field, $instance) {
  * 
  * CCK uses the 'cck_field_settings' table to store custom settings
  * not used by core.
+ * 
+ * Field settings will have no $instance nor a db bundle column.
  */
-function cck_field_set_setting($setting, $setting_type, $value, $field, $instance) {
+function cck_field_set_setting($setting, $setting_type, $value, $field, $instance = NULL) {
   // Delete any prior values.
-  db_delete('cck_field_settings')
-    ->condition('field_name', $field['field_name'])
-    ->condition('bundle', $instance['bundle'])
-    ->condition('setting', $setting)
-    ->condition('setting_type', $setting_type)
-    ->execute();
+  $bundle = ($setting_type == 'field' || empty($instance)) ? NULL : $instance['bundle'];
+  if ($setting_type == 'field' || empty($instance)) {
+    db_delete('cck_field_settings')
+      ->condition('field_name', $field['field_name'])
+      ->condition('setting', $setting)
+      ->condition('setting_type', $setting_type)
+      ->execute();
+  }
+  else {
+    db_delete('cck_field_settings')
+      ->condition('field_name', $field['field_name'])
+      ->condition('bundle', $bundle)
+      ->condition('setting', $setting)
+      ->condition('setting_type', $setting_type)
+      ->execute();
+  }
   // Create the new values.
   if (in_array($setting, cck_serialized_settings())) {
     $value = serialize($value);
   }
   $record = array(
     'field_name' => $field['field_name'],
-    'bundle' => $instance['bundle'],
+    'bundle' => $bundle,
     'setting' => $setting,
     'setting_option' => $value,
     'setting_type' => $setting_type,
index 26e0bc1..9c2e659 100644 (file)
@@ -57,7 +57,6 @@ function cck_types_overview() {
 function cck_fields_list() {
   $instances = field_info_instances();
   $field_types = field_info_field_types();
-
   $bundles = field_info_bundles();
   $header = array(t('Field name'), t('Field type'), t('Used in'));
   $rows = array();
index 5987b88..1d9e308 100644 (file)
@@ -2,18 +2,11 @@
 // $Id$
 
 /**
- * Get the PHP code for allowed values for a field.
- */
-function cck_list_get_allowed_values_php($field) {
-  return db_query("SELECT f.setting_option FROM {cck_field_settings} f WHERE setting_type='field' AND setting='allowed_values_php' AND field_name = :field_name", array(':field_name' => $field['field_name']))->fetchField();
-}
-
-/**
  * A custom function to return allowed values from PHP code.
  */
 function cck_list_allowed_values_php($field) {
   $allowed_values = array();
-  $php = cck_list_get_allowed_values_php($field);
+  $php = cck_field_get_setting('allowed_values_php', 'field', $field);
   if (!empty($php)) {
     ob_start();
     $result = eval($php);
@@ -30,15 +23,14 @@ function cck_list_allowed_values_php($field) {
  * on behalf of core List module.
  */
 function list_field_settings_form($field, $instance) {
-    
   $form = array(
     '#element_validate' => array('list_field_settings_form_validate'),
     );
   
-  $defaults = list_field_settings($field['type']);
+  $defaults = field_info_field_settings($field['type']);
   $settings = array_merge($defaults, $field['settings']);
-  $settings['allowed_values_php'] = cck_list_get_allowed_values_php($field);
-  
+  $settings['allowed_values_php'] = cck_field_get_setting('allowed_values_php', 'field', $field);
+  $settings['allowed_values_function'] = !empty($field['allowed_values_function']) && $field['allowed_values_function'] != 'cck_list_allowed_values_php' ? $field['allowed_values_function'] : '';
   $form['allowed_values'] = array(
     '#type' => 'textarea',
     '#title' => t('Allowed values list'),
@@ -87,17 +79,10 @@ function list_field_settings_form_validate($form, &$form_state) {
   $field = $form_values['field'];
   $field_name = $field['field_name'];
   $option = $form_values['field']['settings']['advanced_options']['allowed_values_php'];
-  db_query("DELETE FROM {cck_field_settings} WHERE setting='field' AND setting_type='allowed_values_php' AND field_name = ':field_name'", array(':field_name' => $field['field_name']));
-  if (!empty($option)) {
-    $record = array(
-      'field_name' => $field_name,
-      'bundle' => NULL,
-      'setting_type' => 'field',
-      'setting' => 'allowed_values_php',
-      'setting_option' => $option,
-    );
-    drupal_write_record('cck_field_settings', $record, array());  
+  cck_field_set_setting('allowed_values_php', 'field', $option, $field);
+  
+  if (empty($form_values['allowed_values_function'])) {
     form_set_value($form['allowed_values_function'], 'cck_list_allowed_values_php', $form_state);
-    form_set_value($form['advanced_options'], NULL, $form_state);    
   }
+  form_set_value($form['advanced_options'], NULL, $form_state);    
 }