5 * Functions needed for devel_generate Fields API integration.
9 * Enrich the $object that is about to be saved with arbitrary
10 * information in each of its fields.
12 function devel_generate_fields(&$object, $obj_type, $bundle) {
13 $field_types = field_info_field_types();
14 $instances = field_info_instances($obj_type, $bundle);
15 $skips = function_exists('drush_get_option') ?
drush_get_option('skip-fields', '') : @
$_REQUEST['skip-fields'];
16 foreach (explode(',', $skips) as
$skip) {
17 unset($instances[$skip]);
19 foreach ($instances as
$instance) {
20 $field_name = $instance['field_name'];
21 $field = field_info_field($field_name);
23 $object_field = array();
24 // If module handles own multiples, then only call its hook once.
25 if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM
) {
29 switch ($field['cardinality']) {
30 case FIELD_CARDINALITY_UNLIMITED
:
31 $max = rand(0, 3); //just an arbitrary number for 'unlimited'
34 $max = $field['cardinality'] - 1;
38 for ($i = 0; $i <= $max; $i++) {
39 $module = $field_types[$field['type']]['module'];
41 // Include any support file that might exist for this field.
42 if (in_array($module, array('file', 'image', 'taxonomy', 'number', 'text', 'comment', 'list'))) {
43 // devel_generate implements on behalf of core and special friends.
44 module_load_include('inc', 'devel_generate', "$module.devel_generate");
47 module_load_include('inc', $module, "$module.devel_generate");
49 $function = $module .
'_devel_generate';
50 if (function_exists($function)) {
51 if ($result = $function($object, $field, $instance, $bundle)) {
52 if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM
) {
53 // Fields that handle their own multiples will add their own deltas.
54 $object_field = $result;
57 // When multiples are handled by the content module, add a delta for each result.
58 $object_field[$i] = $result;
63 // TODO: Completely overriding any existing $object->{$field['field_name']}
64 // is necessary here because the forum module has a bug where it
65 // initializes the property with incorrect data.
66 // @see http://drupal.org/node/652176
67 $object->{$field['field_name']} = array(
68 $object->language
=> $object_field,
74 * A simple function to return multiple values for fields that use
75 * custom multiple value widgets but don't need any other special multiple
76 * values handling. This will call the field generation function
77 * a random number of times and compile the results into a node array.
79 function devel_generate_multiple($function, $object, $field, $instance, $bundle) {
80 $object_field = array();
81 if (function_exists($function)) {
82 switch ($field['cardinality']) {
83 case FIELD_CARDINALITY_UNLIMITED
:
84 $max = rand(0, 3); //just an arbitrary number for 'unlimited'
87 $max = $field['cardinality'] - 1;
90 for ($i = 0; $i <= $max; $i++) {
91 $result = $function($object, $field, $instance, $bundle);
92 if (!empty($result)) {
93 $object_field[$i] = $result;