devel_generate.fields.inc 3.4 KB

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