It'll be great to get this field working with devel generate. Here's a first go at writing the hook that can just be added to the module file

/**
 * Implementation of hook content_generate().
 * Returns an array for the node field which is then saved to the CCK tables
 * Also adds terms to the taxonomy property of node so that they are added
 * to the taxonomy tables
 */
function content_taxonomy_content_generate( &$node, $field ) {
  $node_field = array();
  $sql = "SELECT tid FROM {term_data} WHERE vid = %d ORDER BY RAND() LIMIT 1";
  $term = db_fetch_object( db_query( $sql, $field[ 'vid' ] ) );
  $node_field[] = array( 'value' => $term->tid );
  if( $field['save_term_node'] ) {
    $node->taxonomy[] = $term->tid;
  }
  return $node_field;
}

Comments

JoeMcGuire’s picture

Quick update. Appears to require the key when building the taxonomy array

/**
 * Implementation of hook content_generate().
 * Returns an array for the node field which is then saved to the CCK tables
 * Also adds terms to the taxonomy property of node so that they are added
 * to the taxonomy tables
 */
function content_taxonomy_content_generate( &$node, $field ) {
  $node_field = array();
  $sql = "SELECT tid FROM {term_data} WHERE vid = %d ORDER BY RAND() LIMIT 1";
  $term = db_fetch_object( db_query( $sql, $field[ 'vid' ] ) );
  $node_field[] = array( 'value' => $term->tid );
  if( $field['save_term_node'] ) {
    $node->taxonomy[ $term->tid ] = $term->tid;
  }
  return $node_field;
}
JoeMcGuire’s picture

Status: Active » Needs review
StatusFileSize
new904 bytes

I've made a patch for snippet above. Tested and works would be good to get pushed through

floretan’s picture

The patch still applies cleanly. I tested it with a complex content type making use of various content_taxonomy fields and it seems to be working fine. The only thing missing is when using the hierarchical_select widget with the "save term lineage" or "force users to choose from the last level" options set. The attached patch adds support for these two options allowing to generate a consistent structure, which makes the hook implementation look like this:

/**
* Implementation of hook content_generate().
* Returns an array for the node field which is then saved to the CCK tables
* Also adds terms to the taxonomy property of node so that they are added
* to the taxonomy tables
*/
function content_taxonomy_content_generate( &$node, $field ) {
  $node_field = array();
  $sql = "SELECT tid FROM {term_data} WHERE vid = %d ORDER BY RAND() LIMIT 1";
  $term = db_fetch_object( db_query( $sql, $field[ 'vid' ] ) );
  $node_field[] = array( 'value' => $term->tid );

  if ($field['widget']['type'] == 'content_taxonomy_hs' && module_exists('hs_content_taxonomy')) {
    // Get the hierarchical_select config for this field.
    require_once(drupal_get_path('module', 'hierarchical_select') .'/includes/common.inc');
    $hs_config_id = "content-taxonomy-" . $field['field_name'];
    $hs_config = hierarchical_select_common_config_get($hs_config_id);

    if ($hs_config['save_lineage']) {
      // Add parent terms.
      $parents = taxonomy_get_parents_all($term->tid);
      foreach ($parents as $parent) {
        if ($parent->tid != $term->tid) {
          $node_field[] = array('value' => $parent->tid);
        }
      }
    }
    if ($hs_config['enforce_deepest']) {
      // Add children.
      $tid = $term->tid;
      while ($children = taxonomy_get_children($tid)) {
        $child = $children[array_rand($children)];
        if ($hs_config['save_lineage']) {
          $node_field[] = array('value' => $child->tid);
        }
        else {
          $node_field = array(
            array('value' => $child->tid),
          );
        }
        $tid = $child->tid;
      }
    }
  }

  if ($field['save_term_node']) {
    foreach ($node_field as $delta) {
      $node->taxonomy[$delta['value']] = $delta['value'];
    }
  }

  return $node_field;
}
floretan’s picture

Due to the weird way the CCK - Devel Generate integration works (see comment at top of content.devel.inc), the random fields are generated multiple times. This doesn't usually have side effects besides wasting CPU cycles, but when we have a content_taxonomy field that also saves values to the taxonomy table, we end up with way too many values in there (the values of the field itself are overridden each time). This updated patch fixes this problem by checking for the existence of field values.

aaronbauman’s picture

patch in #4 works great for me on a site with multiple content taxonomy fields, some shared between content types.

rhymeswithcamera’s picture

The patch in #4 worked great for me too with multiple, shared CT fields.

blackspiraldancer’s picture

The patch in #4 also worked for me. What about including it in the next dev build?

he0x410’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev
Assigned: Unassigned » he0x410
Status: Needs review » Patch (to be ported)

Ok.

blackspiraldancer’s picture

+1 works beautifully

hefox’s picture

Just an update so it'll add more than 1 value if it's a multi field