Hi everyone! Newbie here. I'd like to create a module that upon installation will create a vocabulary and populate it with terms. How do I do that? All I know is it has to be written in the .install file. I'm using Drupal 6.2. Thanks in advance!

Comments

pembeci’s picture

You have to put the required SQL queries in your .install file. To do that study the database. You can use phpmyadmin to export the tables prefixed with vocabulary and term (ignore term_node table). Filter the INSERT queries that are related to your vocabulary and terms and put them in your install file. Oh, don't forget to make your module dependant on taxonomy to make sure that it is enabled and the tables are already created in the database.

EDIT: I can't delete the comment. The above is incomplete advice if you already read it. You can insert the vocabulary and get an auto increment vid but there is no way you can be sure the vid assigned for the module's user will be the same as your vid for term related queries. You have to get the vid first like _forum_get_vid and modify your term queries with that vid (vocabulary id, the index used for denoting the vocabulary at database tables).

Another alternative may be using taxonomy_save_term and taxonomy_save_vocabulary by faking the form values but you have to be careful.

archard’s picture

The best way to do it would be to use the taxonomy_save_term and taxonomy_save_vocabulary functions like pembeci said. You can use hook_install() to fire them when the module is installed.

shroge’s picture

Have you found any examples on how to do this yet? I'm also interested in this as well.

randell’s picture

I didn't find any examples. I had to look up taxonomy_save_term and taxonomy_save_vocabulary. It's very tricky actually. I'm still trying to find better ways to do this. Or maybe it's not the perfect solution to what I want to achieve. I'm currently looking of changing my implementation and using Content Types instead.

rainer_f’s picture

You might look in the core forum code for an example cause that's what forum is doing.
Look in /modules/forum/forum.admin.inc and search for taxonomy_save_term.

There's your solution.

Cheers,
Rainer
--
Professional Drupal services for Germany
http://www.feike.de

kenuck’s picture

You can use the hook_enable() hook to add/remove vocab and terms.

function sb_event_enable(){

        $vocabulary = array(
            'name'  => t('Event Category'),
            'multiple'  => 0,
            'required'  => 1,
            'hierarchy' => 1,
            'relations' => 0,
            'nodes' => array('sb_event'=>1),
            'module'    => 'sb_event',
            'weight'        => -50,
        );
        taxonomy_save_vocabulary($vocabulary);
        variable_set('sb_event_category', $vocabulary['vid']);

        $terms = array('Party','Causes','Education','Meetings','Music/Arts','Sports','Trips','Other');
        $vid = $vocabulary['vid'];
        foreach($terms as $term){
            $edit = array('vid'=>$vid, 'name'=>$term);
            taxonomy_save_term($edit);
        };
        drupal_set_message('Taxonomy "Event Category" created.');
}

cheers
k
-----------------------------------------
http://www.netrift.com - Custom modules