How do you programmatically create a flag in drupal 6? I wish to capture this in code so that I can stage flags between different environments. The current stable release of flag does not support features.
2 Answers
Check out the flag API. There are examples for creatig flags, flagging a node and more. Here the example for the flag-creation:
$flag = flag_flag::factory_by_content_type('node');
// Absolutely required, will break your site if not added properties.
$flag->name = 'my_flag_name';
$flag->title = 'My Title';
// Properties required by the UI.
$flag->roles = array(2); // An array of role IDs. 2 is for "authenticated users".
$flag->types = array('story', 'page'); // An array of node types.
$flag->flag_short = 'Flag this';
$flag->unflag_short = 'Unflag this';
// Optional properties, defaults are defined for these (and more).
// Use a print_r() or dsm() to see all the available flag properties.
$flag->global = TRUE;
$flag->flag_long = '';
$flag->flag_message = '';
$flag->show_on_form = TRUE;
$flag->show_on_node = TRUE;
$flag->show_on_teaser = TRUE;
$flag->link_type = 'toggle'; // For JS link. Other options: 'normal' and 'confirm'.
// Save the flag.
$flag->save();
Nonsenz's answer is great, the only thing missing is that the roles are set differently than that.
You set a role for each action - flag and unflag.
$flag->roles = array(
'flag' => array(3),
'unflag' => array(3),
); // sets up for administrator role only, corresponds to drupal role id in roles table, by the way