#687788 by moshe weitzman. Support file and image fields in D7's devel_generate....
authormoshe weitzman
Thu, 28 Jan 2010 21:06:42 +0000 (21:06 +0000)
committermoshe weitzman
Thu, 28 Jan 2010 21:06:42 +0000 (21:06 +0000)
14 files changed:
devel_generate.fields.inc [deleted file]
devel_generate/devel_generate.drush.inc [moved from devel_generate.drush.inc with 95% similarity]
devel_generate/devel_generate.fields.inc [new file with mode: 0644]
devel_generate/devel_generate.inc [moved from devel_generate.inc with 99% similarity]
devel_generate/devel_generate.info [new file with mode: 0644]
devel_generate/devel_generate.module [moved from devel_generate.module with 93% similarity]
devel_generate/devel_generate_batch.inc [new file with mode: 0644]
devel_generate/file.devel_generate.inc [new file with mode: 0644]
devel_generate/image.devel_generate.inc [new file with mode: 0644]
devel_generate/nodereference.devel_generate.inc [new file with mode: 0644]
devel_generate/number.devel_generate.inc [new file with mode: 0644]
devel_generate/taxonomy.devel_generate.inc [new file with mode: 0644]
devel_generate/text.devel_generate.inc [new file with mode: 0644]
devel_generate/userreference.devel_generate.inc [new file with mode: 0644]

