2001-03-10 11:07:52 +00:00
<?php
2001-10-20 18:57:09 +00:00
// $Id$
2000-12-23 15:20:10 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Controls the boxes that are displayed around the main content.
*/
2008-05-26 17:12:55 +00:00
/**
* Denotes that a block is not enabled in any region and should not be shown.
*/
2006-11-08 19:27:59 +00:00
define('BLOCK_REGION_NONE', -1);
2007-08-19 08:08:45 +00:00
/**
* Constants defining cache granularity for blocks.
*
* Modules specify the caching patterns for their blocks using binary
2008-12-16 23:57:33 +00:00
* combinations of these constants in their hook_block_list():
2007-08-19 08:08:45 +00:00
* $block[delta]['cache'] = BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE;
* BLOCK_CACHE_PER_ROLE is used as a default when no caching pattern is
* specified.
*
* The block cache is cleared in cache_clear_all(), and uses the same clearing
* policy than page cache (node, comment, user, taxonomy added or updated...).
* Blocks requiring more fine-grained clearing might consider disabling the
* built-in block cache (BLOCK_NO_CACHE) and roll their own.
*
* Note that user 1 is excluded from block caching.
*/
/**
* The block should not get cached. This setting should be used:
* - for simple blocks (notably those that do not perform any db query),
* where querying the db cache would be more expensive than directly generating
* the content.
* - for blocks that change too frequently.
*/
define('BLOCK_NO_CACHE', -1);
/**
2007-12-08 14:06:23 +00:00
* The block can change depending on the roles the user viewing the page belongs to.
* This is the default setting, used when the block does not specify anything.
*/
2007-08-19 08:08:45 +00:00
define('BLOCK_CACHE_PER_ROLE', 0x0001);
/**
2007-12-08 14:06:23 +00:00
* The block can change depending on the user viewing the page.
* This setting can be resource-consuming for sites with large number of users,
* and thus should only be used when BLOCK_CACHE_PER_ROLE is not sufficient.
*/
2007-08-19 08:08:45 +00:00
define('BLOCK_CACHE_PER_USER', 0x0002);
/**
2007-12-08 14:06:23 +00:00
* The block can change depending on the page being viewed.
*/
2007-08-19 08:08:45 +00:00
define('BLOCK_CACHE_PER_PAGE', 0x0004);
/**
* The block is the same for every user on every page where it is visible.
*/
define('BLOCK_CACHE_GLOBAL', 0x0008);
2004-05-11 20:10:14 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_help().
2004-05-11 20:10:14 +00:00
*/
2007-06-30 19:46:58 +00:00
function block_help($path, $arg) {
switch ($path) {
2003-10-09 18:53:22 +00:00
case 'admin/help#block':
2009-07-20 18:51:36 +00:00
$output = '<p>' . 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 <a href="@blocks">blocks administration page</a> 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/structure/block'))) . '</p>';
2009-03-08 21:25:18 +00:00
$output .= '<p>' . t('Although blocks are usually generated automatically by modules (like the <em>User login</em> 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 <a href="@text-format">text format</a>.', array('@text-format' => url('admin/settings/filter'))) . '</p>';
2008-04-14 17:48:46 +00:00
$output .= '<p>' . t('When working with blocks, remember that:') . '</p>';
$output .= '<ul><li>' . t('since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis.') . '</li>';
$output .= '<li>' . t('disabled blocks, or blocks not in a region, are never shown.') . '</li>';
$output .= '<li>' . t('blocks can be configured to be visible only on certain pages.') . '</li>';
$output .= '<li>' . t('blocks can be configured to be visible only when specific conditions are true.') . '</li>';
$output .= '<li>' . t('blocks can be configured to be visible only for certain user roles.') . '</li>';
$output .= '<li>' . t('when allowed by an administrator, specific blocks may be enabled or disabled on a per-user basis using the <em>My account</em> page.') . '</li>';
$output .= '<li>' . t('some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.') . '</li></ul>';
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@block">Block module</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/')) . '</p>';
2005-11-01 10:17:34 +00:00
return $output;
2009-07-20 18:51:36 +00:00
case 'admin/structure/block':
2009-08-02 15:44:08 +00:00
$output = '<p>' . 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. 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 <em>Save blocks</em> button at the bottom of the page.') . '</p>';
2009-07-20 18:51:36 +00:00
$output .= '<p>' . t('Click the <em>configure</em> link next to each block to configure its specific title and visibility settings. Use the <a href="@add-block">add block page</a> to create a custom block.', array('@add-block' => url('admin/structure/block/add'))) . '</p>';
2007-12-18 16:38:21 +00:00
return $output;
2009-07-20 18:51:36 +00:00
case 'admin/structure/block/add':
return '<p>' . 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 <a href="@blocks">blocks administration page</a> to be visible.', array('@blocks' => url('admin/structure/block'))) . '</p>';
2003-08-05 18:33:39 +00:00
}
2001-01-13 16:33:19 +00:00
}
2007-04-06 13:27:23 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_theme().
2007-04-06 13:27:23 +00:00
*/
function block_theme() {
return array(
2009-04-26 01:15:04 +00:00
'block' => array(
2009-06-12 09:02:55 +00:00
'arguments' => array('elements' => NULL),
2009-04-26 01:15:04 +00:00
'template' => 'block',
2009-05-24 17:39:35 +00:00
),
2007-10-05 09:35:09 +00:00
'block_admin_display_form' => array(
'template' => 'block-admin-display-form',
2009-05-21 23:07:16 +00:00
'file' => 'block.admin.inc',
2007-04-06 13:27:23 +00:00
'arguments' => array('form' => NULL),
),
);
}
2004-05-11 20:10:14 +00:00
/**
2009-07-05 18:00:11 +00:00
* Implement hook_permission().
2004-05-11 20:10:14 +00:00
*/
2009-07-05 18:00:11 +00:00
function block_permission() {
2008-02-20 13:46:43 +00:00
return array(
2008-10-09 15:15:55 +00:00
'administer blocks' => array(
'title' => t('Administer blocks'),
'description' => t('Select which blocks are displayed, and arrange them on the page.'),
),
2008-02-20 13:46:43 +00:00
);
2001-06-29 22:08:57 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_menu().
2004-04-21 13:56:38 +00:00
*/
2007-01-24 14:48:36 +00:00
function block_menu() {
2009-07-20 18:51:36 +00:00
$items['admin/structure/block'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Blocks',
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
2007-10-05 09:35:09 +00:00
'page callback' => 'block_admin_display',
2007-01-24 14:48:36 +00:00
'access arguments' => array('administer blocks'),
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/list'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'List',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/list/js'] = array(
2007-12-19 21:40:05 +00:00
'title' => 'JavaScript List Form',
2007-10-05 09:35:09 +00:00
'page callback' => 'block_admin_display_js',
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2007-10-05 09:35:09 +00:00
'type' => MENU_CALLBACK,
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/configure'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Configure block',
2007-10-05 12:57:20 +00:00
'page callback' => 'drupal_get_form',
2007-01-24 14:48:36 +00:00
'page arguments' => array('block_admin_configure'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/delete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Delete block',
2007-10-05 12:57:20 +00:00
'page callback' => 'drupal_get_form',
2007-01-24 14:48:36 +00:00
'page arguments' => array('block_box_delete'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add block',
2007-10-05 09:35:09 +00:00
'page callback' => 'drupal_get_form',
2007-01-24 14:48:36 +00:00
'page arguments' => array('block_add_block_form'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
);
$default = variable_get('theme_default', 'garland');
foreach (list_themes() as $key => $theme) {
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/list/' . $key] = array(
2007-12-19 19:09:52 +00:00
'title' => check_plain($theme->info['name']),
'page arguments' => array($key),
'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
'weight' => $key == $default ? -10 : 0,
'access callback' => '_block_themes_access',
'access arguments' => array($theme),
);
2004-09-16 07:17:56 +00:00
}
2004-06-18 15:04:37 +00:00
return $items;
2001-06-20 20:00:40 +00:00
}
2007-12-19 19:09:52 +00:00
/**
2008-05-15 21:30:02 +00:00
* Menu item access callback - only admin or enabled themes can be accessed.
2007-12-19 19:09:52 +00:00
*/
function _block_themes_access($theme) {
2009-05-31 06:31:00 +00:00
$admin_theme = variable_get('admin_theme');
return user_access('administer blocks') && ($theme->status || ($admin_theme && ($theme->name == $admin_theme)));
2007-12-19 19:09:52 +00:00
}
2004-05-11 20:10:14 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_list().
2004-05-11 20:10:14 +00:00
*/
2008-12-16 23:57:33 +00:00
function block_block_list() {
$blocks = array();
2004-10-31 07:34:47 +00:00
2008-12-16 23:57:33 +00:00
$result = db_query('SELECT bid, info FROM {box} ORDER BY info');
2009-05-28 11:31:20 +00:00
foreach ($result as $block) {
2008-12-16 23:57:33 +00:00
$blocks[$block->bid]['info'] = $block->info;
// Not worth caching.
$blocks[$block->bid]['cache'] = BLOCK_NO_CACHE;
}
return $blocks;
}
2004-10-31 07:34:47 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_configure().
2008-12-16 23:57:33 +00:00
*/
function block_block_configure($delta = 0, $edit = array()) {
$box = array('format' => FILTER_FORMAT_DEFAULT);
if ($delta) {
$box = block_box_get($delta);
}
2009-05-24 07:13:12 +00:00
return block_box_form($box);
2002-05-02 19:13:45 +00:00
}
2008-12-16 23:57:33 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_save().
2008-12-16 23:57:33 +00:00
*/
function block_block_save($delta = 0, $edit = array()) {
block_box_save($edit, $delta);
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_view().
2008-12-16 23:57:33 +00:00
*
* Generates the administrator-defined blocks for display.
*/
function block_block_view($delta = 0, $edit = array()) {
2009-05-28 11:31:20 +00:00
$block = db_query('SELECT body, format FROM {box} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
2008-12-16 23:57:33 +00:00
$data['content'] = check_markup($block->body, $block->format, '', FALSE);
return $data;
}
2009-01-27 00:22:27 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_page_alter().
2009-01-27 00:22:27 +00:00
*
* Render blocks into their regions.
*/
function block_page_alter($page) {
global $theme;
// The theme system might not yet be initialized. We need $theme.
2009-07-14 10:22:17 +00:00
drupal_theme_initialize();
2009-01-27 00:22:27 +00:00
// Populate all block regions
2009-07-18 02:36:01 +00:00
$all_regions = system_region_list($theme);
2009-01-27 00:22:27 +00:00
// Load all region content assigned via blocks.
2009-07-18 02:36:01 +00:00
foreach (array_keys($all_regions) as $region) {
2009-08-03 03:04:34 +00:00
// Prevent sidebar regions from rendering blocks when 'show_blocks' == FALSE.
if (!empty($page['#show_blocks']) || (strpos($region, 'sidebar_') !== 0)) {
2009-01-27 00:22:27 +00:00
// Assign blocks to region.
if ($blocks = block_get_blocks_by_region($region)) {
2009-06-12 09:02:55 +00:00
$page[$region] = $blocks;
2009-01-27 00:22:27 +00:00
}
// Append region description if we are rendering the block admin page.
$item = menu_get_item();
2009-08-11 20:18:28 +00:00
if ($item['path'] == 'admin/structure/block') {
$visible_regions = system_region_list($theme, REGIONS_VISIBLE);
if (isset($visible_regions[$region])) {
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
$page[$region]['block_description'] = array(
'#markup' => $description,
'#weight' => 15,
);
}
2009-01-27 00:22:27 +00:00
}
}
}
}
/**
* Get a renderable array of a region containing all enabled blocks.
*
* @param $region
* The requested region.
*/
function block_get_blocks_by_region($region) {
$weight = 0;
$build = array();
if ($list = block_list($region)) {
foreach ($list as $key => $block) {
2009-06-12 09:02:55 +00:00
$build[$key] = $block->content;
unset($block->content);
$build[$key] += array(
2009-08-04 06:38:57 +00:00
'#theme_wrappers' => array('block'),
2009-01-27 00:22:27 +00:00
'#block' => $block,
'#weight' => ++$weight,
);
}
$build['#sorted'] = TRUE;
}
return $build;
}
2002-10-30 08:12:28 +00:00
/**
2008-11-15 08:23:07 +00:00
* Update the 'block' DB table with the blocks currently exported by modules.
2002-11-01 10:47:20 +00:00
*
2004-05-11 20:10:14 +00:00
* @return
2006-04-04 23:32:40 +00:00
* Blocks currently exported by modules.
2002-10-30 08:12:28 +00:00
*/
2006-04-04 23:32:40 +00:00
function _block_rehash() {
2005-08-16 18:06:18 +00:00
global $theme_key;
2009-07-14 10:22:17 +00:00
drupal_theme_initialize();
2005-08-16 18:06:18 +00:00
2007-11-06 11:40:15 +00:00
$old_blocks = array();
2009-05-28 11:31:20 +00:00
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme_key));
foreach ($result as $old_block) {
$old_block = is_object($old_block) ? get_object_vars($old_block) : $old_block;
2007-11-06 11:40:15 +00:00
$old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
2002-10-26 15:17:26 +00:00
}
2006-08-20 06:49:15 +00:00
$blocks = array();
2008-01-25 10:56:59 +00:00
// Valid region names for the theme.
$regions = system_region_list($theme_key);
2002-10-26 15:17:26 +00:00
2008-12-16 23:57:33 +00:00
foreach (module_implements('block_list') as $module) {
$module_blocks = module_invoke($module, 'block_list');
2002-10-26 15:17:26 +00:00
if ($module_blocks) {
foreach ($module_blocks as $delta => $block) {
2007-11-06 11:40:15 +00:00
if (empty($old_blocks[$module][$delta])) {
// If it's a new block, add identifiers.
$block['module'] = $module;
$block['delta'] = $delta;
$block['theme'] = $theme_key;
2007-11-07 21:13:31 +00:00
if (!isset($block['pages'])) {
// {block}.pages is type 'text', so it cannot have a
// default value, and not null, so we need to provide
// value if the module did not.
$block['pages'] = '';
}
2007-11-06 11:40:15 +00:00
// Add defaults and save it into the database.
2008-11-15 08:23:07 +00:00
drupal_write_record('block', $block);
2007-11-14 09:50:00 +00:00
// Set region to none if not enabled.
$block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
// Add to the list of blocks we return.
2007-11-06 11:40:15 +00:00
$blocks[] = $block;
2002-10-26 15:17:26 +00:00
}
else {
2007-11-06 11:40:15 +00:00
// If it's an existing block, database settings should overwrite
// the code. But aside from 'info' everything that's definable in
// code is stored in the database and we do not store 'info', so we
// do not need to update the database here.
// Add 'info' to this block.
$old_blocks[$module][$delta]['info'] = $block['info'];
2008-01-25 10:56:59 +00:00
// If the region name does not exist, disable the block and assign it to none.
if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) {
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');
$old_blocks[$module][$delta]['status'] = 0;
$old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
}
else {
$old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE;
}
2007-11-14 09:50:00 +00:00
// Add this block to the list of blocks we return.
2007-11-06 11:40:15 +00:00
$blocks[] = $old_blocks[$module][$delta];
// Remove this block from the list of blocks to be deleted.
unset($old_blocks[$module][$delta]);
2002-10-26 15:17:26 +00:00
}
}
}
}
2007-11-06 11:40:15 +00:00
// Remove blocks that are no longer defined by the code from the database.
foreach ($old_blocks as $module => $old_module_blocks) {
foreach ($old_module_blocks as $delta => $block) {
2009-05-28 11:31:20 +00:00
db_delete('block')
->condition('module', $module)
->condition('delta', $delta)
->condition('theme', $theme_key)
->execute();
2007-11-06 11:40:15 +00:00
}
2006-08-20 06:49:15 +00:00
}
2002-10-26 15:17:26 +00:00
return $blocks;
}
2001-01-26 13:38:46 +00:00
2002-05-02 19:13:45 +00:00
function block_box_get($bid) {
2009-05-28 11:31:20 +00:00
return db_query("SELECT * FROM {box} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
2004-05-11 20:10:14 +00:00
}
2007-01-23 16:44:58 +00:00
/**
* Define the custom block form.
*/
2002-05-02 19:13:45 +00:00
function block_box_form($edit = array()) {
2007-03-27 05:13:55 +00:00
$edit += array(
'info' => '',
'body' => '',
);
2006-01-24 08:38:29 +00:00
$form['info'] = array(
'#type' => 'textfield',
'#title' => t('Block description'),
2007-01-25 22:14:06 +00:00
'#default_value' => $edit['info'],
2006-01-24 08:38:29 +00:00
'#maxlength' => 64,
2009-07-20 18:51:36 +00:00
'#description' => t('A brief description of your block. Used on the <a href="@overview">blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
2006-01-24 08:38:29 +00:00
'#required' => TRUE,
'#weight' => -19,
);
2007-04-09 13:58:03 +00:00
$form['body_field']['#weight'] = -17;
$form['body_field']['body'] = array(
2005-12-05 09:11:33 +00:00
'#type' => 'textarea',
'#title' => t('Block body'),
2007-01-25 22:14:06 +00:00
'#default_value' => $edit['body'],
2009-01-21 16:58:42 +00:00
'#text_format' => isset($edit['format']) ? $edit['format'] : FILTER_FORMAT_DEFAULT,
2005-12-05 09:11:33 +00:00
'#rows' => 15,
'#description' => t('The content of the block as shown to the user.'),
2009-03-17 03:42:30 +00:00
'#required' => TRUE,
2005-12-05 09:11:33 +00:00
'#weight' => -17,
2009-05-24 07:13:12 +00:00
'#access' => filter_access($edit['format']),
2005-12-05 09:11:33 +00:00
);
2002-05-02 19:13:45 +00:00
2005-10-07 06:11:12 +00:00
return $form;
2002-05-02 19:13:45 +00:00
}
2007-01-23 16:44:58 +00:00
function block_box_save($edit, $delta) {
2009-05-28 11:31:20 +00:00
db_update('box')
->fields(array(
'body' => $edit['body'],
'info' => $edit['info'],
'format' => $edit['body_format'],
))
->condition('bid', $delta)
->execute();
2006-05-26 09:21:10 +00:00
return TRUE;
2002-05-02 19:13:45 +00:00
}
2004-05-11 20:10:14 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_user_form().
2004-05-11 20:10:14 +00:00
*/
2009-08-12 12:36:05 +00:00
function block_user_form(&$edit, $account, $category) {
2008-10-06 11:30:12 +00:00
if ($category == 'account') {
$rids = array_keys($account->roles);
2009-04-26 16:30:28 +00:00
$result = db_query("SELECT DISTINCT b.* FROM {block} b LEFT JOIN {block_role} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom <> 0 AND (r.rid IN (:rids) OR r.rid IS NULL) ORDER BY b.weight, b.module", array(':rids' => $rids));
2009-06-08 04:55:34 +00:00
$form['block'] = array(
'#type' => 'fieldset',
'#title' => t('Personalize blocks'),
'#description' => t('Blocks consist of content or information that complements the main content of the page. Enable or disable optional blocks using the checkboxes below.'),
'#weight' => 3,
'#collapsible' => TRUE,
'#tree' => TRUE
);
2009-05-28 11:31:20 +00:00
foreach ($result as $block) {
2008-12-16 23:57:33 +00:00
$data = module_invoke($block->module, 'block_list');
2008-10-06 11:30:12 +00:00
if ($data[$block->delta]['info']) {
$return = TRUE;
2009-05-28 11:31:20 +00:00
$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),
);
2002-05-02 19:13:45 +00:00
}
2008-10-06 11:30:12 +00:00
}
2002-11-16 14:28:22 +00:00
2008-10-06 11:30:12 +00:00
if (!empty($return)) {
return $form;
}
}
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_user_validate().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function block_user_validate(&$edit, $account, $category) {
2008-10-06 11:30:12 +00:00
if (empty($edit['block'])) {
$edit['block'] = array();
2002-05-02 19:13:45 +00:00
}
2008-10-06 11:30:12 +00:00
return $edit;
2002-05-02 19:13:45 +00:00
}
2009-04-21 09:31:31 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_form_FORM_ID_alter().
2009-04-21 09:31:31 +00:00
*/
function block_form_system_themes_form_alter(&$form, &$form_state) {
2009-05-31 07:46:54 +00:00
// This function needs to fire before the theme changes are recorded in the
// database, otherwise it will populate the default list of blocks from the
// new theme, which is empty.
array_unshift($form['#submit'], 'block_system_themes_form_submit');
2009-04-21 09:31:31 +00:00
}
/**
* Initialize blocks for enabled themes.
*/
function block_system_themes_form_submit(&$form, &$form_state) {
if ($form_state['values']['op'] == t('Save configuration')) {
if (is_array($form_state['values']['status'])) {
foreach ($form_state['values']['status'] as $key => $choice) {
if ($choice || $form_state['values']['theme_default'] == $key) {
2009-07-14 10:22:17 +00:00
block_theme_initialize($key);
2009-04-21 09:31:31 +00:00
}
}
}
2009-08-11 14:16:55 +00:00
if ($form_state['values']['admin_theme'] && $form_state['values']['admin_theme'] !== variable_get('admin_theme', 0)) {
2009-04-21 09:31:31 +00:00
// If we're changing themes, make sure the theme has its blocks initialized.
2009-05-16 15:23:16 +00:00
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $form_state['values']['admin_theme']), 0, 1)->fetchField();
if (!$has_blocks) {
2009-07-14 10:22:17 +00:00
block_theme_initialize($form_state['values']['admin_theme']);
2009-04-21 09:31:31 +00:00
}
}
}
}
/**
* Assign an initial, default set of blocks for a theme.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* This function is called the first time a new theme is enabled. The new theme
* gets a copy of the default theme's blocks, with the difference that if a
* particular region isn't available in the new theme, the block is assigned
* to the new theme's default region.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* @param $theme
* The name of a theme.
*/
2009-07-14 10:22:17 +00:00
function block_theme_initialize($theme) {
2009-04-21 09:31:31 +00:00
// Initialize theme's blocks if none already registered.
2009-05-16 15:23:16 +00:00
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $theme), 0, 1)->fetchField();
if (!$has_blocks) {
2009-04-21 09:31:31 +00:00
$default_theme = variable_get('theme_default', 'garland');
$regions = system_region_list($theme);
2009-05-28 11:31:20 +00:00
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $default_theme), array('fetch' => PDO::FETCH_ASSOC));
$query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'custom', 'cache'));
foreach ($result as $block) {
2009-04-21 09:31:31 +00:00
// If the region isn't supported by the theme, assign the block to the theme's default region.
if (!array_key_exists($block['region'], $regions)) {
$block['region'] = system_default_region($theme);
}
2009-05-28 11:31:20 +00:00
$block['theme'] = $theme;
$query->values($block);
2009-04-21 09:31:31 +00:00
}
2009-05-28 11:31:20 +00:00
$query->execute();
2009-04-21 09:31:31 +00:00
}
}
2005-08-16 18:06:18 +00:00
/**
* Return all blocks in the specified region for the current user.
*
* @param $region
* The name of a region.
*
* @return
* An array of block objects, indexed with <i>module</i>_<i>delta</i>.
* If you are displaying your blocks in one or two sidebars, you may check
* whether this array is empty to see how many columns are going to be
* displayed.
*
* @todo
2007-10-06 16:08:50 +00:00
* Now that the blocks table has a primary key, we should use that as the
* array key instead of <i>module</i>_<i>delta</i>.
2005-08-16 18:06:18 +00:00
*/
function block_list($region) {
2009-06-07 02:29:07 +00:00
$blocks = &drupal_static(__FUNCTION__, array());
2003-11-19 16:13:07 +00:00
2009-06-07 02:29:07 +00:00
if (empty($blocks)) {
2008-03-21 08:41:25 +00:00
$blocks = _block_load_blocks();
}
2008-05-15 21:30:02 +00:00
// Create an empty array if there were no entries.
2008-03-21 08:41:25 +00:00
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
$blocks[$region] = _block_render_blocks($blocks[$region]);
return $blocks[$region];
}
/**
2008-05-15 21:30:02 +00:00
* Load blocks information from the database.
2008-03-21 08:41:25 +00:00
*/
function _block_load_blocks() {
2009-06-08 09:23:55 +00:00
global $theme_key;
2008-03-21 08:41:25 +00:00
2009-05-28 11:31:20 +00:00
$query = db_select('block', 'b');
$result = $query
->fields('b')
->condition('b.theme', $theme_key)
->condition('b.status', 1)
->orderBy('b.region')
->orderBy('b.weight')
->orderBy('b.module')
->addTag('block_load')
->execute();
2009-06-08 09:23:55 +00:00
$block_list = $result->fetchAllAssoc('bid');
// Allow modules to modify the block list.
drupal_alter('block_list', $block_list);
$blocks = array();
foreach ($block_list as $block) {
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
return $blocks;
}
/**
* Implement hook_block_list_alter().
*
2009-07-01 08:04:19 +00:00
* Check the page, user role, content type and user specific visibilty settings.
* Remove the block if the visibility conditions are not met.
2009-06-08 09:23:55 +00:00
*/
function block_block_list_alter(&$blocks) {
global $user, $theme_key;
// Build an array of roles for each block.
$block_roles = array();
$result = db_query('SELECT module, delta, rid FROM {block_role}');
foreach ($result as $record) {
$block_roles[$record->module][$record->delta][] = $record->rid;
}
2009-07-01 08:04:19 +00:00
// Build an array of node types for each block.
$block_node_types = array();
$result = db_query('SELECT module, delta, type FROM {block_node_type}');
foreach ($result as $record) {
$block_node_types[$record->module][$record->delta][] = $record->type;
}
2009-06-08 09:23:55 +00:00
foreach ($blocks as $key => $block) {
if ($block->theme != $theme_key || $block->status != 1) {
// This block was added by a contrib module, leave it in the list.
continue;
2008-03-21 08:41:25 +00:00
}
2009-06-08 09:23:55 +00:00
// If a block has no roles associated, it is displayed for every role.
// For blocks with roles associated, if none of the user's roles matches
// the settings from this block, remove it from the block list.
2009-07-01 08:04:19 +00:00
if (isset($block_roles[$block->module][$block->delta]) && !array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
2009-06-08 09:23:55 +00:00
// No match.
unset($blocks[$key]);
continue;
2009-07-01 08:04:19 +00:00
}
// If a block has no node types associated, it is displayed for every type.
// For blocks with node types associated, if the node type does not match
// the settings from this block, remove it from the block list.
if (isset($block_node_types[$block->module][$block->delta])) {
$node = menu_get_object();
if (!empty($node)) {
// This is a node or node edit page.
if (!in_array($node->type, $block_node_types[$block->module][$block->delta])) {
// This block should not be displayed for this node type.
unset($blocks[$key]);
continue;
}
}
elseif (arg(0) == 'node' && arg(1) == 'add' && in_array(arg(2), array_keys(node_type_get_types()))) {
// This is a node creation page
if (!in_array(arg(2), $block_node_types[$block->module][$block->delta])) {
// This block should not be displayed for this node type.
unset($blocks[$key]);
continue;
}
}
else {
// This is not a node page, remove the block.
unset($blocks[$key]);
continue;
}
}
2009-06-08 09:23:55 +00:00
2008-05-15 21:30:02 +00:00
// Use the user's block visibility setting, if necessary.
2008-03-21 08:41:25 +00:00
if ($block->custom != 0) {
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
$enabled = $user->block[$block->module][$block->delta];
2004-10-31 07:34:47 +00:00
}
else {
2008-03-21 08:41:25 +00:00
$enabled = ($block->custom == 1);
2004-02-25 21:19:31 +00:00
}
2008-03-21 08:41:25 +00:00
}
else {
$enabled = TRUE;
}
2009-06-08 09:23:55 +00:00
if (!$enabled) {
unset($blocks[$key]);
continue;
}
2004-08-06 11:10:15 +00:00
2008-05-15 21:30:02 +00:00
// Match path if necessary.
2008-03-21 08:41:25 +00:00
if ($block->pages) {
if ($block->visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $block->pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
2005-01-27 19:41:01 +00:00
}
2008-03-21 08:41:25 +00:00
// When $block->visibility has a value of 0, the block is displayed on
// all pages except those listed in $block->pages. When set to 1, it
// is displayed only on those pages listed in $block->pages.
$page_match = !($block->visibility xor $page_match);
2004-08-06 11:10:15 +00:00
}
2009-05-07 15:29:08 +00:00
elseif (module_exists('php')) {
$page_match = php_eval($block->pages);
}
2004-08-06 11:10:15 +00:00
else {
2009-05-07 15:29:08 +00:00
$page_match = FALSE;
2004-08-06 11:10:15 +00:00
}
2008-03-21 08:41:25 +00:00
}
else {
$page_match = TRUE;
}
2009-06-08 09:23:55 +00:00
if (!$page_match) {
unset($blocks[$key]);
}
2008-03-21 08:41:25 +00:00
}
}
2005-09-14 21:37:11 +00:00
2008-03-21 08:41:25 +00:00
/**
* Render the content and subject for a set of blocks.
*
* @param $region_blocks
2008-05-15 21:30:02 +00:00
* An array of block objects such as returned for one region by _block_load_blocks().
2008-03-21 08:41:25 +00:00
*
* @return
2008-04-16 11:35:52 +00:00
* An array of visible blocks with subject and content rendered.
2008-03-21 08:41:25 +00:00
*/
function _block_render_blocks($region_blocks) {
foreach ($region_blocks as $key => $block) {
// Render the block content if it has not been created already.
if (!isset($block->content)) {
2009-06-08 09:23:55 +00:00
// Erase the block from the static array - we'll put it back if it has
// content.
2008-03-21 08:41:25 +00:00
unset($region_blocks[$key]);
2009-06-08 09:23:55 +00:00
// Try fetching the block from cache. Block caching is not compatible
// with node_access modules. We also preserve the submission of forms in
// blocks, by fetching from cache only if the request method is 'GET'
// (or 'HEAD').
if (!count(module_implements('node_grants')) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
$array = $cache->data;
}
else {
$array = module_invoke($block->module, 'block_view', $block->delta);
if (isset($cid)) {
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
2008-04-16 11:35:52 +00:00
}
2009-06-08 09:23:55 +00:00
}
2007-08-19 08:08:45 +00:00
2009-06-08 09:23:55 +00:00
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
2003-11-28 20:03:00 +00:00
}
2009-06-08 09:23:55 +00:00
}
if (isset($block->content) && $block->content) {
2009-06-12 09:02:55 +00:00
// Normalize to the drupal_render() structure.
$block->content = array('content' => is_string($block->content) ? array('#markup' => $block->content) : $block->content);
2009-06-08 09:23:55 +00:00
// Override default block title if a custom display title is present.
if ($block->title) {
// Check plain here to allow module generated titles to keep any
// markup.
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
if (!isset($block->subject)) {
$block->subject = '';
2003-11-19 16:13:07 +00:00
}
2009-06-08 09:23:55 +00:00
$region_blocks["{$block->module}_{$block->delta}"] = $block;
2003-11-19 16:13:07 +00:00
}
}
}
2008-03-21 08:41:25 +00:00
return $region_blocks;
2003-11-19 16:13:07 +00:00
}
2007-08-19 08:08:45 +00:00
/**
* Assemble the cache_id to use for a given block.
*
* The cache_id string reflects the viewing context for the current block
* instance, obtained by concatenating the relevant context information
* (user, page, ...) according to the block's cache settings (BLOCK_CACHE_*
* constants). Two block instances can use the same cached content when
* they share the same cache_id.
*
2008-12-30 16:43:20 +00:00
* Theme and language contexts are automatically differentiated.
2007-08-19 08:08:45 +00:00
*
* @param $block
* @return
* The string used as cache_id for the block.
*/
function _block_get_cache_id($block) {
global $theme, $base_root, $user;
// User 1 being out of the regular 'roles define permissions' schema,
// it brings too many chances of having unwanted output get in the cache
// and later be served to other users. We therefore exclude user 1 from
// block caching.
if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) {
$cid_parts = array();
// Start with common sub-patterns: block identification, theme, language.
$cid_parts[] = $block->module;
$cid_parts[] = $block->delta;
$cid_parts[] = $theme;
if (module_exists('locale')) {
global $language;
$cid_parts[] = $language->language;
}
// 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a
// resource drag for sites with many users, so when a module is being
// equivocal, we favor the less expensive 'PER_ROLE' pattern.
if ($block->cache & BLOCK_CACHE_PER_ROLE) {
2008-04-14 17:48:46 +00:00
$cid_parts[] = 'r.' . implode(',', array_keys($user->roles));
2007-08-19 08:08:45 +00:00
}
elseif ($block->cache & BLOCK_CACHE_PER_USER) {
$cid_parts[] = "u.$user->uid";
}
if ($block->cache & BLOCK_CACHE_PER_PAGE) {
$cid_parts[] = $base_root . request_uri();
}
return implode(':', $cid_parts);
}
2007-11-26 16:36:44 +00:00
}
2009-02-03 12:30:14 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_flush_caches().
2009-02-03 12:30:14 +00:00
*/
function block_flush_caches() {
return array('cache_block');
}
2009-04-26 01:15:04 +00:00
/**
* Process variables for block.tpl.php
*
* Prepare the values passed to the theme_block function to be passed
* into a pluggable template engine. Uses block properties to generate a
* series of template file suggestions. If none are found, the default
* block.tpl.php is used.
*
* Most themes utilize their own copy of block.tpl.php. The default is located
* inside "modules/block/block.tpl.php". Look in there for the full list of
* variables.
*
* The $variables array contains the following arguments:
* - $block
*
* @see block.tpl.php
*/
function template_preprocess_block(&$variables) {
2009-06-07 02:29:07 +00:00
$block_counter = &drupal_static(__FUNCTION__, array());
2009-06-12 09:02:55 +00:00
$variables['block'] = $variables['elements']['#block'];
2009-04-26 01:15:04 +00:00
// All blocks get an independent counter for each region.
if (!isset($block_counter[$variables['block']->region])) {
$block_counter[$variables['block']->region] = 1;
}
// Same with zebra striping.
$variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';
$variables['block_id'] = $block_counter[$variables['block']->region]++;
2009-06-12 09:02:55 +00:00
// Create the $content variable that templates expect.
$variables['content'] = $variables['elements']['#children'];
2009-05-21 21:12:25 +00:00
2009-05-28 16:44:07 +00:00
$variables['classes_array'][] = 'block-' . $variables['block']->module;
2009-04-26 01:15:04 +00:00
$variables['template_files'][] = 'block-' . $variables['block']->region;
$variables['template_files'][] = 'block-' . $variables['block']->module;
$variables['template_files'][] = 'block-' . $variables['block']->module . '-' . $variables['block']->delta;
2009-05-28 16:44:07 +00:00
}