0

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' => t('Get involved block'), 
'weight' => 10,   
);
return $blocks;
}


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


function get_involved_block_view($delta) {
global $user;
$block = array();
if (arg(0) == 'node' && 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->uid == $user->uid)
{
} else {

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

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

  return $block;
}
}
  • Welcome to Drupal Answers! In short, you want to assign the value returned from a function to a variable, and you are wondering why your code doesn't work. This is a plain PHP question. – kiamlaluno Apr 24 '15 at 18:17
  • Hi Kiamlaluno, it is quite possibly a php question. However, the mechanics of displaying content in a block through the block_view() hook do not seem simply to be just php. I put the whole code in. If you still believe that it is php, then I will ask on stack O. – Pierre_colin Apr 24 '15 at 19:30
  • If you are asking why $string = "<?php flag_create_link('get_involved_interested', $nodeid); ?>"; doesn't get the function called, that is a plain PHP question. Since you changed that part, and shown the rest of the code, this starts to be a Drupal question. It could be you need to implement some hook you didn't implement. – kiamlaluno Apr 25 '15 at 6:08
1

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->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' => t('Get involved block'), 
    'weight' => 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 && $node->uid != $user->uid) {
        $block = array();
        $block['subject'] = t('Get involved');
        $block['content'] = flag_create_link('get_involved_interested', $node->nid);

        return $block;
      }
    break;
  }
}

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

  return $flags;
}
  • Unfortunately, whilst this does not create any error message, it does not show the link either... do I not need to put it in a <a href=...> tag? – Pierre_colin Apr 24 '15 at 18:12
  • 1
    Nope, you don't need to put this in anchor tags. Update your question with full code you are using; however, I believe that kiamlaluno is right in that this is a PHP question and not a Drupal one. – Aram Boyajyan Apr 24 '15 at 18:37
  • I have tried your suggestion with a flag created previously through the GUI, and it works. I am investigating to see what is wrong with the creation of my flag in the module. – Pierre_colin Apr 24 '15 at 21:57
  • I was struggling to get it to work, but after uninstalling the module and reinstalling it (and enabling the flag in the GUI), it worked fine. Thank you very much for the help! – Pierre_colin Apr 26 '15 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.