function farm_log_categories_create in farmOS 7
Helper function for creating log categories. Terms will only be added if they don't already exist.
Parameters
array $categories: An array of strings that will be added as terms to the Farm Log Categories vocabulary.
2 calls to farm_log_categories_create()
- farm_log_categories_create_all in modules/
farm/ farm_log/ farm_log.module - Create log categories on behalf of all modules that provide them.
- farm_soil_update_7002 in modules/
farm/ farm_soil/ farm_soil.install - Add the "Tillage" log category.
File
- modules/
farm/ farm_log/ farm_log.module, line 344 - Code for the Farm Log feature.
Code
function farm_log_categories_create($categories) {
// If the categories is not an array, bail.
if (!is_array($categories)) {
return;
}
// Define the vocabulary machine name.
$vocabulary_machine_name = 'farm_log_categories';
// Load the vocabulary.
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
// If the vocabulary doesn't exist, bail.
if (empty($vocabulary->vid)) {
return;
}
// Iterate through the categories.
foreach ($categories as $category) {
// Translate the category name.
$term_name = t($category);
// Create new category term, if one doesn't already exist.
farm_term($term_name, $vocabulary_machine_name);
}
// Always reset the categories to alphabetical order.
/**
* @see taxonomy_vocabulary_confirm_reset_alphabetical_submit()
*/
db_update('taxonomy_term_data')
->fields(array(
'weight' => 0,
))
->condition('vid', $vocabulary->vid)
->execute();
}