taxonomy.devel_generate.inc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. function taxonomy_devel_generate($object, $field, $instance, $bundle) {
  3. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  4. return devel_generate_multiple('_taxonomy_devel_generate', $object, $field, $instance, $bundle);
  5. }
  6. else {
  7. return _taxonomy_devel_generate($object, $field, $instance, $bundle);
  8. }
  9. }
  10. function _taxonomy_devel_generate($object, $field, $instance, $bundle) {
  11. $object_field = array();
  12. // TODO: For free tagging vocabularies that do not already have terms, this
  13. // will not result in any tags being added.
  14. $machine_name = $field['settings']['allowed_values'][0]['vocabulary'];
  15. $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
  16. if ($max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vocabulary->vid))->fetchField()) {
  17. $candidate = mt_rand(1, $max);
  18. $query = db_select('taxonomy_term_data', 't');
  19. $tid = $query
  20. ->fields('t', array('tid'))
  21. ->condition('t.vid', $vocabulary->vid, '=')
  22. ->condition('t.tid', $candidate, '>=')
  23. ->range(0,1)
  24. ->execute()
  25. ->fetchField();
  26. // If there are no terms for the taxonomy, the query will fail, in which
  27. // case we return NULL.
  28. if ($tid === FALSE) {
  29. return NULL;
  30. }
  31. $object_field['tid'] = (int) $tid;
  32. return $object_field;
  33. }
  34. }