summaryrefslogtreecommitdiffstats
path: root/devel_generate/devel_generate.fields.inc
blob: 147fe9f1357e7cd224fd848be531cb2a51b0377d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

/**
 * @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);
  $skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields'];
  foreach (explode(',', $skips) as $skip) {
    unset($instances[$skip]);
  }
  foreach ($instances as $instance) {
    $field_name = $instance['field_name'];
    $field = field_info_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', 'comment', 'list'))) {
        // 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)) {
        if ($result = $function($object, $field, $instance, $bundle)) {
          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: 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(
      $object->language => $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++) {
      $result = $function($object, $field, $instance, $bundle);
      if (!empty($result)) {
        $object_field[$i] = $result;
      }
    }
  }
  return $object_field;
}