webmaster@1: '. t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Garland, for example, implements the regions "left sidebar", "right sidebar", "content", "header", and "footer", and a block may appear in any one of these areas. The blocks administration page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('@blocks' => url('admin/build/block'))) .'

'; webmaster@1: $output .= '

'. t('Although blocks are usually generated automatically by modules (like the User login block, for example), administrators can also define custom blocks. Custom blocks have a title, description, and body. The body of the block can be as long as necessary, and can contain content supported by any available input format.', array('@input-format' => url('admin/settings/filters'))) .'

'; webmaster@1: $output .= '

'. t('When working with blocks, remember that:') .'

'; webmaster@1: $output .= ''; webmaster@1: $output .= '

'. t('For more information, see the online handbook entry for Block module.', array('@block' => 'http://drupal.org/handbook/modules/block/')) .'

'; webmaster@1: return $output; webmaster@1: case 'admin/build/block': webmaster@1: $throttle = module_exists('throttle'); webmaster@1: $output = '

'. t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. To change the region or order of a block, grab a drag-and-drop handle under the Block column and drag the block to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the Save blocks button at the bottom of the page.') .'

'; webmaster@1: if ($throttle) { webmaster@1: $output .= '

'. t('To reduce CPU usage, database traffic or bandwidth, blocks may be automatically disabled during high server loads by selecting their Throttle checkbox. Adjust throttle thresholds on the throttle configuration page.', array('@throttleconfig' => url('admin/settings/throttle'))) .'

'; webmaster@1: } webmaster@1: $output .= '

'. t('Click the configure link next to each block to configure its specific title and visibility settings. Use the add block page to create a custom block.', array('@add-block' => url('admin/build/block/add'))) .'

'; webmaster@1: return $output; webmaster@1: case 'admin/build/block/add': webmaster@1: return '

'. t('Use this page to create a new custom block. New blocks are disabled by default, and must be moved to a region on the blocks administration page to be visible.', array('@blocks' => url('admin/build/block'))) .'

'; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_theme() webmaster@1: */ webmaster@1: function block_theme() { webmaster@1: return array( webmaster@1: 'block_admin_display_form' => array( webmaster@1: 'template' => 'block-admin-display-form', webmaster@1: 'file' => 'block.admin.inc', webmaster@1: 'arguments' => array('form' => NULL), webmaster@1: ), webmaster@1: ); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_perm(). webmaster@1: */ webmaster@1: function block_perm() { webmaster@1: return array('administer blocks', 'use PHP for block visibility'); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_menu(). webmaster@1: */ webmaster@1: function block_menu() { webmaster@1: $items['admin/build/block'] = array( webmaster@1: 'title' => 'Blocks', webmaster@1: 'description' => 'Configure what block content appears in your site\'s sidebars and other regions.', webmaster@1: 'page callback' => 'block_admin_display', webmaster@1: 'access arguments' => array('administer blocks'), webmaster@1: 'file' => 'block.admin.inc', webmaster@1: ); webmaster@1: $items['admin/build/block/list'] = array( webmaster@1: 'title' => 'List', webmaster@1: 'type' => MENU_DEFAULT_LOCAL_TASK, webmaster@1: 'weight' => -10, webmaster@1: ); webmaster@1: $items['admin/build/block/list/js'] = array( webmaster@1: 'title' => 'JavaScript List Form', webmaster@1: 'page callback' => 'block_admin_display_js', webmaster@5: 'access arguments' => array('administer blocks'), webmaster@1: 'type' => MENU_CALLBACK, webmaster@1: 'file' => 'block.admin.inc', webmaster@1: ); webmaster@1: $items['admin/build/block/configure'] = array( webmaster@1: 'title' => 'Configure block', webmaster@1: 'page callback' => 'drupal_get_form', webmaster@1: 'page arguments' => array('block_admin_configure'), webmaster@5: 'access arguments' => array('administer blocks'), webmaster@1: 'type' => MENU_CALLBACK, webmaster@1: 'file' => 'block.admin.inc', webmaster@1: ); webmaster@1: $items['admin/build/block/delete'] = array( webmaster@1: 'title' => 'Delete block', webmaster@1: 'page callback' => 'drupal_get_form', webmaster@1: 'page arguments' => array('block_box_delete'), webmaster@5: 'access arguments' => array('administer blocks'), webmaster@1: 'type' => MENU_CALLBACK, webmaster@1: 'file' => 'block.admin.inc', webmaster@1: ); webmaster@1: $items['admin/build/block/add'] = array( webmaster@1: 'title' => 'Add block', webmaster@1: 'page callback' => 'drupal_get_form', webmaster@1: 'page arguments' => array('block_add_block_form'), webmaster@5: 'access arguments' => array('administer blocks'), webmaster@1: 'type' => MENU_LOCAL_TASK, webmaster@1: 'file' => 'block.admin.inc', webmaster@1: ); webmaster@1: $default = variable_get('theme_default', 'garland'); webmaster@1: foreach (list_themes() as $key => $theme) { webmaster@1: $items['admin/build/block/list/'. $key] = array( webmaster@1: 'title' => check_plain($theme->info['name']), webmaster@1: 'page arguments' => array($key), webmaster@1: 'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, webmaster@1: 'weight' => $key == $default ? -10 : 0, webmaster@1: 'file' => 'block.admin.inc', webmaster@1: 'access callback' => '_block_themes_access', webmaster@1: 'access arguments' => array($theme), webmaster@1: ); webmaster@1: } webmaster@1: return $items; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Menu item access callback - only admin or enabled themes can be accessed webmaster@1: */ webmaster@1: function _block_themes_access($theme) { webmaster@1: return user_access('administer blocks') && ($theme->status || $theme->name == variable_get('admin_theme', '0')); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_block(). webmaster@1: * webmaster@1: * Generates the administrator-defined blocks for display. webmaster@1: */ webmaster@1: function block_block($op = 'list', $delta = 0, $edit = array()) { webmaster@1: switch ($op) { webmaster@1: case 'list': webmaster@1: $blocks = array(); webmaster@1: webmaster@1: $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info'); webmaster@1: while ($block = db_fetch_object($result)) { webmaster@1: $blocks[$block->bid]['info'] = $block->info; webmaster@1: // Not worth caching. webmaster@1: $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE; webmaster@1: } webmaster@1: return $blocks; webmaster@1: webmaster@1: case 'configure': webmaster@1: $box = array('format' => FILTER_FORMAT_DEFAULT); webmaster@1: if ($delta) { webmaster@1: $box = block_box_get($delta); webmaster@1: } webmaster@1: if (filter_access($box['format'])) { webmaster@1: return block_box_form($box); webmaster@1: } webmaster@1: break; webmaster@1: webmaster@1: case 'save': webmaster@1: block_box_save($edit, $delta); webmaster@1: break; webmaster@1: webmaster@1: case 'view': webmaster@1: $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta)); webmaster@1: $data['content'] = check_markup($block->body, $block->format, FALSE); webmaster@1: return $data; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Update the 'blocks' DB table with the blocks currently exported by modules. webmaster@1: * webmaster@1: * @return webmaster@1: * Blocks currently exported by modules. webmaster@1: */ webmaster@1: function _block_rehash() { webmaster@1: global $theme_key; webmaster@1: webmaster@1: init_theme(); webmaster@1: webmaster@1: $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key); webmaster@1: $old_blocks = array(); webmaster@1: while ($old_block = db_fetch_array($result)) { webmaster@1: $old_blocks[$old_block['module']][$old_block['delta']] = $old_block; webmaster@1: } webmaster@1: webmaster@1: $blocks = array(); webmaster@1: // Valid region names for the theme. webmaster@1: $regions = system_region_list($theme_key); webmaster@1: webmaster@1: foreach (module_list() as $module) { webmaster@1: $module_blocks = module_invoke($module, 'block', 'list'); webmaster@1: if ($module_blocks) { webmaster@1: foreach ($module_blocks as $delta => $block) { webmaster@1: if (empty($old_blocks[$module][$delta])) { webmaster@1: // If it's a new block, add identifiers. webmaster@1: $block['module'] = $module; webmaster@1: $block['delta'] = $delta; webmaster@1: $block['theme'] = $theme_key; webmaster@1: if (!isset($block['pages'])) { webmaster@1: // {block}.pages is type 'text', so it cannot have a webmaster@1: // default value, and not null, so we need to provide webmaster@1: // value if the module did not. webmaster@1: $block['pages'] = ''; webmaster@1: } webmaster@1: // Add defaults and save it into the database. webmaster@1: drupal_write_record('blocks', $block); webmaster@1: // Set region to none if not enabled. webmaster@1: $block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE; webmaster@1: // Add to the list of blocks we return. webmaster@1: $blocks[] = $block; webmaster@1: } webmaster@1: else { webmaster@1: // If it's an existing block, database settings should overwrite webmaster@1: // the code. But aside from 'info' everything that's definable in webmaster@1: // code is stored in the database and we do not store 'info', so we webmaster@1: // do not need to update the database here. webmaster@1: // Add 'info' to this block. webmaster@1: $old_blocks[$module][$delta]['info'] = $block['info']; webmaster@1: // If the region name does not exist, disable the block and assign it to none. webmaster@1: if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) { webmaster@1: drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_blocks[$module][$delta]['info'], '%region' => $old_blocks[$module][$delta]['region'])), 'warning'); webmaster@1: $old_blocks[$module][$delta]['status'] = 0; webmaster@1: $old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE; webmaster@1: } webmaster@1: else { webmaster@1: $old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE; webmaster@1: } webmaster@1: // Add this block to the list of blocks we return. webmaster@1: $blocks[] = $old_blocks[$module][$delta]; webmaster@1: // Remove this block from the list of blocks to be deleted. webmaster@1: unset($old_blocks[$module][$delta]); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Remove blocks that are no longer defined by the code from the database. webmaster@1: foreach ($old_blocks as $module => $old_module_blocks) { webmaster@1: foreach ($old_module_blocks as $delta => $block) { webmaster@1: db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme_key); webmaster@1: } webmaster@1: } webmaster@1: return $blocks; webmaster@1: } webmaster@1: webmaster@1: function block_box_get($bid) { webmaster@1: return db_fetch_array(db_query("SELECT bx.*, bl.title FROM {boxes} bx INNER JOIN {blocks} bl ON bx.bid = bl.delta WHERE bl.module = 'block' AND bx.bid = %d", $bid)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Define the custom block form. webmaster@1: */ webmaster@1: function block_box_form($edit = array()) { webmaster@1: $edit += array( webmaster@1: 'info' => '', webmaster@1: 'body' => '', webmaster@1: ); webmaster@1: $form['info'] = array( webmaster@1: '#type' => 'textfield', webmaster@1: '#title' => t('Block description'), webmaster@1: '#default_value' => $edit['info'], webmaster@1: '#maxlength' => 64, webmaster@1: '#description' => t('A brief description of your block. Used on the block overview page.', array('@overview' => url('admin/build/block'))), webmaster@1: '#required' => TRUE, webmaster@1: '#weight' => -19, webmaster@1: ); webmaster@1: $form['body_field']['#weight'] = -17; webmaster@1: $form['body_field']['body'] = array( webmaster@1: '#type' => 'textarea', webmaster@1: '#title' => t('Block body'), webmaster@1: '#default_value' => $edit['body'], webmaster@1: '#rows' => 15, webmaster@1: '#description' => t('The content of the block as shown to the user.'), webmaster@1: '#weight' => -17, webmaster@1: ); webmaster@1: if (!isset($edit['format'])) { webmaster@1: $edit['format'] = FILTER_FORMAT_DEFAULT; webmaster@1: } webmaster@1: $form['body_field']['format'] = filter_form($edit['format'], -16); webmaster@1: webmaster@1: return $form; webmaster@1: } webmaster@1: webmaster@1: function block_box_save($edit, $delta) { webmaster@1: if (!filter_access($edit['format'])) { webmaster@1: $edit['format'] = FILTER_FORMAT_DEFAULT; webmaster@1: } webmaster@1: webmaster@1: db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta); webmaster@1: webmaster@1: return TRUE; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_user(). webmaster@1: * webmaster@1: * Allow users to decide which custom blocks to display when they visit webmaster@1: * the site. webmaster@1: */ webmaster@1: function block_user($type, $edit, &$account, $category = NULL) { webmaster@1: switch ($type) { webmaster@1: case 'form': webmaster@1: if ($category == 'account') { webmaster@1: $rids = array_keys($account->roles); webmaster@1: $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids); webmaster@1: $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE); webmaster@1: while ($block = db_fetch_object($result)) { webmaster@1: $data = module_invoke($block->module, 'block', 'list'); webmaster@1: if ($data[$block->delta]['info']) { webmaster@1: $return = TRUE; webmaster@1: $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => check_plain($data[$block->delta]['info']), '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1)); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if (!empty($return)) { webmaster@1: return $form; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: break; webmaster@1: case 'validate': webmaster@1: if (empty($edit['block'])) { webmaster@1: $edit['block'] = array(); webmaster@1: } webmaster@1: return $edit; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return all blocks in the specified region for the current user. webmaster@1: * webmaster@1: * @param $region webmaster@1: * The name of a region. webmaster@1: * webmaster@1: * @return webmaster@1: * An array of block objects, indexed with module_delta. webmaster@1: * If you are displaying your blocks in one or two sidebars, you may check webmaster@1: * whether this array is empty to see how many columns are going to be webmaster@1: * displayed. webmaster@1: * webmaster@1: * @todo webmaster@1: * Now that the blocks table has a primary key, we should use that as the webmaster@1: * array key instead of module_delta. webmaster@1: */ webmaster@1: function block_list($region) { webmaster@1: global $user, $theme_key; webmaster@1: webmaster@1: static $blocks = array(); webmaster@1: webmaster@1: if (!count($blocks)) { webmaster@1: $rids = array_keys($user->roles); webmaster@1: $result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array($theme_key), $rids)); webmaster@1: while ($block = db_fetch_object($result)) { webmaster@1: if (!isset($blocks[$block->region])) { webmaster@1: $blocks[$block->region] = array(); webmaster@1: } webmaster@1: // Use the user's block visibility setting, if necessary webmaster@1: if ($block->custom != 0) { webmaster@1: if ($user->uid && isset($user->block[$block->module][$block->delta])) { webmaster@1: $enabled = $user->block[$block->module][$block->delta]; webmaster@1: } webmaster@1: else { webmaster@1: $enabled = ($block->custom == 1); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: $enabled = TRUE; webmaster@1: } webmaster@1: webmaster@1: // Match path if necessary webmaster@1: if ($block->pages) { webmaster@1: if ($block->visibility < 2) { webmaster@1: $path = drupal_get_path_alias($_GET['q']); webmaster@1: // Compare with the internal and path alias (if any). webmaster@1: $page_match = drupal_match_path($path, $block->pages); webmaster@1: if ($path != $_GET['q']) { webmaster@1: $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages); webmaster@1: } webmaster@1: // When $block->visibility has a value of 0, the block is displayed on webmaster@1: // all pages except those listed in $block->pages. When set to 1, it webmaster@1: // is displayed only on those pages listed in $block->pages. webmaster@1: $page_match = !($block->visibility xor $page_match); webmaster@1: } webmaster@1: else { webmaster@1: $page_match = drupal_eval($block->pages); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: $page_match = TRUE; webmaster@1: } webmaster@5: $block->enabled = $enabled; webmaster@5: $block->page_match = $page_match; webmaster@5: $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; webmaster@5: } webmaster@5: } webmaster@1: webmaster@5: // Create an empty array if there were no entries webmaster@5: if (!isset($blocks[$region])) { webmaster@5: $blocks[$region] = array(); webmaster@5: } webmaster@5: webmaster@5: foreach ($blocks[$region] as $key => $block) { webmaster@5: // Render the block content if it has not been created already. webmaster@5: if (!isset($block->content)) { webmaster@5: // Erase the block from the static array - we'll put it back if it has content. webmaster@5: unset($blocks[$region][$key]); webmaster@5: if ($block->enabled && $block->page_match) { webmaster@1: // Check the current throttle status and see if block should be displayed webmaster@1: // based on server load. webmaster@1: if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) { webmaster@1: // Try fetching the block from cache. Block caching is not compatible with webmaster@1: // node_access modules. We also preserve the submission of forms in blocks, webmaster@1: // by fetching from cache only if the request method is 'GET'. webmaster@1: if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) { webmaster@1: $array = $cache->data; webmaster@1: } webmaster@1: else { webmaster@1: $array = module_invoke($block->module, 'block', 'view', $block->delta); webmaster@1: if (isset($cid)) { webmaster@1: cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if (isset($array) && is_array($array)) { webmaster@1: foreach ($array as $k => $v) { webmaster@1: $block->$k = $v; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: if (isset($block->content) && $block->content) { webmaster@1: // Override default block title if a custom display title is present. webmaster@1: if ($block->title) { webmaster@1: // Check plain here to allow module generated titles to keep any markup. webmaster@1: $block->subject = $block->title == '' ? '' : check_plain($block->title); webmaster@1: } webmaster@1: if (!isset($block->subject)) { webmaster@1: $block->subject = ''; webmaster@1: } webmaster@1: $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: return $blocks[$region]; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Assemble the cache_id to use for a given block. webmaster@1: * webmaster@1: * The cache_id string reflects the viewing context for the current block webmaster@1: * instance, obtained by concatenating the relevant context information webmaster@1: * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_* webmaster@1: * constants). Two block instances can use the same cached content when webmaster@1: * they share the same cache_id. webmaster@1: * webmaster@1: * Theme and language contexts are automatically differenciated. webmaster@1: * webmaster@1: * @param $block webmaster@1: * @return webmaster@1: * The string used as cache_id for the block. webmaster@1: */ webmaster@1: function _block_get_cache_id($block) { webmaster@1: global $theme, $base_root, $user; webmaster@1: webmaster@1: // User 1 being out of the regular 'roles define permissions' schema, webmaster@1: // it brings too many chances of having unwanted output get in the cache webmaster@1: // and later be served to other users. We therefore exclude user 1 from webmaster@1: // block caching. webmaster@1: if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) { webmaster@1: $cid_parts = array(); webmaster@1: webmaster@1: // Start with common sub-patterns: block identification, theme, language. webmaster@1: $cid_parts[] = $block->module; webmaster@1: $cid_parts[] = $block->delta; webmaster@1: $cid_parts[] = $theme; webmaster@1: if (module_exists('locale')) { webmaster@1: global $language; webmaster@1: $cid_parts[] = $language->language; webmaster@1: } webmaster@1: webmaster@1: // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a webmaster@1: // resource drag for sites with many users, so when a module is being webmaster@1: // equivocal, we favor the less expensive 'PER_ROLE' pattern. webmaster@1: if ($block->cache & BLOCK_CACHE_PER_ROLE) { webmaster@1: $cid_parts[] = 'r.'. implode(',', array_keys($user->roles)); webmaster@1: } webmaster@1: elseif ($block->cache & BLOCK_CACHE_PER_USER) { webmaster@1: $cid_parts[] = "u.$user->uid"; webmaster@1: } webmaster@1: webmaster@1: if ($block->cache & BLOCK_CACHE_PER_PAGE) { webmaster@1: $cid_parts[] = $base_root . request_uri(); webmaster@1: } webmaster@1: webmaster@1: return implode(':', $cid_parts); webmaster@1: } webmaster@1: }