Experimental project

This is a sandbox project, which contains experimental code for developer use only.

Provide a base set of classes to ease creation of custom blocks.

This is a developer-facing module intended to help ease the creation of multiple custom blocks through the use of base PHP classes. The module features two primary classes out-of-the-box: CustomBlocksBlock and CustomBlocksManager.

CustomBlocksBlock

This is the basic block class that developers can use to create their own custom blocks.

CustomBlocksManager

The manager class is used in the various block hooks to manage custom blocks. Currently, it is important that you set the class prefix in the constructor of the CustomBlocksManager. This allows the manager to automatically find the classes that is should manage for each of the hooks.

Here is an example .module file that creates and uses a CustomBlocksManager. This example will include a second file called my_custom_module.blocks.inc which contain the block classes. You can define the classes in any manner you wish though. The manager just inspects the defined classes to find block classes with the correct prefix.

/*
 * This module uses the custom_blocks_base module to easily create and manage blocks.
 * 
 * To view the blocks code, see my_custom_module.blocks.inc
 */

/**
 * Implements hook_block_info()
 */
function my_custom_module_block_info() {
	$block_manager = _my_custom_module_block_manager();
	return $block_manager->info();
}

/**
 * Implements hook_block_configure
 */
function my_custom_module_block_configure($delta = '') {
	$block_manager = _my_custom_module_block_manager();
	return $block_manager->configure($delta);
}

/**
 * Implements hook_block_save
 */
function my_custom_module_block_save($delta = '', $edit = array()) {
	$block_manager = _my_custom_module_block_manager();
	return $block_manager->save($delta, $edit);
}

/**
 * Implements hook_block_view()
 */
function my_custom_module_block_view($delta = '') {
	$block_manager = _my_custom_module_block_manager();
	return $block_manager->view($delta);
}

/**
 * Create the CustomBlocksManager for use with this module
 */
function _my_custom_module_block_manager() {
	module_load_include('module', 'custom_blocks_base');
	module_load_include('inc', 'my_custom_module', 'my_custom_module.blocks');
	
	if (!isset($GLOBALS['custom_block_manager'])) {
		$GLOBALS['custom_block_manager'] = new CustomBlocksManager('MyCustomModule');
	}
	return $GLOBALS['custom_block_manager'];
}

Project information