Using a custom php to write nodes.
Taxonomy free-tagging associated to nodes.
in 6.x i was using taxonomy_node_save().
Now i have to use only node_save() and fill the items for taxonomy with the tid, In order to know this tid, I started to make my own script for 7.x and it is working.

function taxonomy_tid($tname, $voc, $vid) {
  $taxo = taxonomy_get_term_by_name($tname);
  if (!$taxo) {
    $taxo = new stdClass();
    $taxo->vocabulary_machine_name = $voc;
    $taxo->name = $tname; print_r($taxo);
    $taxo->vid = $vid;
    $status = taxonomy_term_save($taxo); 
    $taxo = taxonomy_get_term_by_name($tname);
  }

  return $taxo->tid;
}

but it doesn't look very clean isn't it?
Is there a better way?

Comments

Anonymous’s picture

I advise you to save the node properly. Everything is handled by Field Attach API now. This is by design.

egarias’s picture

Thanks bangpound
here is the solution i found, now it works, but my impresion is that is not very clean and surely it is another way to do it, if someone wants to share, here is my code.

  $nodecreated = strtotime($fecha);
  $node = new stdClass();
  $node->is_new = TRUE; 
  $node->type = 'boletin'; 
  node_object_prepare($node); 
  $node->uid = 1;  
  $node->name = 'articgroup';
  $node->created = $nodecreated; 
  $node->date = $nodecreated;
  $node->timestamp = $nodecreated;
  $node->changed = $nodecreated;
  $node->promote = $nodepromoted;
  $node->sticky = $nodesticky;
  $node->language = 'und';
  $node->format = '1';
  $node->pathauto_perform_alias = 1;
  $node->title = $titulo; 
  $node->body[$node->language][0]['value']   = $texto;
  $node->body[$node->language][0]['summary'] = text_summary($texto);
  $node->body[$node->language][0]['format']  = 'filtered_html';
  $node->field_numero[$node->language][0]['value'] = $Num; 
  $node->field_documento[$node->language][0]['url'] =  $boelink; 

  $valor = taxonomy_tid('BOE',7);
  if ($valor) $node->field_tipo[$node->language][0]['tid'] = $valor;
  $valor = taxonomy_tid($capitulo,4);
  if ($valor) $node->field_capitulo[$node->language][0]['tid'] = $valor;
  $valor = taxonomy_tid($organo,5);
  if ($valor) $node->field_organo[$node->language][0]['tid'] = $valor;
  $valor = taxonomy_tid($asunto,3);
  if ($valor) $node->field_asunto[$node->language][0]['tid'] = $valor;
  $valor = taxonomy_tid('MADRID', 2);
  if ($valor) $node->field_provincia[$node->language][0]['tid'] = $valor; 

  node_validate($node); 
  $node = node_submit($node); 
  node_save($node);    
}
function taxonomy_tid($tname, $vid) {
  $tid = false;
  $taxo = new stdClass();
  $taxo = taxonomy_get_term_by_name($tname);
  if (!$taxo) {
    $taxo = new stdClass();
    $taxo->name = $tname;
    $taxo->vid = $vid; print_r($taxo);
    $status = taxonomy_term_save($taxo); 
    $taxo = taxonomy_get_term_by_name($tname);
  }

  foreach($taxo as $ind=>$valor) {
    if ($valor->vid = $vid) $tid = $valor->tid;
  }
  return $tid;
}
jereoms’s picture

I was looking to do this very same thing and I actually came up with almost the same exact function during development. Would love to know if there is a script provided by drupal core that alleviates the need for the taxonomy_tid function above?

drzraf’s picture

Status: Closed (duplicate) » Active

I can't see which one of the above bugs deals with the missing taxonomy_node_save().
And this is an annoying issue. It's really (really) boring to bind terms to the node in the X possibilities
before finding the _right_data_structure_Drupal_expects.
That's the goal of the API to make data structure simpler to use.
here is a hint: http://yoodey.com/how-create-taxonomy-node-programmatically-drupal-7

Can we know why this helper has been removed ?

drzraf’s picture

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.