User login

Create vocabulary and taxonomy terms in an installation profile

The first part is provided by the default installation profile:

<?php
 
// Create a default vocabulary named "Tags".
 
$description = st('Use tags to group articles on similar topics into categories.');
 
$help = st('Enter a comma-separated list of words to describe your content.');
 
$vocabulary = (object) array(
   
'name' => st('Tags'),
   
'description' => $description,
   
'machine_name' => 'tags',
   
'help' => $help,
  );
 
taxonomy_vocabulary_save($vocabulary);
?>

The second part is simply repeated application of taxonomy_term_save().

Putting it all together, below, with a foreach loop to save typing for repeated terms.

This code can be put in or this function can be called from your installation profile or from an update hook (hook_update_N).

<?php
/**
 * Create and populate the Project's image types.
 */
function feature_media_image_categories() {
 
// We know the install profile has created an Administrator at uid 3.
 
$uid = variable_get('sdl_admin_uid', 3);

 

// Create a default vocabulary named "Image type".
 
$description = st('The type of image, such as Drawing, Rendering, or Photograph.');
 
$help = st('Select the type of image.');
 
$vocabulary = (object) array(
   
'name' => st('Image type'),
   
'description' => $description,
   
'machine_name' => 'image_types',
   
'help' => $help,
  );
 
taxonomy_vocabulary_save($vocabulary);

 

$names = array(
   
'Photograph',
   
'Rendering',
   
'Drawing',
  );

 

$weight = -15;
  foreach(
$names as $name) {   
   
$term = (object) array(
     
'name' => $name,
     
'vid' => $vocabulary->vid,
     
'weight' => $weight,
    );
   
taxonomy_term_save($term);     
   
$weight += 5;
   
$terms[$name] = $term->tid;
  }
}
?>
Searched words: 
taxonomy installation Drupal taxonomy update hook

Comments

Here's the same thing, making use of fieldhelp module

(Check out fieldhelp.)

<?php
/**
 * Implements hook_install().  Add orders to book content type.
 */
function libcat_order_install() {
 
$vocabulary = libcat_order_create_vocabulary_status();
 
libcat_order_populate_vocabulary_status($vocabulary);
 
libcat_order_attach_field_status($vocabulary);
}

/**
 * Create book status (Ordered, Present, Not sourcable, etc) vocabulary.
 */
function libcat_order_create_vocabulary_status() {
 
$vocabulary = (object) array(
   
'name' => st('Book status'),
   
'description' => st('Status of a book or potential book in a collection.'),
   
'machine_name' => 'libcat_status',
   
'help' => st('Select your location'),
  );
 
taxonomy_vocabulary_save($vocabulary);
  return
$vocabulary;
}

/**
 * Populate the book order status vocabulary with its terms.
 */
function libcat_order_populate_vocabulary_status($vocabulary) {
 
$t = get_t();
 
$names = array(
   
$t('Requested'),
   
$t('Ordered'),
   
$t('Not sourceable'),
   
$t('Present'),
  );

 

$weight = -50;
  foreach(
$names as $name) {
   
$term = (object) array(
     
'name' => $name,
     
'vid' => $vocabulary->vid,
     
'weight' => $weight,
    );
   
taxonomy_term_save($term);
   
$weight += 10;
  }
}

/**
 * Add book order status field to book node type.
 */
function libcat_order_attach_field_status($vocabulary) {
 
drupal_load('module', 'fieldhelp');
 
fieldhelp_taxonomy_single_select_add('node', LIBCAT_BOOK_NODE_TYPE, $vocabulary);
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.