You are here

function _nat_add_terms in Node Auto Term [NAT] 7

Same name and namespace in other branches
  1. 5 nat.module \_nat_add_terms()
  2. 6.2 nat.module \_nat_add_terms()
  3. 6 nat.module \_nat_add_terms()
  4. 7.2 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_node_insert in ./nat.module
Implements hook_node_insert().
_nat_sync_associations in ./nat.admin.inc
Synchronize NAT node-term relationships. Create associated terms for node where missing.

File

./nat.module, line 549
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();
  $hierarchy = _nat_get_term_hierarchies($node);
  $vids = empty($vids) ? $nat_config['types'][$node->type] : $vids;
  $terms_saved = array();
  foreach ($vids as $vid) {
    $term = new StdClass();
    $term->weight = 0;
    $term->vid = $vid;

    // Save hierarchy for vocabularies also present in the node form. No parent
    // requires a 0.
    $term->parent = isset($hierarchy[$vid]) ? $hierarchy[$vid] : array(
      0,
    );
    $fields = $nat_config['associations'][$node->type][$term->vid];

    // $entities = entity_create_stub_entity('taxonomy_term', array(null, null, $term->vid));
    $entities = array();
    $terms_saved[] = _nat_save_term($node, $hierarchy, $term, $entities, $fields);
  }
  return $terms_saved;
}