'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(); $blocktheme_vars = blocktheme_get_vars(); $form['block_settings']['#weight'] = -1; $form['custom_block_theme'] = array( '#type' => 'fieldset', '#title' => t('Block Theme'), '#weight' => 0, '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['custom_block_theme']['blocktheme'] = array( '#type' => 'select', '#title' => t('Custom theme'), '#default_value' => $blocktheme[$var_name], '#options' => $options, ); $form['custom_block_theme']['blocktheme_vars'] = array( '#type' => 'textarea', '#default_value' => blocktheme_format_vars_admin($blocktheme_vars[$var_name]), '#title' => t('Custom block variables'), '#description' => t('Enter one entry per line, in the format: variable_name|variable_content.'), '#wysiwyg' => FALSE, ); $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']['#weight'] = -1; $form['custom_block_theme'] = array( '#type' => 'fieldset', '#title' => t('Block Theme'), '#weight' => 0, '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['custom_block_theme']['blocktheme'] = array( '#type' => 'select', '#title' => t('Custom theme'), '#options' => $options, ); $form['custom_block_theme']['blocktheme_vars'] = array( '#type' => 'textarea', '#default_value' => '', '#title' => t('Custom block variables'), '#description' => t('Enter one entry per line, in the format: variable_name|variable_content.'), '#wysiwyg' => FALSE, ); $form['#submit'][] = 'blocktheme_save'; } /** * Set Block Theme custom block settings. */ function blocktheme_set($blocktheme, $blocktheme_vars = NULL) { variable_set('blocktheme', $blocktheme); if ($blocktheme_vars) { variable_set('blocktheme_vars', $blocktheme_vars); } } /** * Get Block Theme custom block settings. */ function blocktheme_get() { static $blocktheme; if (empty($blocktheme)) { $blocktheme = variable_get('blocktheme', array()); } return $blocktheme; } /** * Get Block Theme custom block variables settings. */ function blocktheme_get_vars() { static $blocktheme_vars; if (empty($blocktheme_vars)) { $blocktheme_vars = variable_get('blocktheme_vars', array()); } return $blocktheme_vars; } /** * 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(); $blocktheme_vars = blocktheme_get_vars(); if (!$form_state['values']['blocktheme']) { unset($blocktheme[$var_name]); } else { $blocktheme[$var_name] = $form_state['values']['blocktheme']; } if (!$form_state['values']['blocktheme_vars']) { unset($blocktheme_vars[$var_name]); } else { $blocktheme_vars[$var_name] = blocktheme_format_vars($form_state['values']['blocktheme_vars']); } blocktheme_set($blocktheme, $blocktheme_vars); } /** * 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(); $blocktheme_vars = blocktheme_get_vars(); if ($form_state['values']['blocktheme']) { $blocktheme[$var_name] = $form_state['values']['blocktheme']; } if ($form_state['values']['blocktheme_vars']) { $blocktheme_vars[$var_name] = blocktheme_format_vars($form_state['values']['blocktheme_vars']); } blocktheme_set($blocktheme, $blocktheme_vars); }; } /** * 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; } /** * Formats custom variables as an array to be used in the block template. */ function blocktheme_format_vars($block_vars) { $formatted_vars = array(); if ($block_vars) { $_sets = explode("\n", $block_vars); foreach ($_sets as $key => $value) { $set = explode('|', $value); $formatted_vars[$set[0]] = $set[1]; } } return $formatted_vars; } /** * Formats custom variables for displaying in a block configuration form. */ function blocktheme_format_vars_admin($block_vars) { $formatted_vars = ''; if ($block_vars) { foreach ($block_vars as $key => $value) { $_formatted_vars[] = $key .'|'. $value; } $formatted_vars = implode("\n", $_formatted_vars); } return $formatted_vars; } /** * 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]; } } /** * Get custom variables for a block. */ function blocktheme_get_theme_vars($block) { $blocktheme_vars = blocktheme_get_vars(); $var_name = $block->module .'-'. $block->delta; if (isset($blocktheme_vars[$var_name])) { return $blocktheme_vars[$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; } if ($custom_block_vars = blocktheme_get_theme_vars($vars['block'])) { $vars['blocktheme_vars'] = $custom_block_vars; } } /** * 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]] : ''; } } } }