You are here

function _nat_add_terms in Node Auto Term [NAT] 5

Same name and namespace in other branches
  1. 6.2 nat.module \_nat_add_terms()
  2. 6 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

$vids: An array of vocabulary IDs denoting the vocabularies into which a NAT term is to be added.

$title: Node title.

$body: Node body.

$hierarchy: The taxonomy array for the node in question. This is used to, if set, insert the new term as a child term of the selected parent.

Return value

$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.module
Synchronize NAT node-term relationships. Create associated terms for node where missing.

File

./nat.module, line 568
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($vids, $title, $body, $hierarchy = array(), $relations = array(), $synonyms = NULL) {
  $edit = array(
    'name' => $title,
    'description' => $body,
    'weight' => 0,
  );
  if (!empty($synonyms)) {
    $edit['synonyms'] = $synonyms;
  }
  $tids = array();
  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();
    }
    if (count($relations)) {
      $edit['relations'] = $relations[$vid];
    }
    taxonomy_save_term($edit);
    $tids[] = $edit;
  }
  return $tids;
}