I'm having difficulty understanding what my options are when trying to override a module's hook function.

As a concrete example, I've enabled the blog module on the site I'm responsible for, and I would like to modify the layout of the "Recent blog posts" block that it generates.

In the blog.module code, the function that does this is

/**
 * Implementation of hook_block().
 *
 * Displays the most recent 10 blog titles.
 */
function blog_block($op = 'list', $delta = 0) {
  global $user;
  if ($op == 'list') {
    $block[0]['info'] = t('Recent blog posts');
    return $block;
  }
  else if ($op == 'view') {
    if (user_access('access content')) {
      $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), 0, 10);
      if (db_num_rows($result)) {
        $block['content'] = node_title_list($result);
        $block['content'] .= '<div class="more-link">'. l(t('more'), 'blog', array('title' => t('Read the latest blog entries.'))) .'</div>';
        $block['subject'] = t('Recent blog posts');
        return $block;
      }
    }
  }
}

I want to alter the content of the links, and modify how many are listed. I don't want to modify blog.module directly, for the usual reasons. However, I can't determine how to override this function either from these forums or the drupal development books that I have. Can someone give me a clue?

Comments

nevets’s picture

You could provide your own module that provides the alternative blocks. You can not override functions in PHP.

behindthepage’s picture

Take that code and modify it so that it does what you want and put it in a custom block. Best to do this in an editor so you don't have any syntax errors cause they will kill your site and you will have to edit the database to switch off the block.

Check out the snippets page
http://drupal.org/node/21867

gpdinoz
"Everything should be made as simple as possible, but not simpler." - Albert Einstein

Regards
Geoff

glenn_paulley’s picture

Ah... now I get it. I need to create a module that defines a new block to list the recent blogs in the way I want (and therefore depends on the blog module being installed). I've already rewriten the blog_block function; I just have to include the other bits involved in establishing a block into the new module.

Thanks

Glenn

behindthepage’s picture

You don't need a module. You can put the php in a custom block by going to admin/build/block and then add block (admin/build/block/add). Put the php code in the block and choose php as the input type. Saves you the work of building a module.

gpdinoz
"Everything should be made as simple as possible, but not simpler." - Albert Einstein

Regards
Geoff

behindthepage’s picture

You can also create blocks and configue how many links and how they are displayed with the views module. Usually there are easier options than writing your own modules because someone else has solved the same problem before. Read all the module descriptions regularly (they keep multiplyng) and use drupal search and google search. You can save yourself a lot of time coding. But you will spend a lot of time reading.

gpdinoz
"Everything should be made as simple as possible, but not simpler." - Albert Einstein

Regards
Geoff