Shredder Puzzle Drupal Module

Written by James Mansson on December 5, 2012 Categories: Drupal

I recently created a small Drupal module to add a daily chess puzzle to the Horsham Chess Club website. In this post I’m going to look at how I implemented it.

The first thing to note is that I was targeting Drupal 7, so the following applies to that version.

The files for a Drupal module are organised inside a directory with the same internal name as the module. In this case I decided to name the module shredder_puzzle. There are several possible locations for module directories in the Drupal file structure:

  • The core modules directory (modules): This contains the modules that make up the Drupal core; it will be overwritten as part of any upgrade, and so is not an appropriate place to use.
  • The common┬ámodules directory (sites/all/modules): This is for modules shared by all sites.
  • The site-specific modules directory (sites/sitename/modules): This is for modules for a specific site.

As the module is aimed at just the default site, I placed the directory under sites/default/modules.

The next thing to do was to create the module’s info file. This has a name in the form modulename.info, in this case shredder_puzzle.info. It contained the following:

name = Shredder Puzzle
description = A module for displaying the daily Shredder chess puzzle.
package = Chess
core = 7.x
files[] = shredder_puzzle.module

The contents should be self-explanatory.

The implementation is contained in the module’s module file. This has a name in the form modulename.module, in this case shredder_puzzle.module.

A Drupal module links in with the core Drupal system via a series of hooks. Hooks are functions with names in the form modulename_hookname; the parameters and return types depend on the specific hook. In this case I implemented the following hooks:

  • Help (shredder_puzzle_help)
  • Block Info (shredder_puzzle_block_info)
  • Block View (shredder_puzzle_block_view)

The Help hook is used to retrieve the help text for the module for display in the Drupal administration section. It has a simple implementation:

/**
* Implements hook_help().
*/
function shredder_puzzle_help($path, $arg) {
	if ($path == 'admin/help#shredder_puzzle') {
		return t('A module for displaying the daily Shredder chess puzzle.');
	}
}

The basic function of the module is to create a block containing the markup for the chess puzzle, which loads the current puzzle via an iframe. This block can then be inserted into the site’s content in an appropriate place.

The purpose of the Block Info hook is to allow the core system to determine the basic details of the blocks that the module can generate. This particular module offers five different blocks, which are the chess puzzle in five different sizes:

/**
* Implements hook_block_info().
*/
function shredder_puzzle_block_info() {
	// Generate the list of blocks
	$blocks = array();

	$blocks['tiny_puzzle'] = array(
		'info' => t('Tiny version of the daily Shredder chess puzzle.'),
		'cache' => DRUPAL_NO_CACHE,
	);

	$blocks['small_puzzle'] = array(
		'info' => t('Small version of the daily Shredder chess puzzle.'),
		'cache' => DRUPAL_NO_CACHE,
	);

	$blocks['medium_puzzle'] = array(
		'info' => t('Medium version of the daily Shredder chess puzzle.'),
		'cache' => DRUPAL_NO_CACHE,
	);

	$blocks['large_puzzle'] = array(
		'info' => t('Large version of the daily Shredder chess puzzle.'),
		'cache' => DRUPAL_NO_CACHE,
	);

	$blocks['very_large_puzzle'] = array(
		'info' => t('Very large version of the daily Shredder chess puzzle.'),
		'cache' => DRUPAL_NO_CACHE,
	);

	return $blocks;
}

Note the structure of the array returned by the function. The array keys contain the unique ID of the block type. Each array value is itself an array, which contains a description of the block and the caching setting.

When the core system needs to render an instance of a particular block, it uses the Block View hook. This is passed the unique ID of the particular block, and returns the markup:

/**
* Implements hook_block_view().
*/
function shredder_puzzle_block_view($block_name = '') {
	// Get the block content
	if ($block_name == 'tiny_puzzle') {
		$content = '<iframe width="148" scrolling="no" height="197" frameborder="0" src="http://www.shredderchess.com/online/playshredder/gdailytactics.php?mylang=en&mysize=14"></iframe>';
	} else if ($block_name == 'small_puzzle') {
		$content = '<iframe width="184" scrolling="no" height="229" frameborder="0" src="http://www.shredderchess.com/online/playshredder/gdailytactics.php?mylang=en&mysize=18"></iframe>';
	} else if ($block_name == 'medium_puzzle') {
		$content = '<iframe width="220" scrolling="no" height="261" frameborder="0" src="http://www.shredderchess.com/online/playshredder/gdailytactics.php?mylang=en&mysize=22"></iframe>';
	} else if ($block_name == 'large_puzzle') {
		$content = '<iframe width="256" scrolling="no" height="293" frameborder="0" src="http://www.shredderchess.com/online/playshredder/gdailytactics.php?mylang=en&mysize=26"></iframe>';
	} else if ($block_name == 'very_large_puzzle') {
		$content = '<iframe width="310" scrolling="no" height="341" frameborder="0" src="http://www.shredderchess.com/online/playshredder/gdailytactics.php?mylang=en&mysize=32"></iframe>';
	} else {
		$content = '';
	}

	$block = array(
		'subject' => t('Shredder Daily Puzzle'),
		'content' => $content,
	);

	return $block;
}

Note again the structure of the array returned by the function. This returns a heading for the block (subject) and the body (content).

The module is now ready for use. It should be activated via the Drupal administration section. The most straightforward way to use it is then to add it as a block in the theme layout.

For an example of it in action, see the right sidebar on the Horsham Chess Club website.

No Comments on Shredder Puzzle Drupal Module

Leave a Reply

Your email address will not be published. Required fields are marked *