I am having an issue creating a block. I just do not know how the hook_block view works or how I get my code to work.

/*
*  Impliment hook_block_view().
*/
function chris_to_buy_block_view($block_name = ''){
if ($block_name == 'project_activation'){
require_once 'includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    global $user;
    print 'this is now visible.';
    if ((arg(0) == 'node') && is_numeric(arg(1)) && !(arg(2))) {
      $node = node_load(arg(1));
      $Pnode = node_load(arg(1) -4);
      $pnode_nid = $Pnode->nid;
      $comments = $node->comment_count;
      $nid = $node->nid;
      $uid = $node->uid;
      $unread_comments = ($node->field_total_comment_count['und'][0]['value'] - $comments);
      if($node->field_to_buy['und'][0]['value']== 0 && $unread_comments >0 && $uid == $user->uid) {
        $flag = flag_get_flag('thorny_follow');    
        echo '<strong><font color=#2872C2><h3 class="title comment-form">';
        print t('You are ready to activate this project!');
        echo '</h3></font></strong><h4><font color=#2872C2>';
        print 'For this project you have:';
        echo '<ul>';
        print $comments .'  visible comments,';
        echo '</br>';
        print  $unread_comments .'  unread comments and, ';
        echo '</br>';
        print $flag->get_count($nid) .'  followers';
        echo '</ul></font><h4 align= center><font color= #952112><strong>';
        print t("To see your unread comments and followers activate your project now.");
        echo '</strong></font></h4>';
      }
      print '<form action="to_buy.module" method="POST">';
      print '<input type="image" name="action" value="Activation Button" img src="/sites/default/files/default_images/Project-activation-1.jpg" class="activate"/>';
      print '</form>';
      if($_POST['action'] == "Activation Button"){
        $Pnode->field_onclick_buy['und'][0]['value'] = 1;
        header( 'Location: /node/$pnode_nid');
      }
    }
    return $blocks;
}
}

Comments

OR Should it look like this?

function chris_to_buy_block_view(($delta = '') {

  $block = array();

  switch ($delta) {
    case 'syndicate':
      $block['subject'] = t('Project_activation');
      $block['content'] = array(
        '#theme' => 'feed_icon',
        '#url' => 'rss.xml',
        '#title' => t('Syndicate'),
      );
    require_once 'includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    global $user;
    print 'this is now visible.';
    if ((arg(0) == 'node') && is_numeric(arg(1)) && !(arg(2))) {
      $node = node_load(arg(1));
//   code continued.....
      break;
}
}
Spineless

Both are valid, the 'if' and

Both are valid, the 'if' and 'switch' provide the same logic. I prefer the switch for readability.

What is your actual problem?

Thanks for the info. I was

Thanks for the info. I was able to get the hook_block_view() to do something. I am able to see the block in the list of blocks located at admin/structure/blocks.

However, when I move the block to the "highlighted" region it does not appear there. No matter where I put it the block it only appears at the top of the page. Here is the code I am working with.

/*
* Implementation of hook_block_info().
*/  
function chris_to_buy_block_info(){
  $blocks = array();

  $blocks['project_activation'] = array(
    'info' => t('Project activation'),
    'region' => array('Highlighted'),
    'status' => 1,
    'cashe' => DRUPAL_NO_CACHE,
  ); 
  return $blocks;
}

/*
*  Implementation hook_block_view().
*/
function chris_to_buy_block_view($delta = ''){
$block = array();
switch ($delta){
      case 'project_activation':
    $block ['subject'] = t('Project Activation');
    $block ['content'] = printmesss();
   break;
   }
    return $blocks;
}

function printmesss(){
Print ' this is a test of printmesss.';
}
Spineless

You initialize and use $block

You initialize and use $block but return $blocks

block does not appear in region

Thanks for pointing out that mistake. However, I still cannot see the block in the correct region. I only see it at the top of the page.

function chris_to_buy_block_info(){
$block = array();

$block['project_activation'] = array(
'info' => t('Project activation'),
// 'region' => 'Highlighted',
'status' => 1,
'cashe' => DRUPAL_NO_CACHE,
);
return $block;
}

/*
* Implementation hook_block_view().
*/
function chris_to_buy_block_view($delta = ''){
$block = array();
switch ($delta){
case 'project_activation':
$block ['subject'] = t('Project Activation');
$block ['content'] = printmesss();
break;
return $block;
}

function printmesss(){
print ' This is a test of printmesss.';
}

Spineless

In printmesss you want to

In printmesss you want to return the contents of the block (not print the contents).

Yes. You also don't ever need

Yes. You also don't ever need to bootstrap Drupal when working within Drupal.

A simple example:

<?php
function my_module_block_view($delta = '')
{
  if(
$delta === 'my_block')
  {
   
$block['subject'] = t('This is the block subject');
   
$block['content'] = t('This is the block contents');

    return
$block;
  }
}
?>

As you can see I've created a block with a subject and content, and returned it. No printing to the screen. The reason you are 'seeing your block printed at the top of the screen' is because you are printing the data which happens during processing, before page generation. If you look at the page source, you will see that the data you are printing comes ahead of your actual web page.

Jaypan We build websites

Well this is just example code

The print printmess replaces about 20 lines of code that is currently working in the PHP block. But I need to make this a module now.

So to make life simple I am just printing test text.

The real issue I am attempting to solve is why does my test text appear at the top of the page instead of in the block region?

Spineless

As I said before, printmess()

As I said before, printmess() needs to return the text/html for the block instead of printing it.

Thanks

Thanks I guess i did not read the word return the correct way. I got it now.

:)

Spineless

I not only showed an example,

I not only showed an example, I explained exactly why it was happening. Did you not read my post?

Jaypan We build websites

nobody click here