Created
September 16, 2014 08:34
-
-
Save augustyip/6f48ae39ded3501bf771 to your computer and use it in GitHub Desktop.
drupal: create the custom term if not exist.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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