entityreference.devel_generate.inc 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * @file
  4. * Support for processing entity reference fields in devel generate.
  5. */
  6. function entityreference_devel_generate($object, $field, $instance, $bundle) {
  7. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  8. return devel_generate_multiple('_entityreference_devel_generate', $object, $field, $instance, $bundle);
  9. }
  10. else {
  11. return _entityreference_devel_generate($object, $field, $instance, $bundle);
  12. }
  13. }
  14. function _entityreference_devel_generate($object, $field, $instance, $bundle) {
  15. $object_field = array();
  16. // Get all the entity that are referencable here.
  17. $referencable_entity = entityreference_get_selection_handler($field, $instance)->getReferencableEntities();
  18. if (is_array($referencable_entity) && !empty($referencable_entity)) {
  19. // $referencable_entity is keyed by bundle type.
  20. $random_bundle = array_rand($referencable_entity);
  21. if (!empty($random_bundle)) {
  22. $target_id = array_rand($referencable_entity[$random_bundle]);
  23. if (!empty($referencable_entity[$random_bundle][$target_id])) {
  24. $object_field['target_id'] = $target_id;
  25. }
  26. }
  27. }
  28. return $object_field;
  29. }