2

I have updated a module from Drupal 6 to Drupal 7, but the following code doesn't work anymore. I have updated the .info file, but the module still doesn't work.

function header_image_block($op='list',$delta=0,$edit=array()) {
   if ($op=='list') {
     $blocks[0] = array(
       'info' => t('Header image'),
     );
     return $blocks;
   }

   elseif ($op == 'view') {
     $block = array(
       'subject' => 'Header Images',
       'content' => changeHeader(),
     );
     return $block;
   }
}

function changeHeader() {
  $output = '';
  $output = '<img src = "' . base_path() . 'sites/all/themes/optimiza/images/imge-header.png" border = "0" />';
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);
    $path = $node->field_head_img[0]['filepath'];
    if ($path != '') {
      $output = '<img src = "' . base_path() . $path . '" border = "0" />';
    }
  }
  return $output;
}

1 Answer 1

3

In Drupal 7, instead of hook_block() there are more hooks:

In short, you need to split the Drupal 6 code for hook_block() between those hooks.

As side notes:

  • Every function implemented from a module should be prefixed by the module short name
  • To load the node, you should use the following code.

    if ($node = menu_get_object()) {
      // The page is a node page.
    }  
    
  • To access the content of a field, you should use the following code.

    $items = field_get_items('node', $node, 'field_head_img');
    $path = $items[0]['filepath'];
    
  • To output an image, you should use theme('image'), or theme('image_style'). As you are outputting a field, you should use field_view_field(), as in the following code.

    $output = field_view_field('node', $node, 'field_head_img');
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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