front and back-end web development, Leeds, UK


Richard's Blog - Design, coding and life in Japan

Richard

How to localize your main blog listing page using Drupal

I am just in the process of internationalizing this site into Japanese. In Drupal there is a path blog which has a list of all the teasers of the blog posts on your site. I looked into the source code of the Blog module and found the function 'blog_page_last' that feeds this list. Of course the golden rule in Drupal and any other type of web framework is 'DO NOT HACK THE CORE'!

Drupal awesomeness to the rescue

In my own module I realized that I could over-ride the loaded menu parameters with 'MYMODULE_menu_alter' hence changing the blog modules reference to 'blog' to my own function 'MYMODULE_menu_alter'. This is what I added.

function mackstarmod_menu_alter(&$items){
  $items['blog'] = array(
    'title' => 'Blogs',
    'page callback' => 'MYMODULE_blog_page_last',
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
  );
}

I then copy and paste the function blog_page_last and rename it MYMODULE_blog_page_last I then add the global $language and include the language into the SQL conditions. The output looks like the following.

function MYMODULE_blog_page_last() {
  global $user;
  global $language;

  $output = '';
  $items = array();

  if (user_access('create blog entries')) {
    $items[] = l(t('Create new blog entry.'), "node/add/blog");
  }

  $output = theme('item_list', $items);

  $result = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1  AND n.language = '$language->language' ORDER BY n.sticky DESC, n.created DESC"), variable_get('default_nodes_main', 10));
  $has_posts = FALSE;

  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
    $has_posts = TRUE;
  }
  
  if ($has_posts) {
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
  }
  else {
    drupal_set_message(t('No blog entries have been created.'));
  }
  drupal_add_feed(url('blog/feed'), t('RSS - blogs'));

  return $output;
}
Reload the cache and voila in my case the blogs for English are now in blog and Japanese in ja/blog. Although this is how I have approached the internationalization of a block that does not support it. This is also I believe the proper way to over-ride any other output from a non-custom module, meaning you don't have to hack! Thank you Drupal!

Tags:

Recent Blog Posts