You are here

function _data_taxonomy_save_terms in Data 6

Helper function, saves a series of taxonomy terms for a record.

Creates new taxonomy terms on the fly for vocabularies that are tags.

Parameters

$table_name: Table name of the record.

$id: Record identifier.

$terms: An array of terms. Can be an array of tids, term names, term arrays or objects that can be casted into a term array. If the target vocabulary is a tag vocabulary, non-existing terms will be created on the fly.

$vocabulary: A vocuabulary object.

2 calls to _data_taxonomy_save_terms()
data_taxonomy_data_insert in data_taxonomy/data_taxonomy.module
Implementation of hook_data_insert().
data_taxonomy_data_update in data_taxonomy/data_taxonomy.module
Implementation of hook_data_update().

File

data_taxonomy/data_taxonomy.module, line 130
Hooks and API functions for Data Node module.

Code

function _data_taxonomy_save_terms($table_name, $id, $terms, $vocabulary) {
  if (!is_array($terms)) {
    $terms = array(
      $terms,
    );
  }
  $tids = array();
  foreach ($terms as $term) {
    if (is_string($term)) {
      $term = data_taxonomy_sanitize($term, $vocabulary->vid);
      $term = data_taxonomy_save_term_name($term, $vocabulary->vid, $vocabulary->tags);
    }
    else {
      if (is_object($term)) {
        $term = (array) $term;
      }
      if (is_array($term)) {
        $term['name'] = data_taxonomy_sanitize($term['name'], $vocabulary->vid);
        $term = data_taxonomy_save_term_array($term, $vocabulary->vid, $vocabulary->tags);
      }
    }
    if (is_array($term)) {
      $term = isset($term['tid']) ? $term['tid'] : FALSE;
    }
    if (is_numeric($term) && !isset($tids[$term])) {
      $tids[$term] = $term;
    }
  }
  _data_taxonomy_save_relations($vocabulary->vid, $id, $table_name, $tids);
}