'blocktheme-admin-display-form',
);
}
return $return;
}
/**
* Implementation of hook_menu().
*/
function blocktheme_menu() {
$items = array();
$items['admin/settings/blocktheme'] = array(
'title' => t('Block Theme'),
'description' => t('Allows the admin to define re-usable block templates that can be configured from block config screen'),
'page callback' => 'drupal_get_form',
'page arguments' => array('blocktheme_admin_settings'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Block Theme settings page.
*/
function blocktheme_admin_settings() {
$form = array();
$form['blocktheme_themes'] = array(
'#type' => 'textarea',
'#default_value' => variable_get('blocktheme_themes', ''),
'#title' => t('Custom Block Templates'),
'#description' => t('Enter in the form: customtemplate|Friendly Name Where customtemplate corresponds to a tpl file called blocktheme-customtemplate.tpl.php as well as to the value of an extra variable $blocktheme in the block templates'),
'#wysiwyg' => FALSE,
);
$form['blocktheme_show_custom_block_theme'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('blocktheme_show_custom_block_theme', ''),
'#title' => t('Show Custom Block Theme'),
'#description' => t('Show the custom block theme used for a block in the block admin page.', array('@block-admin-page' => url('admin/build/block'))),
);
return system_settings_form($form);
}
/**
* Implementation of hook_help().
*/
function blocktheme_help($path, $arg) {
switch ($path) {
case 'admin/help#blocktheme':
return t('Allows the admin to define re-usable block templates that can be configured from block config screen');
break;
case 'admin/settings/blocktheme':
return t('BlockTheme allows an admin to define tpl files for standard block templates and provides a select box on the block configure form so the user can select a tpl file to use as opposed to having to override the templates by block ID.');
break;
}
}
/**
* Form for updating a block
*/
function blocktheme_form_block_admin_configure_alter(&$form, &$form_state) {
$module = $form['module']['#value'];
$delta = $form['delta']['#value'];
$var_name = $module .'-'. $delta;
$options = blocktheme_get_blockthemes();
$blocktheme = blocktheme_get();
$form['block_settings']['blocktheme'] = array(
'#type' => 'select',
'#weight' => -1,
'#title' => t('Custom Theme'),
'#default_value' => $blocktheme[$var_name],
'#options' => $options,
);
$form['#submit'][] = 'blocktheme_update';
}
/**
* Form for adding a new block
*/
function blocktheme_form_block_add_block_form_alter(&$form, &$form_state) {
$options = blocktheme_get_blockthemes();
$form['block_settings']['blocktheme'] = array(
'#type' => 'select',
'#weight' => -1,
'#title' => t('Custom Theme'),
'#options' => $options,
);
$form['#submit'][] = 'blocktheme_save';
}
/**
* Set Block Theme custom block settings.
*/
function blocktheme_set($new_val) {
variable_set('blocktheme', $new_val);
}
/**
* Get Block Theme custom block settings.
*/
function blocktheme_get() {
static $blocktheme;
if (empty($blocktheme)) {
$blocktheme = variable_get('blocktheme', array());
}
return $blocktheme;
}
/**
* Update an existing block after the block form has been submitted
*/
function blocktheme_update($form_id, &$form_state) {
$var_name = $form_state['values']['module'] .'-'. $form_state['values']['delta'];
$blocktheme = blocktheme_get();
if (!$form_state['values']['blocktheme']) {
unset($blocktheme[$var_name]);
}
else {
$blocktheme[$var_name] = $form_state['values']['blocktheme'];
}
blocktheme_set($blocktheme);
}
/**
* Save a new block after form was submitted
*/
function blocktheme_save($form_id, &$form_state) {
// first get the new delta value
$result = db_query("SELECT delta FROM {blocks} WHERE bid = '%s'", db_last_insert_id('boxes', 'bid'));
if ($delta_result = db_fetch_object($result)) {
$delta = $delta_result->delta;
$var_name = $form_state['values']['module'] .'-'. $delta;
$blocktheme = blocktheme_get();
if (!$form_state['values']['blocktheme']) {
unset($blocktheme[$var_name]);
}
else {
$blocktheme[$var_name] = $form_state['values']['blocktheme'];
}
blocktheme_set($blocktheme);
};
}
/**
* Get the defined blockthemes and return an array to be used in a select list.
*/
function blocktheme_get_blockthemes() {
$options = array();
$blockthemes = variable_get('blocktheme_themes', '');
$options[] = t('- None -');
if ($blockthemes) {
$_sets = explode("\n", $blockthemes);
foreach ($_sets as $key => $value) {
$set = explode('|', $value);
$options[$set[0]] = $set[1];
}
}
return $options;
}
/**
* Get custom theme for a block.
*/
function blocktheme_get_theme(&$block) {
$blocktheme = blocktheme_get();
$var_name = $block->module .'-'. $block->delta;
if (isset($blocktheme[$var_name])) {
return $blocktheme[$var_name];
}
}
/**
* Implementation of hook_preprocess_block().
*/
function blocktheme_preprocess_block(&$vars) {
if ($custom_block_theme = blocktheme_get_theme($vars['block'])) {
// Added for downward compatibility:
// Remove blocktheme- prefix if present
// Maybe use an update instead?
$len = drupal_strlen('blocktheme-');
if (strncmp($custom_block_theme, 'blocktheme-', $len) == 0) {
$custom_block_theme = drupal_substr($custom_block_theme, $len);
}
$vars['blocktheme'] = $custom_block_theme;
$vars['template_files'][] = 'blocktheme-' . $custom_block_theme;
/* Added for backward compatibility */
$vars['template_files'][] = $custom_block_theme;
}
}
/**
* Implementation of template_preprocess_block_admin_display_form().
*/
function blocktheme_preprocess_block_admin_display_form(&$variables) {
if (variable_get('blocktheme_show_custom_block_theme', '')) {
// Get blocks with custom templates.
$blocktheme = blocktheme_get();
// Get block theme names.
$blocktheme_themes = blocktheme_get_blockthemes();
foreach (element_children($variables['form']) as $i) {
$block = &$variables['form'][$i];
// Only take form elements that are blocks.
if (isset($block['info'])) {
// Fetch region for current block.
$region = $block['region']['#default_value'];
// Set block theme key for the current module.
$var_name = $block['module']['#value'] .'-'. $block['delta']['#value'];
$variables['block_listing'][$region][$i]->custom_block_theme = (isset($blocktheme[$var_name])) ? $blocktheme_themes[$blocktheme[$var_name]] : '';
}
}
}
}
// @todo: Is it better to unserialize a huge array, or to use a hacky string based namespace?