function blogapi_validate_terms in Blog API 7.2
Helper function. Find allowed taxonomy terms for a node type.
File
- ./
blogapi.module, line 337 - Enable users to post using applications that support BlogAPIs.
Code
function blogapi_validate_terms($node) {
// We do a lot of heavy lifting here since taxonomy module doesn't have a
// stand-alone validation function.
if (module_exists('taxonomy')) {
$found_terms = array();
if (!empty($node->taxonomy)) {
$term_list = array_unique($node->taxonomy);
$terms = taxonomy_term_load_multiple($term_list, array(
'type' => $node->type,
));
$found_terms = array();
$found_count = 0;
foreach ($terms as $term) {
$found_terms[$term->vid][$term->tid] = $term->tid;
$found_count++;
}
// If the counts don't match, some terms are invalid or not accessible to
// this user.
if (count($term_list) != $found_count) {
$error_data = array(
'message' => t('Invalid categories were submitted.'),
'error_code' => 405,
);
return $error_data;
}
}
// Look up all the vocabularies for this node type.
$vocabularies = taxonomy_vocabulary_load_multiple(array(), array(
'type' => $node->type,
));
// Check each vocabulary associated with this node type.
foreach ($vocabularies as $vocabulary) {
// Required vocabularies must have at least one term.
if ($vocabulary->required && empty($found_terms[$vocabulary->vid])) {
$error_data = array(
'message' => t('A category from the @vocabulary_name vocabulary is required.', array(
'@vocabulary_name' => $vocabulary->name,
)),
'error_code' => 403,
);
return $error_data;
}
// Vocabularies that don't allow multiple terms may have at most one.
if (!$vocabulary->multiple && (isset($found_terms[$vocabulary->vid]) && count($found_terms[$vocabulary->vid]) > 1)) {
$error_data = array(
'message' => t('You may only choose one category from the @vocabulary_name vocabulary.', array(
'@vocabulary_name' => $vocabulary->name,
)),
'error_code' => 403,
);
return $error_data;
}
}
}
elseif (!empty($node->taxonomy)) {
$error_data = array(
'message' => t('Error saving categories. This feature is not available.'),
'error_code' => 405,
);
return $error_data;
}
return TRUE;
}