diff --git a/devel_generate.fields.inc b/devel_generate.fields.inc
deleted file mode 100644 (file)
index 8f5209c..0000000
+++ /dev/null
@@ -1,240 +0,0 @@
-<?php
-// $Id$
-
-/**
- * @file
- * Functions needed for Devel module integration.
- */
-
-/**
- * Enrich the $object that is about to be saved with arbitrary
- * information in each of its fields.
- **/
-function cck_generate_fields(&$object, $obj_type, $bundle) {
-  $field_types = field_info_field_types();
-  $instances = field_info_instances($obj_type, $bundle);
-  foreach ($instances as $instance) {
-    $field_name = $instance['field_name'];
-    $table_name = _field_sql_storage_tablename($field_name);
-    $revision_name = _field_sql_storage_revision_tablename($field_name);
-    $field = field_read_field($field_name);
-
-    $object_field = array();
-    // If module handles own multiples, then only call its hook once.
-    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-      $max = 0;
-    }
-    else {
-      switch ($field['cardinality']) {
-        case FIELD_CARDINALITY_UNLIMITED:
-          $max = rand(0, 3); //just an arbitrary number for 'unlimited'
-          break;
-        default:
-          $max = $field['cardinality'] - 1;
-          break;
-      }
-    }
-    for ($i = 0; $i <= $max; $i++) {
-      $module = $field_types[$field['type']]['module'];
-      $function = $module .'_cck_generate';
-      if (function_exists($function)) {
-        $result = $function($object, $field, $instance, $bundle); // $items, $teaser, $page
-        if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-          // Fields that handle their own multiples will add their own deltas.
-          $object_field = $result;
-        }
-        else {
-          // When multiples are handled by the content module, add a delta for each result.
-          $object_field[$i] = $result;
-        }
-      }
-    }
-    // TODO Come back later and see if we need
-    // a valid language code or if 'none' is OK.
-    // TODO: Completely overriding any existing $object->{$field['field_name']}
-    // is necessary here because the forum module has a bug where it
-    // initializes the property with incorrect data.
-    // @see http://drupal.org/node/652176
-    $object->{$field['field_name']} = array(
-      LANGUAGE_NONE => $object_field,
-    );
-  }
-}
-
-/**
- * A simple function to return multiple values for fields that use
- * custom multiple value widgets but don't need any other special multiple
- * values handling. This will call the field generation function
- * a random number of times and compile the results into a node array.
- */
-function cck_devel_multiple($function, $object, $field, $instance, $bundle) {
-  $object_field = array();
-  if (function_exists($function)) {
-    switch ($field['cardinality']) {
-      case FIELD_CARDINALITY_UNLIMITED:
-        $max = rand(0, 3); //just an arbitrary number for 'unlimited'
-        break;
-      default:
-        $max = $field['cardinality'] - 1;
-        break;
-    }
-    for ($i = 0; $i <= $max; $i++) {
-      $object_field[$i] = $function($object, $field, $instance, $bundle);
-    }
-  }
-  return $object_field;
-}
-
-if (module_exists('text')) {
-  function text_cck_generate($object, $field, $instance, $bundle) {
-    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-      return cck_devel_multiple('_text_cck_generate', $object, $field, $instance, $bundle);
-    }
-    else {
-      return _text_cck_generate($object, $field, $instance, $bundle);
-    }
-  }
-
-  function _text_cck_generate($object, $field, $instance, $bundle) {
-    $object_field = array();
-    if (!empty($instance['settings']['text_processing'])) {
-      $formats = filter_formats();
-      $format = array_rand($formats);
-    }
-    else {
-      $format = 0;
-    }
-    if ($instance['widget']['type'] != 'text_textfield') {
-      $object_field['value'] = devel_create_content($format);
-      if ($instance['widget']['type'] == 'text_textarea_with_summary' && !empty($instance['display_summary'])) {
-        $object_field['summary'] = devel_create_content($format);
-      }
-    }
-    else {
-      // Generate a value that respects max_length.
-      if (empty($field['max_length'])) {
-        $field['max_length'] = 12;
-      }
-      $object_field['value'] = user_password($field['max_length']);
-    }
-    $object_field['format'] = $format;
-    return $object_field;
-  }
-}
-
-if (module_exists('number')) {
-  function number_cck_generate($object, $field, $instance, $bundle) {
-    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-      return cck_devel_multiple('_number_cck_generate', $object, $field, $instance, $bundle);
-    }
-    else {
-      return _number_cck_generate($object, $field, $instance, $bundle);
-    }
-  }
-
-  function _number_cck_generate($object, $field, $instance, $bundle) {
-    $object_field = array();
-    // Make sure the field settings are all set.
-    foreach (array('min', 'max', 'precision', 'scale') as $key) {
-      if (empty($field[$key])) {
-        $field[$key] = NULL;
-      }
-    }
-    $min = is_numeric($field['min']) ? $field['min'] : 0;
-    switch ($field['type']) {
-      case 'number_integer':
-        $max = is_numeric($field['max']) ? $field['max'] : 10000;
-        $decimal = 0;
-        $scale = 0;
-        break;
-
-      case 'number_decimal':
-        $precision = is_numeric($field['precision']) ? $field['precision'] : 10;
-        $scale = is_numeric($field['scale']) ? $field['scale'] : 2;
-        $max = is_numeric($field['max']) ? $field['max'] : pow(10, ($precision - $scale));
-        $decimal = rand(0, (10 * $scale)) / 100;
-        break;
-
-      case 'number_float':
-        $precision = rand(10, 32);
-        $scale = rand(0, 2);
-        $decimal = rand(0, (10 * $scale)) / 100;
-        $max = is_numeric($field['max']) ? $field['max'] : pow(10, ($precision - $scale));
-        break;
-    }
-    $object_field['value'] = round((rand($min, $max) + $decimal), $scale);
-    return $object_field;
-  }
-}
-
-if (module_exists('nodereference')) {
-  function nodereference_cck_generate($object, $field, $instance, $bundle) {
-    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-      return cck_devel_multiple('_nodereference_cck_generate', $object, $field, $instance, $bundle);
-    }
-    else {
-      return _nodereference_cck_generate($object, $field, $instance, $bundle);
-    }
-  }
-
-  function _nodereference_cck_generate($object, $field, $instance, $bundle) {
-    $object_field = array();
-    $allowed_values = nodereference_allowed_values($field);
-    unset($allowed_values[0]);
-    if (!empty($allowed_values)) {
-      // Just pick one of the specified allowed values.
-      $object_field['nid'] = array_rand($allowed_values);
-    }
-    return $object_field;
-  }
-}
-
-if (module_exists('userreference')) {
-  function userreference_cck_generate($object, $field) {
-    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-      return cck_devel_multiple('_userreference_cck_generate', $object, $field);
-    }
-    else {
-      return _userreference_cck_generate($object, $field);
-    }
-  }
-
-  function _userreference_cck_generate($object, $field) {
-    $object_field = array();
-    $allowed_values = userreference_allowed_values($field);
-    if (isset($allowed_values['none'])) {
-      unset($allowed_values['none']);
-    }
-    if (!empty($allowed_values)) {
-      // Just pick one of the specified allowed values.
-      $object_field['uid'] = array_rand($allowed_values);
-    }
-    return $object_field;
-  }
-}
-
-if (module_exists('taxonomy')) {
-  function taxonomy_cck_generate($object, $field, $instance, $bundle) {
-    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
-      return cck_devel_multiple('_taxonomy_cck_generate', $object, $field, $instance, $bundle);
-    }
-    else {
-      return _taxonomy_cck_generate($object, $field, $instance, $bundle);
-    }
-  }
-
-  function _taxonomy_cck_generate($object, $field, $instance, $bundle) {
-    $object_field = array();
-    // TODO: For free tagging vocabularies that do not already have terms, this
-    // will not result in any tags being added.
-    $allowed_values = taxonomy_allowed_values($field);
-    if (!empty($allowed_values)) {
-      // Pick one of the allowed values.
-      $object_field['tid'] = array_rand($allowed_values);
-    }
-    else {
-      $object_field['tid'] = 0;
-    }
-    return $object_field;
-  }
-}
similarity index 95%
rename from devel_generate.drush.inc
rename to devel_generate/devel_generate.drush.inc
index 334557e..602f588 100644 (file)
@@ -57,7 +57,6 @@ function devel_generate_drush_command() {
     'options' => array(
       'kill' => 'Specify \'kill\' to delete all content before generating new content.',
       'types' => 'A comma delimited list of content types to create. Defaults to page,article.',
-      'add-upload' => 'Add an attached file to generated nodes.',
     ),
     'aliases' => array('genc'),
   );
