I have the following code in my page.tpl.php:

<?php 
$flag = flag_get_flag('flag1');
if ($flag && $flag->is_flagged($node->nid) && ($flag->name == 'flag1')) {
  print "this is flagged with flag1";
}

$flag = flag_get_flag('flag2');
if ($flag && $flag->is_flagged($node->nid) && ($flag->name == 'flag2')) {
  print "this is flagged with flag2";
}
?>

this works, but it seems terribly inefficient. What I would like to do is call up the array that was generated by the flag module itself once, instead of constantly calling individual flags. I am just not sure how to go about this. I tried looking at the module code, and template override code, but it just seems too much for what I want to do. I really just want to call each flag and then print text if the flag "is flagged" and it's names matches one of the flag names I created.

Comments

gbrands’s picture

You could generate the $flags array using a custom query to rather than hard-coding it in. I'm not too familiar with the flags module to know what table and field to query.

$flags = array('flag1','flag2','flag3');
foreach($flags as $flag){
    $check = flag_get_flag($flag);
    if($check && $check->is_flagged($node->nid){
        print 'This is flagged with '.$flag;
    }
}
solidad’s picture

hmm it gets a syntax error. I don't know if it matters which tables are queried but it may. It does look exactly what I wanted to do though.

gbrands’s picture

Sorry. I didn't have enough parenthesis:

$flags = array('flag1','flag2','flag3');
foreach($flags as $flag){
    $check = flag_get_flag($flag);
    if($check && $check->is_flagged($node->nid)){
        print 'This is flagged with '.$flag;
    }
}

If($check.... line

solidad’s picture

That works perfectly! I didn't see the bracket either hence my confusion