Skip to content

Instantly share code, notes, and snippets.

@augustyip
Created September 16, 2014 08:34
Show Gist options
  • Save augustyip/6f48ae39ded3501bf771 to your computer and use it in GitHub Desktop.
Save augustyip/6f48ae39ded3501bf771 to your computer and use it in GitHub Desktop.
drupal: create the custom term if not exist.
<?php
function get_term_tid($term_name, $vocabulary){
$terms = taxonomy_get_term_by_name($term_name, $vocabulary->machine_name);
if (empty($terms)) {
$term = create_term($term_name, $vocabulary);
return $term->tid;
} else {
return reset($terms)->tid;
}
}
function create_term($term_name, $vocabulary){
$term = new stdClass();
$term->vid = $vocabulary->vid;
$term->name = trim($term_name);
taxonomy_term_save($term);
return $term;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment