function hs_taxonomy_form_alter in Hierarchical Select 6.3
Same name and namespace in other branches
- 5.3 modules/hs_taxonomy.module \hs_taxonomy_form_alter()
Implementation of hook_form_alter().
File
- modules/
hs_taxonomy.module, line 237 - Implementation of the Hierarchical Select API for the Taxonomy module.
Code
function hs_taxonomy_form_alter(&$form, $form_state, $form_id) {
// Change the term selection of nodes. Only affects hierarchical
// vocabularies.
if (isset($form['type']) && isset($form['#node']) && variable_get('taxonomy_override_selector', FALSE) && $form['type']['#value'] . '_node_form' == $form_id) {
$node = $form['#node'];
if (!isset($node->taxonomy)) {
$terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node);
}
else {
// After preview the terms must be converted to objects.
reset($node->taxonomy);
if (!is_object(current($node->taxonomy))) {
$node->taxonomy = taxonomy_preview_terms($node);
}
$terms = $node->taxonomy;
}
$c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);
while ($vocabulary = db_fetch_object($c)) {
if ($vocabulary->tags) {
if (isset($form_state['node_preview'])) {
// Typed string can be changed by the user before preview,
// so we just insert the tags directly as provided in the form.
$typed_string = $node->taxonomy['tags'][$vocabulary->vid];
}
else {
$typed_string = taxonomy_implode_tags($terms, $vocabulary->vid) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
}
if ($vocabulary->help) {
$help = filter_xss($vocabulary->help);
}
else {
$help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".');
}
$form['taxonomy']['tags'][$vocabulary->vid] = array(
'#type' => 'textfield',
'#title' => $vocabulary->name,
'#description' => $help,
'#required' => $vocabulary->required,
'#default_value' => $typed_string,
'#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
'#weight' => $vocabulary->weight,
'#maxlength' => 255,
);
if (module_exists('i18ntaxonomy')) {
$vid = $vocabulary->vid;
// Special treatment for tags, add some help texts
$type = i18ntaxonomy_vocabulary($vid);
if ($type == I18N_TAXONOMY_LOCALIZE || $type == I18N_TAXONOMY_TRANSLATE) {
$form['taxonomy']['tags'][$vid]['#title'] = i18nstrings_string("taxonomy:vocabulary:{$vid}:name", $form['taxonomy']['tags'][$vid]['#title']);
$form['taxonomy']['tags'][$vid]['#description'] = i18nstrings("taxonomy:vocabulary:{$vid}:help", $form['taxonomy']['tags'][$vid]['#description']);
}
if ($type == I18N_TAXONOMY_LOCALIZE) {
$form['taxonomy']['tags'][$vid]['#description'] .= ' ' . t('This is a localizable vocabulary, so only terms in %language are allowed here.', array(
'%language' => language_default('name'),
));
}
}
}
else {
// Just looking at $vocabulary->required is not sufficient: the forum
// module does not correctly set $vocabulary->required and hence the
// forum vocabulary does not have it set. It's made required through
// some hardcoded piece of Forms API logic. Therefore we have to do
// more extensive checks to decide to make the form item required.
$required = isset($form['taxonomy'][$vocabulary->vid]['#required']) ? $form['taxonomy'][$vocabulary->vid]['#required'] : $vocabulary->required;
// Extract terms belonging to the vocabulary in question.
$default_terms = array();
foreach ($terms as $term) {
// Free tagging has no default terms and also no vid after preview.
if (isset($term->vid) && $term->vid == $vocabulary->vid) {
$default_terms[$term->tid] = $term;
}
}
$form_function = variable_get("taxonomy_hierarchical_select_{$vocabulary->vid}", 0) ? 'hs_taxonomy_form' : 'taxonomy_form';
if ($form_function == 'hs_taxonomy_form' && module_exists('i18ntaxonomy')) {
$vid = $vocabulary->vid;
if (is_numeric($vid) && i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_LOCALIZE) {
// Rebuild this vocabulary's form.
$vocabulary = taxonomy_vocabulary_load($vid);
// Extract terms belonging to the vocabulary in question.
$default_terms = array();
foreach ($terms as $term) {
if ($term->vid == $vid) {
$default_terms[$term->tid] = $term;
}
}
$form['taxonomy'][$vid] = i18ntaxonomy_vocabulary_form($vocabulary->vid, array_keys($default_terms));
$form['taxonomy'][$vid]['#weight'] = $vocabulary->weight;
$form['taxonomy'][$vid]['#required'] = $vocabulary->required;
$form['taxonomy'][$vid]['#description'] = i18nstrings("taxonomy:vocabulary:{$vid}:help", $vocabulary->help);
}
elseif (is_numeric($vid) && i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_TRANSLATE) {
// Rebuild this vocabulary's form.
$vocabulary = taxonomy_vocabulary_load($vid);
$form['taxonomy'][$vid]['#title'] = i18nstrings_string("taxonomy:vocabulary:{$vid}:name", $vocabulary->name);
$form['taxonomy'][$vid]['#description'] = i18nstrings("taxonomy:vocabulary:{$vid}:help", $vocabulary->help);
}
}
$form['taxonomy'][$vocabulary->vid] = $form_function($vocabulary->vid, array_keys($default_terms), filter_xss($vocabulary->help));
$form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
$form['taxonomy'][$vocabulary->vid]['#required'] = $required;
}
}
if (!empty($form['taxonomy']) && is_array($form['taxonomy'])) {
if (count($form['taxonomy']) > 1) {
// Add fieldset only if form has more than 1 element.
$form['taxonomy'] += array(
'#type' => 'fieldset',
'#title' => t('Vocabularies'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
}
$form['taxonomy']['#weight'] = -3;
$form['taxonomy']['#tree'] = TRUE;
}
}
}