Skip to content

Instantly share code, notes, and snippets.

@nickopris
Created May 16, 2014 09:40
Show Gist options
  • Save nickopris/bc98f86a800bca79cb35 to your computer and use it in GitHub Desktop.
Save nickopris/bc98f86a800bca79cb35 to your computer and use it in GitHub Desktop.
Drupal 7 taxonomy vocabulary, terms and fields
<?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