Created
May 16, 2014 09:40
-
-
Save nickopris/bc98f86a800bca79cb35 to your computer and use it in GitHub Desktop.
Drupal 7 taxonomy vocabulary, terms and fields
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 | |
// Create new vocabulary | |
$vo = new stdClass(); | |
$vo->name = "AAA"; | |
$vo->machine_name = "aaa"; | |
taxonomy_vocabulary_save($vo); | |
// Create field in db | |
if(!db_table_exists('field_data_field_id_on_hub')) { | |
$field = array( | |
'field_name' => 'field_id_on_hub', | |
'type' => 'number_integer', | |
'label' => t('ID on hub') | |
); | |
field_create_field($field); | |
} | |
// Attach the field to our taxonomy entity | |
$instance = array( | |
'field_name' => 'field_id_on_hub', | |
'entity_type' => 'taxonomy_term', | |
'bundle' => $vo->machine_name, | |
'label' => t('ID on hub'), | |
'required' => true, | |
'widget' => array( | |
'active' => 0, | |
'module' => 'number', | |
'settings' => array(), | |
'type' => 'number', | |
'weight' => 16, | |
), | |
); | |
field_create_instance($instance); | |
// Create new taxonomy term | |
$term = new stdClass(); | |
$term->name = 'Your New Term Name'; | |
$term->vid = $vo->vid; | |
$term->field_id_on_hub[LANGUAGE_NONE][0]['value'] = 13; | |
taxonomy_term_save($term); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment