You are here

function _entityreference_devel_generate in Entity reference 7

1 call to _entityreference_devel_generate()
entityreference_devel_generate in ./entityreference.devel_generate.inc
@file Support for processing entity reference fields in devel generate.
1 string reference to '_entityreference_devel_generate'
entityreference_devel_generate in ./entityreference.devel_generate.inc
@file Support for processing entity reference fields in devel generate.

File

./entityreference.devel_generate.inc, line 17
Support for processing entity reference fields in devel generate.

Code

function _entityreference_devel_generate($object, $field, $instance, $bundle) {
  $object_field = array();

  // Get all the entity that are referencable here.
  $referencable_entity = entityreference_get_selection_handler($field, $instance)
    ->getReferencableEntities();
  if (is_array($referencable_entity) && !empty($referencable_entity)) {

    // $referencable_entity is keyed by bundle type.
    $random_bundle = array_rand($referencable_entity);
    if (!empty($random_bundle)) {
      $target_id = array_rand($referencable_entity[$random_bundle]);
      if (!empty($referencable_entity[$random_bundle][$target_id])) {
        $object_field['target_id'] = $target_id;
      }
    }
  }
  return $object_field;
}