Last active
August 29, 2015 14:22
-
-
Save halfzebra/e441f9f4d5f44376a334 to your computer and use it in GitHub Desktop.
Drupal 7: Dynamic term generation for exsisting parent
This file contains 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
$vocab_machine_name = 'name'; | |
$vacab = taxonomy_vocabulary_machine_name_load($vocab_machine_name); | |
if (isset($vacab->vid)) { | |
$vid = $vacab->vid; | |
$terms = taxonomy_get_term_by_name('parent', $vocab_machine_name); | |
$parent_term = array_pop($terms); | |
// Create terms what needed. | |
$term_names = array( | |
'new term', | |
); | |
if (empty($terms) && isset($parent_term->tid)) { | |
$parent_tid = $parent_term->tid; | |
foreach ($term_names as $term_name) { | |
$new_term = taxonomy_get_term_by_name($term_name); | |
// Check if the term already exsists. | |
if (empty($new_term)) { | |
$new_term = new stdClass(); | |
$new_term->name = $term_name; | |
$new_term->vid = $vid; | |
$new_term->parent = $parent_tid; | |
taxonomy_term_save($new_term); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment