You are here

function _nat_add_terms in Node Auto Term [NAT] 6

Same name and namespace in other branches
  1. 5 nat.module \_nat_add_terms()
  2. 6.2 nat.module \_nat_add_terms()
  3. 7.2 nat.module \_nat_add_terms()
  4. 7 nat.module \_nat_add_terms()

Add node titles as terms into the taxonomy system. @todo Ideas are welcome to allow retaining the hierarchy for vocabularies not present in the node form.

Parameters

Object $node: The node object to associate and update.

Array $vids: An array of vocabulary IDs to restrict associations to; useful for operations such as NAT sync ...

Return value

Array $tids An array of term objects.

2 calls to _nat_add_terms()
nat_nodeapi in ./nat.module
Implementation of hook_nodeapi().
_nat_sync_associations in ./nat.admin.inc
Synchronize NAT node-term relationships. Create associated terms for node where missing.

File

./nat.module, line 488
NAT - node auto term - is a helper module that automatically creates a term using the same title as a node.

Code

function _nat_add_terms($node, $vids = array()) {
  $nat_config = _nat_variable_get();
  $edit = array(
    'name' => $node->title,
    'description' => isset($nat_config['body'][$node->type]) ? $node->body : '',
    'weight' => 0,
  );
  $tids = array();
  $hierarchy = isset($node->taxonomy) ? $node->taxonomy : array();
  $vids = empty($vids) ? $nat_config['types'][$node->type] : $vids;
  foreach ($vids as $vid) {

    // $edit is passed by reference and 'tid' is set with the tid of the new
    // term.
    unset($edit['tid']);
    $edit['vid'] = $vid;

    // Save hierarchy for vocabularies also present in the node form.
    if (isset($hierarchy[$vid])) {
      $edit['parent'] = $hierarchy[$vid];
    }
    else {
      $edit['parent'] = array();
    }
    $edit['relations'] = isset($node->nat) && isset($node->nat['related'][$vid]) ? $node->nat['related'][$vid] : array();
    $edit['synonyms'] = isset($node->nat) ? $node->nat['synonyms'] : '';
    taxonomy_save_term($edit);
    $tids[] = $edit;
  }
  return $tids;
}