function farm_term in farmOS 7
Create a new taxonomy term or load an existing term by name.
Parameters
string $name: The name of the term.
string $vocabulary: The name of the vocabulary.
bool $create: Boolean: If TRUE, a new term will be created if one does not exist.
bool $load: Boolean: If TRUE, attempt to load existing terms first.
Return value
object|bool Returns a taxonomy term object, or FALSE if none were loaded/created.
14 calls to farm_term()
- farm_api_restws_request_alter in modules/
farm/ farm_api/ farm_api.module - Implements hook_restws_request_alter().
- farm_area_generate_form_submit in modules/
farm/ farm_area/ farm_area_generate/ farm_area_generate.module - Farm area generate form submit.
- farm_area_generate_form_validate in modules/
farm/ farm_area/ farm_area_generate/ farm_area_generate.module - Area generator form validate.
- farm_area_import_form_create_submit in modules/
farm/ farm_area/ farm_area_import/ farm_area_import.module - Area importer form submit.
- farm_crop_planting_quick_form_submit in modules/
farm/ farm_crop/ farm_crop.farm_quick.planting.inc - Planting quick form submit.
1 string reference to 'farm_term'
- farm_update_7044 in ./
farm.install - Enable Farm Term module.
File
- modules/
farm/ farm_term/ farm_term.module, line 22 - Code for the Farm Term module.
Code
function farm_term($name, $vocabulary, $create = TRUE, $load = TRUE) {
// If $load is TRUE, attempt to look up existing terms.
$terms = array();
if ($load) {
$terms = taxonomy_get_term_by_name($name, $vocabulary);
}
// If terms were found, return the first one.
if (!empty($terms)) {
$term = reset($terms);
return $term;
}
elseif ($create == TRUE) {
// Attempt to load the vocabulary by machine name. If it can't be loaded,
// bail.
$vocab = taxonomy_vocabulary_machine_name_load($vocabulary);
if (empty($vocab)) {
return FALSE;
}
// Create and save the term.
$term = new stdClass();
$term->name = check_plain($name);
$term->vid = $vocab->vid;
taxonomy_term_save($term);
return $term;
}
else {
return FALSE;
}
}