@@ -116,11 +115,10 @@ function drush_devel_generate_content($num_nodes = NULL, $max_comments = NULL) {
   $values['values']['title_length'] = '8';
   $values['values']['num_nodes'] = $num_nodes;
   $values['values']['max_comments'] = $max_comments;
-  $values['values']['add_upload'] = drush_get_option('add-upload', FALSE);
   $values['values']['node_types'] = drupal_map_assoc(explode(',', drush_get_option('types', 'page,article')));
   drush_generate_include_devel();
   devel_generate_content($values);
-  drush_log(t('Generated @num_nodes nodes, @max_comments comments per node', array('@num_nodes' => (int)$num_nodes, '@max_comments' => (int)$max_comments)), 'success');
+  drush_log(t('Generated @num_nodes nodes, @max_comments comments (or less) per node.', array('@num_nodes' => (int)$num_nodes, '@max_comments' => (int)$max_comments)), 'success');
 }
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/devel_generate/devel_generate.fields.inc b/devel_generate/devel_generate.fields.inc
new file mode 100644 (file)
index 0000000..1190d7a
--- /dev/null
@@ -0,0 +1,196 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Functions needed for devel_generate Fields API integration.
+ */
+
+/**
+ * Enrich the $object that is about to be saved with arbitrary
+ * information in each of its fields.
+ **/
+function devel_generate_fields(&$object, $obj_type, $bundle) {
+  $field_types = field_info_field_types();
+  $instances = field_info_instances($obj_type, $bundle);
+  foreach ($instances as $instance) {
+    $field_name = $instance['field_name'];
+    $table_name = _field_sql_storage_tablename($field_name);
+    $revision_name = _field_sql_storage_revision_tablename($field_name);
+    $field = field_read_field($field_name);
+
+    $object_field = array();
+    // If module handles own multiples, then only call its hook once.
+    if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+      $max = 0;
+    }
+    else {
+      switch ($field['cardinality']) {
+        case FIELD_CARDINALITY_UNLIMITED:
+          $max = rand(0, 3); //just an arbitrary number for 'unlimited'
+          break;
+        default:
+          $max = $field['cardinality'] - 1;
+          break;
+      }
+    }
+      for ($i = 0; $i <= $max; $i++) {
+      $module = $field_types[$field['type']]['module'];
+
+      // Include any support file that might exist for this field.
+      if (in_array($module, array('file', 'image', 'taxonomy', 'number', 'text', 'userreference', 'nodereference', 'comment'))) {
+        // devel_generate implements on behalf of core and special friends.
+        module_load_include('inc', 'devel_generate', "$module.devel_generate");
+      }
+      else {
+        module_load_include('inc', $module, "$module.devel_generate");
+      }
+      $function = $module . '_devel_generate';
+      if (function_exists($function)) {
+        $result = $function($object, $field, $instance, $bundle); // $items, $teaser, $page
+        if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+          // Fields that handle their own multiples will add their own deltas.
+          $object_field = $result;
+        }
+        else {
+          // When multiples are handled by the content module, add a delta for each result.
+          $object_field[$i] = $result;
+        }
+      }
+    }
+    // TODO Come back later and see if we need
+    // a valid language code or if 'none' is OK.
+    // TODO: Completely overriding any existing $object->{$field['field_name']}
+    // is necessary here because the forum module has a bug where it
+    // initializes the property with incorrect data.
+    // @see http://drupal.org/node/652176
+    $object->{$field['field_name']} = array(
+      LANGUAGE_NONE => $object_field,
+    );
+  }
+}
+
+/**
+ * A simple function to return multiple values for fields that use
+ * custom multiple value widgets but don't need any other special multiple
+ * values handling. This will call the field generation function
+ * a random number of times and compile the results into a node array.
+ */
+function devel_generate_multiple($function, $object, $field, $instance, $bundle) {
+  $object_field = array();
+  if (function_exists($function)) {
+    switch ($field['cardinality']) {
+      case FIELD_CARDINALITY_UNLIMITED:
+        $max = rand(0, 3); //just an arbitrary number for 'unlimited'
+        break;
+      default:
+        $max = $field['cardinality'] - 1;
+        break;
+    }
+    for ($i = 0; $i <= $max; $i++) {
+      $object_field[$i] = $function($object, $field, $instance, $bundle);
+    }
+  }
+  return $object_field;
+}
+
+/**
+ * Generate an image based on the properties of a field.
+ *
+ * Inspect the minimum and maximum image sizes and makes sure the generated
+ * image matches the requirements.
+ *
+ * @return
+ *   The path to the new file, in the temporary directory.
+ */
+function devel_generate_file($instance) {
+  if (empty($instance['settings']['file_extensions'])) {
+    $instance['settings']['file_extensions'] = 'png jpg jpeg txt';
+  }
+
+  $extensions = array_intersect(explode(' ', $instance['settings']['file_extensions']), array('png', 'jpg', 'jpeg', 'txt'));
+  $extension = array_rand(drupal_map_assoc($extensions));
+
+  if ($extension == 'txt') {
+    $filesize = empty($instance['settings']['max_filesize']) ? 1024 : parse_size($instance['settings']['max_filesize']);
+    return devel_generate_textfile($filesize);
+  }
+  elseif (in_array($extension, array('png', 'jpg', 'jpeg')) && function_exists('imagecreate')) {
+    $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
+    $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
+    return devel_generate_image($extension, $min_resolution, $max_resolution);
+  }
+
+  return FALSE;
+}
+
+/**
+ * Private function for generating a random text file.
+ */
+function devel_generate_textfile($filesize = 1024) {
+  static $filesizes = array();
+
+  $temp_file = FALSE;
+  if (isset($filesizes[$filesize])) {
+    $temp_file = $filesizes[$filesize];
+  }
+  elseif ($tmp_file = drupal_tempnam('temporary://', 'filefield_')) {
+    $destination = $tmp_file . '.txt';
+    file_unmanaged_move($tmp_file, $destination);
+
+    $fp = fopen($destination, 'w');
+    fwrite($fp, str_repeat('01', $filesize/2));
+    fclose($fp);
+    $filesizes[$filesize] = $destination;
+  }
+
+  return $destination;
+}
+
+/**
+ * Private function for creating a random image.
+ *
+ * This function only works with the GD toolkit. ImageMagick is not supported.
+ */
+function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
+  static $images = array();
+
+  // Generate a max of 5 different images.
+  if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) < 5) {
+    if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
+      $destination = $tmp_file . '.' . $extension;
+      file_unmanaged_move($tmp_file, $destination);
+
+      $min = explode('x', $min_resolution);
+      $max = explode('x', $max_resolution);
+
+      $width = rand((int)$min[0], (int)$max[0]);
+      $height = rand((int)$min[0], (int)$max[0]);
+
+      // Make a image split into 4 sections with random colors.
+      $im = imagecreate($width, $height);
+      for ($n = 0; $n < 4; $n++) {
+        $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
+        $x = $width/2 * ($n % 2);
+        $y = $height/2 * (int) ($n >= 2);
+        imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color);
+      }
+
+      // Make a perfect circle in the image middle.
+      $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
+      $smaller_dimension = min($width, $height);
+      $smaller_dimension = ($smaller_dimension % 2) ? $smaller_dimension : $smaller_dimension;
+      imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color);
+
+      $save_function = 'image'. ($extension == 'jpg' ? 'jpeg' : $extension);
+      $save_function($im, drupal_realpath($destination));
+
+      $images[$extension][$min_resolution][$max_resolution][$destination] = $destination;
+    }
+  }
+  // Select one of the images we've already generated for this field.
+  else {
+    $destination = array_rand($images[$extension][$min_resolution][$max_resolution]);
+  }
+  return $destination;
+}
similarity index 99%
rename from devel_generate.inc
rename to devel_generate/devel_generate.inc
index d5d899c..4fa9dc5 100644 (file)
@@ -148,7 +148,7 @@ function devel_generate_add_comments($node, $users, $max_comments, $title_length
     $comment->language = LANGUAGE_NONE;
     // Populate all core fields on behalf of field.module
     module_load_include('inc', 'devel_generate', 'devel_generate.fields');
-    cck_generate_fields($comment, 'comment', 'comment_node_' . $node->type);
+    devel_generate_fields($comment, 'comment', 'comment_node_' . $node->type);
     comment_save($comment);
   }
 }
