How to print a flag in a block programmatically? I am creating a module which displays a block. I have created a flag within the module, and I am trying to display it through the block_view() hook. Currently struggling to get there...

I am doing this instead of going through the template pages because there are certain conditions and functions involved to decide if I show the flag or not, making it easier for me at the minute to do this in a block.

Unfortunately, the block isn't working. Am I not displaying it correctly?

function get_involved_block_info() {
$blocks = array();
$blocks['get_involved_block'] = array(
'info' =&gt; t('Get involved block'), 
'weight' =&gt; 10,   
);
return $blocks;
}


function get_involved_flag_default_flags() {
$flags = array();
$flags[] = array(
'content_type' =&gt; 'node',
'name' =&gt; 'get_involved_interested',
'title' =&gt; 'Interested',
'global' =&gt; FALSE,
'types' =&gt; array('ad'),
'flag_short' =&gt; 'Join',
'flag_long' =&gt; 'I want to join',
'flag_message' =&gt; 'Your interest was registered',
'unflag_short' =&gt; 'Cancel',
'unflag_long' =&gt; 'Cancel my interest',
'unflag_message' =&gt; 'Your interest was deleted',
'show_on_page' =&gt; TRUE,
'show_on_teaser' =&gt; FALSE,
'show_on_form' =&gt; TRUE,
'status' =&gt; TRUE,
'locked' =&gt; array('name', 'types', 'global'),
);
return $flags;
}


function get_involved_block_view($delta) {
global $user;
$block = array();
if (arg(0) == 'node' &amp;&amp; is_numeric(arg(1))) $nodeid = arg(1);
$node = node_load($nodeid);


//First of all, if the user is the author, do not show this node
if ($node-&gt;uid == $user-&gt;uid)
{
} else {

$block['title'] ='Get involved';

$block['content'] = flag_create_link('get_involved_interested', $nodeid);

  return $block;
}
}
You are adding raw PHP code as block content; you should execute it directly instead.

Try this:

$block['content'] .= flag_create_link('mymodule_interested', $node-&gt;nid);




UPDATE

You were using Flag API v1, instead of v2. Below is the full code:

function get_involved_block_info() {
  $blocks = array();
  $blocks['get_involved_block'] = array(
    'info' =&gt; t('Get involved block'), 
    'weight' =&gt; 10,   
  );

  return $blocks;
}

function get_involved_block_view($delta) {
  $block = array();
  switch ($delta) {
    case 'get_involved_block':
      global $user;
      $node = menu_get_object();
      // First of all, if the user is the author, do not show this node.
      if ($node &amp;&amp; $node-&gt;uid != $user-&gt;uid) {
        $block = array();
        $block['subject'] = t('Get involved');
        $block['content'] = flag_create_link('get_involved_interested', $node-&gt;nid);

        return $block;
      }
    break;
  }
}

function get_involved_flag_default_flags() {
  $flags = array();
  // Exported flag: "Interested".
  $flags['get_involved_interested'] = array(
    'entity_type' =&gt; 'node',
    'title' =&gt; 'Interested',
    'global' =&gt; false,
    'types' =&gt; array(
      0 =&gt; 'ad',
    ),
    'flag_short' =&gt; 'Join',
    'flag_long' =&gt; 'I want to join',
    'flag_message' =&gt; 'Your interest was registered',
    'unflag_short' =&gt; 'Cancel',
    'unflag_long' =&gt; 'Cancel my interest',
    'unflag_message' =&gt; 'Your interest was deleted',
    'unflag_denied_text' =&gt; '',
    'link_type' =&gt; 'toggle',
    'weight' =&gt; 0,
    'show_on_teaser' =&gt; false,
    'show_on_form' =&gt; true,
    'status' =&gt; false,
    'locked' =&gt; array(
      'name' =&gt; 'name',
      'types' =&gt; 'types',
      'global' =&gt; 'global',
    ),
    'module' =&gt; 'get_involved',
    'api_version' =&gt; 3,
    'import_roles' =&gt; array(
      'flag' =&gt; array(),
      'unflag' =&gt; array(),
    ),
    'show_in_links' =&gt; array(
      'full' =&gt; true,
    ),
  );

  return $flags;
}