This is a fantastic module and has a lot of potential to be really powerful! I have already been playing around with it to see what it can do and I like it.

There is one thing I would like, and also think might be beneficial if in the scope of the module. I would like to save the created terms (and terms if they already exist) to nodes. This might be pretty easy if taxonomy_builder_save_terms($vid, $hierarchy) returned an array of term IDs. From what I have gotten to work, only the term ID of the last term in the array of terms is returned. Would it be possible to return an array of tids? Or better yet have a function that would automatically save the tids with a specific nid??

Comments

cpliakas’s picture

Hi mkinnan.

I really like the idea of returning an array of IDs instead of just the last one added, and I think it is a feature missing from the API. In terms of saving nodes, you can actually probably already do this in hook_taxonomy_builder_term_save(). This will give you access to the newly created tid and the given level it was added to. However, this hook is only invoked when a new term is added to the hierarchy, which may or may not be what you are looking for. Hope this helps, and I will think more about the array of ID.

Thanks a lot for posting,
Chris

mkinnan’s picture

Thanks Chris for thinking about this option. I know this is crude, but this is the hack I did to get the term IDs of the array.

<?php
// Replace with whatever vocabulary ID the terms should be added to.
$vid = 14;

// Node ID to save array of terms to
$node_nid = 123;

// Hierarchy array
// In order for this to work, there can only be 1 array of terms
// All the terms in the array will be saved to the node ID specified above
$hierarchies = array(
  array('United states', 'Massachusetts', 'Boston'),
);

// Adds terms to the taxonomy hierarchy one by one.  This makes it easy to
// break up large trees via the batch API so the build doesn't time out.
// In this example, the term ID for 'Boston' will be returned because it is the deepest term
foreach ($hierarchies as $hierarchy) {
  $tax_builder_term_id = taxonomy_builder_save_terms($vid, $hierarchy);
}

// Get all parents of the term ID and receive an array of stdClass objects
// In this example, all the parent objects of 'Boston' will be returned
$term_parents = taxonomy_get_parents_all($tax_builder_term_id);

// Check relation of term IDS to nid and save if it does not exist
foreach($term_parents as $key => $value){
  $sql = "SELECT nid FROM term_node WHERE nid = %d AND tid = %d";
  $result = db_result(db_query($sql, $node_nid, $value->tid));
  if ($result == NULL){
    db_query("INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)", $node_nid, $node_nid, $value->tid);
  }
}

?>
mkinnan’s picture

Additionally, I want to be able to return the term ID whether the term exists or not so that I can save it to the node. Since hook_taxonomy_builder_term_save() is only called on new terms, it won't work in my case.

cpliakas’s picture

Status: Active » Closed (won't fix)

Closed due to end of life announcement.