Allow users to free tag content
- Posted By:admin
- Category:Categories, Forms
- Type:PHP
- Views:5
The snippet creates a textfield for the users to enter freetags and a submit button.
Set up a freetagging vocabulary using taxonomy.module and assign it to your content type as usual. Note the vid (vocabulary id) of your freetagging vocabulary (you can find it in the URL when editing the vocabulary). In a custom module, create a new form like so:
Create a 'Free Tagging' vocabulary using the taxonomy.module and assign it to your content type. Remember to add your vocabulary ID (the number in the URL when editing your vocabulary).
Snippet:
Login/Register to Comment this Snippet<?php
#in template.php:
#freetagging form for nodes
function ds_tag_form($nid = 0) {
$form = array();
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $nid,
);
$form['tag'] = array(
'#type' => 'textfield',
'#title' => 'add tag',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('+'),
);
return $form;
}
function ds_tag_form_submit($form_id, $form_values) {
$nid = $form_values['nid'];
$node = node_load($nid);
$terms = array();
foreach($node->taxonomy as $tax) $tags .= $tax->name .', ';
$tags .= $form_values['tag'];
$terms['tags'] = array(****NUM**** => $tags);#replace ****NUM**** with the vid of your vocabulary
taxonomy_node_save($nid, $terms);
drupal_set_message(t('Your tag has been added.'));
}
?>
in node.tpl:
<?php print drupal_get_form('ds_tag_form', $node->nid); ?>
(Note: unless you're in the penile enhancement business you'll want to wrap that in a conditional access statement.)