@@ -499,7 +499,7 @@ function devel_generate_content_add_node(&$results) {
 
   // Populate all core fields on behalf of field.module
   module_load_include('inc', 'devel_generate', 'devel_generate.fields');
-  cck_generate_fields($node, 'node', $node->type);
+  devel_generate_fields($node, 'node', $node->type);
 
   // See devel_generate_nodeapi() for actions that happen before and after this save.
   node_save($node);
diff --git a/devel_generate/devel_generate.info b/devel_generate/devel_generate.info
new file mode 100644 (file)
index 0000000..b0cdecf
--- /dev/null
@@ -0,0 +1,9 @@
+; $Id$
+name = Devel generate
+description = Generate dummy users, nodes, and taxonomy terms.
+package = Development
+core = 7.x
+files[] = devel_generate.module
+files[] = devel_generate.inc
+files[] = devel_generate_batch.inc
+files[] = devel_generate.drush.inc
similarity index 93%
rename from devel_generate.module
rename to devel_generate/devel_generate.module
index ce141d6..8616c36 100644 (file)
@@ -157,13 +157,6 @@ function devel_generate_content_form() {
     '#default_value' => 4,
     '#size' => 10,
   );
-  $form['add_upload'] = array(
-    '#type' =>  'checkbox',
-    '#disabled' => !module_exists('upload'),
-    '#description' => t('Requires upload.module'),
-    '#title' => t('Add an upload to each node'),
-    '#default_value' => FALSE,
-  );
   $form['add_alias'] = array(
     '#type' => 'checkbox',
     '#disabled' => !module_exists('path'),
@@ -236,17 +229,6 @@ function devel_generate_taxonomy_form_submit($form_id, &$form_state) {
   devel_generate_taxonomy_data($form_state['values']['num_vocab'], $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
 }
 
-// Modules that want to affect generated nodes may implement hook_node('presave'). See OG module or CCK.
-// A few implementations live here because core doesn't do bulk node generation.
-function devel_generate_node_presave(&$node) {
-  if (isset($node->devel_generate)) {
-    $results = $node->devel_generate;
-    if (!empty($results['add_upload'])) {
-      devel_generate_add_upload($node);
-    }
-  }
-}
-
 function devel_generate_node_insert(&$node) {
   if (isset($node->devel_generate)) {
     $results = $node->devel_generate;
diff --git a/devel_generate/devel_generate_batch.inc b/devel_generate/devel_generate_batch.inc
new file mode 100644 (file)
index 0000000..6884fc3
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+// $Id$
+
+/**
+ * Devel Generate batch handling functions using the BatchAPI
+ */
+
+/**
+ * Functions called from FAPI:
+ */
+
+function devel_generate_batch_content($form_state) {
+  $operations = array();
+
+  // Setup the batch operations and save the variables.
+  $operations[] = array('devel_generate_batch_content_pre_node', array($form_state['values']));
+
+  // add the kill operation
+  if ($form_state['values']['kill_content']) {
+    $operations[] = array('devel_generate_batch_content_kill', array());
+  }
+
+  // add the operations to create the nodes
+  for ($num = 0; $num < $form_state['values']['num_nodes']; $num ++) {
+    $operations[] = array('devel_generate_batch_content_add_node', array());
+  }
+
+  // start the batch
+  $batch = array(
+    'title' => t('Generating Content'),
+    'operations' => $operations,
+    'finished' => 'devel_generate_batch_finished',
+    'file' => drupal_get_path('module', 'devel_generate') . '/devel_generate_batch.inc',
+  );
+  batch_set($batch);
+}
+
+/**
+ * Create Content Batch Functions:
+ */
+
+function devel_generate_batch_content_kill(&$context) {
+  module_load_include('inc', 'devel_generate', 'devel_generate');
+  devel_generate_content_kill($context['results']);
+}
+
+function devel_generate_batch_content_pre_node($vars, &$context) {
+  $context['results'] = $vars;
+  $context['results']['num_nids'] = 0;
+  module_load_include('inc', 'devel_generate', 'devel_generate');
+  devel_generate_content_pre_node($context['results']);
+}
+
+function devel_generate_batch_content_add_node(&$context) {
+  module_load_include('inc', 'devel_generate', 'devel_generate');
+  devel_generate_content_add_node($context['results']);
+  $context['results']['num_nids'] ++;
+}
+
+function devel_generate_batch_finished($success, $results, $operations) {
+  if ($success) {
+    $message = t('Finished @num_nids nodes created successfully.', array('@num_nids' => $results['num_nids']));
+  }
+  else {
+    $message = t('Finished with an error.');
+  }
+  drupal_set_message($message);
+}
+
diff --git a/devel_generate/file.devel_generate.inc b/devel_generate/file.devel_generate.inc
new file mode 100644 (file)
index 0000000..f26035b
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+// $Id$
+
+function file_devel_generate($object, $field, $instance, $bundle) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_file_devel_generate', $object, $field, $instance, $bundle);
+  }
+  else {
+    return _file_devel_generate($object, $field, $instance, $bundle);
+  }
+}
+
+function _file_devel_generate($object, $field, $instance, $bundle) {
+  $object_field = array();
+
+  if ($path = devel_generate_file($instance)) {
+    $source->uri = $path;
+    $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
+    $file = file_move($source, $destination);
+  
+    $object_field['fid'] = $file->fid;
+    $object_field['display'] = $field['settings']['display_default'];
+    $object_field['description'] = devel_create_greeking(10);
+  }
+
+  return $object_field;
+}
diff --git a/devel_generate/image.devel_generate.inc b/devel_generate/image.devel_generate.inc
new file mode 100644 (file)
index 0000000..f2e11f2
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+// $Id$
+
+function image_devel_generate($object, $field, $instance, $bundle) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_image_devel_generate', $object, $field, $instance, $bundle);
+  }
+  else {
+    return _image_devel_generate($object, $field, $instance, $bundle);
+  }
+}
+
+function _image_devel_generate($object, $field, $instance, $bundle) {
+  $object_field = array();
+
+  if ($path = devel_generate_file($instance)) {
+    $source->uri = $path;
+    $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
+    $file = file_move($source, $destination);
+
+    $object_field['fid'] = $file->fid;
+    $object_field['alt'] = devel_create_greeking(4);
+    $object_field['title'] = devel_create_greeking(10);
+  }
+
+  return $object_field;
+}
diff --git a/devel_generate/nodereference.devel_generate.inc b/devel_generate/nodereference.devel_generate.inc
new file mode 100644 (file)
index 0000000..ca9ed16
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+// $Id$
+
+function nodereference_devel_generate($object, $field, $instance, $bundle) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_nodereference_devel_generate', $object, $field, $instance, $bundle);
+  }
+  else {
+    return _nodereference_devel_generate($object, $field, $instance, $bundle);
+  }
+}
+
+function _nodereference_devel_generate($object, $field, $instance, $bundle) {
+  $object_field = array();
+  $allowed_values = nodereference_allowed_values($field);
+  unset($allowed_values[0]);
+  if (!empty($allowed_values)) {
+    // Just pick one of the specified allowed values.
+    $object_field['nid'] = array_rand($allowed_values);
+  }
+  return $object_field;
+}
diff --git a/devel_generate/number.devel_generate.inc b/devel_generate/number.devel_generate.inc
new file mode 100644 (file)
index 0000000..f6dc712
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+// Id$
+
+function number_devel_generate($object, $field, $instance, $bundle) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_number_devel_generate', $object, $field, $instance, $bundle);
+  }
+  else {
+    return _number_devel_generate($object, $field, $instance, $bundle);
+  }
+}
+
+function _number_devel_generate($object, $field, $instance, $bundle) {
+  $object_field = array();
+  // Make sure the field settings are all set.
+  foreach (array('min', 'max', 'precision', 'scale') as $key) {
+    if (empty($field[$key])) {
+      $field[$key] = NULL;
+    }
+  }
+  $min = is_numeric($field['min']) ? $field['min'] : 0;
+  switch ($field['type']) {
+    case 'number_integer':
+      $max = is_numeric($field['max']) ? $field['max'] : 10000;
+      $decimal = 0;
+      $scale = 0;
+      break;
+
+    case 'number_decimal':
+      $precision = is_numeric($field['precision']) ? $field['precision'] : 10;
+      $scale = is_numeric($field['scale']) ? $field['scale'] : 2;
+      $max = is_numeric($field['max']) ? $field['max'] : pow(10, ($precision - $scale));
+      $decimal = rand(0, (10 * $scale)) / 100;
+      break;
+
+    case 'number_float':
+      $precision = rand(10, 32);
+      $scale = rand(0, 2);
+      $decimal = rand(0, (10 * $scale)) / 100;
+      $max = is_numeric($field['max']) ? $field['max'] : pow(10, ($precision - $scale));
+      break;
+  }
+  $object_field['value'] = round((rand($min, $max) + $decimal), $scale);
+  return $object_field;
+}
diff --git a/devel_generate/taxonomy.devel_generate.inc b/devel_generate/taxonomy.devel_generate.inc
new file mode 100644 (file)
index 0000000..eac8be8
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+//$Id$
+
+function taxonomy_devel_generate($object, $field, $instance, $bundle) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_taxonomy_devel_generate', $object, $field, $instance, $bundle);
+  }
+  else {
+    return _taxonomy_devel_generate($object, $field, $instance, $bundle);
+  }
+}
+
+function _taxonomy_devel_generate($object, $field, $instance, $bundle) {
+  $object_field = array();
+  // TODO: For free tagging vocabularies that do not already have terms, this
+  // will not result in any tags being added.
+  $allowed_values = taxonomy_allowed_values($field);
+  if (!empty($allowed_values)) {
+    // Pick one of the allowed values.
+    $object_field['tid'] = array_rand($allowed_values);
+  }
+  else {
+    $object_field['tid'] = 0;
+  }
+  return $object_field;
+}
diff --git a/devel_generate/text.devel_generate.inc b/devel_generate/text.devel_generate.inc
new file mode 100644 (file)
index 0000000..96ac296
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+// $Id$
+
+function text_devel_generate($object, $field, $instance, $bundle) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_text_devel_generate', $object, $field, $instance, $bundle);
+  }
+  else {
+    return _text_devel_generate($object, $field, $instance, $bundle);
+  }
+}
+
+function _text_devel_generate($object, $field, $instance, $bundle) {
+  $object_field = array();
+  if (!empty($instance['settings']['text_processing'])) {
+    $formats = filter_formats();
+    $format = array_rand($formats);
+  }
+  else {
+    $format = 0;
+  }
+  if ($instance['widget']['type'] != 'text_textfield') {
+    $object_field['value'] = devel_create_content($format);
+    if ($instance['widget']['type'] == 'text_textarea_with_summary' && !empty($instance['display_summary'])) {
+      $object_field['summary'] = devel_create_content($format);
+    }
+  }
+  else {
+    // Generate a value that respects max_length.
+    if (empty($field['max_length'])) {
+      $field['max_length'] = 12;
+    }
+    $object_field['value'] = user_password($field['max_length']);
+  }
+  $object_field['format'] = $format;
+  return $object_field;
+}
diff --git a/devel_generate/userreference.devel_generate.inc b/devel_generate/userreference.devel_generate.inc
new file mode 100644 (file)
index 0000000..b175f3c
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+function userreference_devel_generate($object, $field) {
+  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
+    return devel_generate_multiple('_userreference_devel_generate', $object, $field);
+  }
+  else {
+    return _userreference_devel_generate($object, $field);
+  }
+}
+
+function _userreference_devel_generate($object, $field) {
+  $object_field = array();
+  $allowed_values = userreference_allowed_values($field);
+  if (isset($allowed_values['none'])) {
+    unset($allowed_values['none']);
+  }
+  if (!empty($allowed_values)) {
+    // Just pick one of the specified allowed values.
+    $object_field['uid'] = array_rand($allowed_values);
+  }
+  return $object_field;
+}