#1045020 by Les Lim: Changed Remove obsolete nodereference and userreference module...
[project/devel.git] / devel_generate / devel_generate.fields.inc
CommitLineData
ccf65fd1 1<?php
ccf65fd1 2
3/**
4 * @file
5 * Functions needed for devel_generate Fields API integration.
6 */
7
8/**
9 * Enrich the $object that is about to be saved with arbitrary
10 * information in each of its fields.
11 **/
12function devel_generate_fields(&$object, $obj_type, $bundle) {
13 $field_types = field_info_field_types();
14 $instances = field_info_instances($obj_type, $bundle);
3cc58ac5 15 $skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields'];
b12afe48 16 foreach (explode(',', $skips) as $skip) {
270d79a3 17 unset($instances[$skip]);
18 }
ccf65fd1 19 foreach ($instances as $instance) {
20 $field_name = $instance['field_name'];
fa78d9b5 21 $field = field_info_field($field_name);
ccf65fd1 22
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) {
26 $max = 0;
27 }
28 else {
29 switch ($field['cardinality']) {
30 case FIELD_CARDINALITY_UNLIMITED:
31 $max = rand(0, 3); //just an arbitrary number for 'unlimited'
32 break;
33 default:
34 $max = $field['cardinality'] - 1;
35 break;
36 }
37 }
fa78d9b5 38 for ($i = 0; $i <= $max; $i++) {
ccf65fd1 39 $module = $field_types[$field['type']]['module'];
40
41 // Include any support file that might exist for this field.
eeb821c4 42 if (in_array($module, array('file', 'image', 'taxonomy', 'number', 'text', 'comment', 'list'))) {
ccf65fd1 43 // devel_generate implements on behalf of core and special friends.
44 module_load_include('inc', 'devel_generate', "$module.devel_generate");
45 }
46 else {
47 module_load_include('inc', $module, "$module.devel_generate");
48 }
49 $function = $module . '_devel_generate';
50 if (function_exists($function)) {
a36e1bc5 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;
55 }
56 else {
57 // When multiples are handled by the content module, add a delta for each result.
58 $object_field[$i] = $result;
59 }
ccf65fd1 60 }
61 }
62 }
ccf65fd1 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(
6fbb08bd 68 $object->language => $object_field,
ccf65fd1 69 );
70 }
71}
72
73/**
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.
78 */
79function 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'
85 break;
86 default:
87 $max = $field['cardinality'] - 1;
88 break;
89 }
90 for ($i = 0; $i <= $max; $i++) {
9a712ed0 91 $result = $function($object, $field, $instance, $bundle);
92 if (!empty($result)) {
93 $object_field[$i] = $result;
94 }
ccf65fd1 95 }
96 }
97 return $object_field;
270d79a3 98}