Usernames as Vocabularies

As If@drupal.org's picture
public
group: Taxonomy
As If@drupal.org - Sat, 2008-01-05 19:58

One possible approach to taxonomy on a social networking/community site is to give each user their own Vocabulary. The following snippet does just that. It is a basic no-frills hack for creating a Vocabulary with the name of the current User (first it checks to make sure such a Vocab doesn't already exist). I left the "echo" lines in it so you can see what it's doing. Feel free to bang on it, bag on it, chop it up, use it in your modules, whatever.

<?php
$thevid
= 0;
global
$user;
$x = taxonomy_get_vocabularies();
echo
'<br><b>VOCABULARIES:</b>';
foreach(
$x AS $thing) {
echo
"<br><a href='/taxonomy/vocabulary/" . $thing->vid . "'>" . $thing->name . '</a>';
    if(
strtolower($thing->name) == strtolower($user->name)) {
       
$thevid = $thing->vid;
      echo
' <- this one matches your name, so we will load it';
       
$vocabobject = array(
         
'name' => $thing->name,
           
'description' => $thing->description,
         
'help' => $thing->help,
           
'nodes' => $thing->nodes,
         
'hierarchy' => $thing->hierarchy,
         
'relations' => $thing->relations,
         
'tags' => $thing->tags,
           
'multiple' => $thing->multiple,
           
'required' => $thing->required,
           
'weight' => $thing->weight,
           
'modules' => $thing->modules,
         
'vid' => $thing->vid,
      );
}
}
echo
'<hr>';
if(
$thevid) {
  echo
'<br>your vocab id is ' . $thevid . '.<br>';
  echo
'<br>the data is:<br>';
  foreach(
$vocabobject AS $key => $val) {
   echo
$key . ' => ' . $val . '<br>';
  }
} else {
 
$vocabobject = array(
     
'name' => $user->name,
    
'description' => 'The tagging vocabulary used by ' . $user->name,
       
'help' => 'Enter all tag terms used by ' . $user->name,
     
'nodes' => array('blog', 'story', 'page'),
     
'hierarchy' => 1,
    
'relations' => 0,
    
'tags' => 0,
     
'multiple' => 1,
     
'required' => 0,
     
'weight' => 0,
       
'modules' => 'taxonomy',
       
'vid' => ''
);
echo
'Vocabulary name \"' . $user->name . '\" is available<br>';
    echo
'Attempting to create vocabulary <b>' . $user->name . '</b><br>';
   
$y = taxonomy_save_vocabulary(&$vocabobject);
  echo
$y;
}
?>

NOTES:

  1. You will want to replace the applicable content types array('blog', 'story', 'page') with your own content types.

  2. You can set the Vocab options heirarchy relations tags multiple required weight any way you want.

  3. Obviously you can also change the help and decription lines.

  4. When you're happy with the way it works, comment out or remove the echo lines.

LVX
TF


Cool! Won't this also need

Benjamin Melançon's picture
Benjamin Melançon - Mon, 2008-01-07 10:14

Cool! Won't this also need a way to restrict who sees the vocabulary when editing nodes? Or should I be able to tag every node as 'Bobalicious' in Bob's vocabulary?

benjamin, Agaric Design Collective


Yes, If

As If@drupal.org's picture
As If@drupal.org - Mon, 2008-01-07 14:34

Yes, if you don't apply some sort of access control to taxonomy on your site, you'll have a setup where everyone can apply tags from other people's taxonomies. This might be useful in certain situations (a group of authors/editors who cooperatively manage content, a working group who can assign nodes to "departments" within the taxonomies of other members, or a social network in which the assignment of nodes to people or groups is performed on a subjective basis).

But yeah, most of the time you will want to apply some sort of access control on taxonomy, or else run around your site and theme-override/undisplay everything that exposes the unwanted vocabularies. ;-)

An alternative approach is to create one big Vocab for User-Generated Tags and consider Usernames as the first layer of Terms within that Vocab. That would have been easier (and easier on the categories table). But I wanted to give Users the ability to answer all the Vocab questions themselves: Is this Vocabulary Hierarchical, Does it allow Related Terms, Does it allow Freetagging, etc.

LVX
TF