| 1 |
<?php
|
| 2 |
// $Id: block.module,v 1.393 2009/10/18 11:42:29 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Controls the visual building blocks a page is constructed with.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Denotes that a block is not enabled in any region and should not be shown.
|
| 11 |
*/
|
| 12 |
define('BLOCK_REGION_NONE', -1);
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implement hook_help().
|
| 16 |
*/
|
| 17 |
function block_help($path, $arg) {
|
| 18 |
switch ($path) {
|
| 19 |
case 'admin/help#block':
|
| 20 |
$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>';
|
| 21 |
$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/config/content/formats'))) . '</p>';
|
| 22 |
$output .= '<p>' . t('When working with blocks, remember that:') . '</p>';
|
| 23 |
$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>';
|
| 24 |
$output .= '<li>' . t('disabled blocks, or blocks not in a region, are never shown.') . '</li>';
|
| 25 |
$output .= '<li>' . t('blocks can be configured to be visible only on certain pages.') . '</li>';
|
| 26 |
$output .= '<li>' . t('blocks can be configured to be visible only when specific conditions are true.') . '</li>';
|
| 27 |
$output .= '<li>' . t('blocks can be configured to be visible only for certain user roles.') . '</li>';
|
| 28 |
$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>';
|
| 29 |
$output .= '<li>' . t('some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.') . '</li></ul>';
|
| 30 |
$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>';
|
| 31 |
return $output;
|
| 32 |
case 'admin/structure/block/add':
|
| 33 |
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>';
|
| 34 |
}
|
| 35 |
if ($arg[0] == 'admin' && $arg[1] == 'structure' && $arg['2'] == 'block' && (empty($arg[3]) || $arg[3] == 'list')) {
|
| 36 |
$demo_theme = !empty($arg[4]) ? $arg[4] : variable_get('theme_default', 'garland');
|
| 37 |
$themes = list_themes();
|
| 38 |
$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. Click the <em>configure</em> link next to each block to configure its specific title and visibility settings.') . '</p>';
|
| 39 |
$output .= '<p>' . l(t('Demonstrate block regions (@theme)', array('@theme' => $themes[$demo_theme]->info['name'])), 'admin/structure/block/demo/' . $demo_theme) . '</p>';
|
| 40 |
return $output;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implement hook_theme().
|
| 46 |
*/
|
| 47 |
function block_theme() {
|
| 48 |
return array(
|
| 49 |
'block' => array(
|
| 50 |
'render element' => 'elements',
|
| 51 |
'template' => 'block',
|
| 52 |
),
|
| 53 |
'block_admin_display_form' => array(
|
| 54 |
'template' => 'block-admin-display-form',
|
| 55 |
'file' => 'block.admin.inc',
|
| 56 |
'render element' => 'form',
|
| 57 |
),
|
| 58 |
);
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Implement hook_permission().
|
| 63 |
*/
|
| 64 |
function block_permission() {
|
| 65 |
return array(
|
| 66 |
'administer blocks' => array(
|
| 67 |
'title' => t('Administer blocks'),
|
| 68 |
'description' => t('Select which blocks are displayed, and arrange them on the page.'),
|
| 69 |
),
|
| 70 |
);
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Implement hook_menu().
|
| 75 |
*/
|
| 76 |
function block_menu() {
|
| 77 |
$default_theme = variable_get('theme_default', 'garland');
|
| 78 |
$items['admin/structure/block'] = array(
|
| 79 |
'title' => 'Blocks',
|
| 80 |
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
|
| 81 |
'page callback' => 'block_admin_display',
|
| 82 |
'page arguments' => array($default_theme),
|
| 83 |
'access arguments' => array('administer blocks'),
|
| 84 |
'file' => 'block.admin.inc',
|
| 85 |
);
|
| 86 |
$items['admin/structure/block/list'] = array(
|
| 87 |
'title' => 'List',
|
| 88 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 89 |
'weight' => -10,
|
| 90 |
);
|
| 91 |
$items['admin/structure/block/manage/%block/%'] = array(
|
| 92 |
'title' => 'Configure block',
|
| 93 |
'page callback' => 'drupal_get_form',
|
| 94 |
'page arguments' => array('block_admin_configure', 4),
|
| 95 |
'load arguments' => array(5),
|
| 96 |
'access arguments' => array('administer blocks'),
|
| 97 |
'file' => 'block.admin.inc',
|
| 98 |
);
|
| 99 |
$items['admin/structure/block/manage/%block/%/configure'] = array(
|
| 100 |
'title' => 'Configure block',
|
| 101 |
'load arguments' => array(5),
|
| 102 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 103 |
'context' => MENU_CONTEXT_INLINE,
|
| 104 |
);
|
| 105 |
$items['admin/structure/block/manage/%block/%/delete'] = array(
|
| 106 |
'title' => 'Delete block',
|
| 107 |
'page callback' => 'drupal_get_form',
|
| 108 |
'page arguments' => array('block_custom_block_delete', 4),
|
| 109 |
'load arguments' => array(5),
|
| 110 |
'access arguments' => array('administer blocks'),
|
| 111 |
'type' => MENU_CALLBACK,
|
| 112 |
'file' => 'block.admin.inc',
|
| 113 |
);
|
| 114 |
$items['admin/structure/block/add'] = array(
|
| 115 |
'title' => 'Add block',
|
| 116 |
'page callback' => 'drupal_get_form',
|
| 117 |
'page arguments' => array('block_add_block_form'),
|
| 118 |
'access arguments' => array('administer blocks'),
|
| 119 |
'type' => MENU_LOCAL_ACTION,
|
| 120 |
'file' => 'block.admin.inc',
|
| 121 |
);
|
| 122 |
foreach (list_themes() as $key => $theme) {
|
| 123 |
$items['admin/structure/block/list/' . $key] = array(
|
| 124 |
'title' => check_plain($theme->info['name']),
|
| 125 |
'page arguments' => array($key),
|
| 126 |
'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
|
| 127 |
'weight' => $key == $default_theme ? -10 : 0,
|
| 128 |
'access callback' => '_block_themes_access',
|
| 129 |
'access arguments' => array($theme),
|
| 130 |
'file' => 'block.admin.inc',
|
| 131 |
);
|
| 132 |
$items['admin/structure/block/demo/' . $key] = array(
|
| 133 |
'title' => check_plain($theme->info['name']),
|
| 134 |
'page callback' => 'block_admin_demo',
|
| 135 |
'page arguments' => array($key),
|
| 136 |
'type' => MENU_CALLBACK,
|
| 137 |
'access callback' => '_block_themes_access',
|
| 138 |
'access arguments' => array($theme),
|
| 139 |
'theme callback' => '_block_custom_theme',
|
| 140 |
'theme arguments' => array($key),
|
| 141 |
'file' => 'block.admin.inc',
|
| 142 |
);
|
| 143 |
}
|
| 144 |
return $items;
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Menu item access callback - only admin or enabled themes can be accessed.
|
| 149 |
*/
|
| 150 |
function _block_themes_access($theme) {
|
| 151 |
return user_access('administer blocks') && drupal_theme_access($theme);
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Theme callback for the block configuration pages.
|
| 156 |
*
|
| 157 |
* @param $theme
|
| 158 |
* The theme whose blocks are being configured. If not set, the default theme
|
| 159 |
* is assumed.
|
| 160 |
* @return
|
| 161 |
* The theme that should be used for the block configuration page, or NULL
|
| 162 |
* to indicate that the default theme should be used.
|
| 163 |
*/
|
| 164 |
function _block_custom_theme($theme = NULL) {
|
| 165 |
// We return exactly what was passed in, to guarantee that the page will
|
| 166 |
// always be displayed using the theme whose blocks are being configured.
|
| 167 |
return $theme;
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Implement hook_block_info().
|
| 172 |
*/
|
| 173 |
function block_block_info() {
|
| 174 |
$blocks = array();
|
| 175 |
|
| 176 |
$result = db_query('SELECT bid, info FROM {block_custom} ORDER BY info');
|
| 177 |
foreach ($result as $block) {
|
| 178 |
$blocks[$block->bid]['info'] = $block->info;
|
| 179 |
// Not worth caching.
|
| 180 |
$blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE;
|
| 181 |
}
|
| 182 |
return $blocks;
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Implement hook_block_configure().
|
| 187 |
*/
|
| 188 |
function block_block_configure($delta = 0) {
|
| 189 |
$custom_block = array('format' => filter_default_format());
|
| 190 |
if ($delta) {
|
| 191 |
$custom_block = block_custom_block_get($delta);
|
| 192 |
}
|
| 193 |
return block_custom_block_form($custom_block);
|
| 194 |
}
|
| 195 |
|
| 196 |
/**
|
| 197 |
* Implement hook_block_save().
|
| 198 |
*/
|
| 199 |
function block_block_save($delta = 0, $edit = array()) {
|
| 200 |
block_custom_block_save($edit, $delta);
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Implement hook_block_view().
|
| 205 |
*
|
| 206 |
* Generates the administrator-defined blocks for display.
|
| 207 |
*/
|
| 208 |
function block_block_view($delta = 0, $edit = array()) {
|
| 209 |
$block = db_query('SELECT body, format FROM {block_custom} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
|
| 210 |
$data['content'] = check_markup($block->body, $block->format, '', TRUE);
|
| 211 |
return $data;
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Implement hook_page_build().
|
| 216 |
*
|
| 217 |
* Render blocks into their regions.
|
| 218 |
*/
|
| 219 |
function block_page_build(&$page) {
|
| 220 |
global $theme;
|
| 221 |
|
| 222 |
// The theme system might not yet be initialized. We need $theme.
|
| 223 |
drupal_theme_initialize();
|
| 224 |
|
| 225 |
// Populate all block regions
|
| 226 |
$all_regions = system_region_list($theme);
|
| 227 |
|
| 228 |
$item = menu_get_item();
|
| 229 |
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
|
| 230 |
// Load all region content assigned via blocks.
|
| 231 |
foreach (array_keys($all_regions) as $region) {
|
| 232 |
$page[$region] = array();
|
| 233 |
// Assign blocks to region.
|
| 234 |
if ($blocks = block_get_blocks_by_region($region)) {
|
| 235 |
$page[$region] = $blocks;
|
| 236 |
}
|
| 237 |
}
|
| 238 |
}
|
| 239 |
else {
|
| 240 |
// Append region description if we are rendering the regions demo page.
|
| 241 |
$item = menu_get_item();
|
| 242 |
if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
|
| 243 |
$visible_regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
|
| 244 |
foreach ($visible_regions as $region) {
|
| 245 |
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
|
| 246 |
$page[$region]['block_description'] = array(
|
| 247 |
'#markup' => $description,
|
| 248 |
'#weight' => 15,
|
| 249 |
);
|
| 250 |
}
|
| 251 |
}
|
| 252 |
}
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
| 256 |
* Get a renderable array of a region containing all enabled blocks.
|
| 257 |
*
|
| 258 |
* @param $region
|
| 259 |
* The requested region.
|
| 260 |
*/
|
| 261 |
function block_get_blocks_by_region($region) {
|
| 262 |
$build = array();
|
| 263 |
if ($list = block_list($region)) {
|
| 264 |
$build = _block_get_renderable_array($list);
|
| 265 |
}
|
| 266 |
return $build;
|
| 267 |
}
|
| 268 |
|
| 269 |
/**
|
| 270 |
* Get an array of blocks suitable for drupal_render().
|
| 271 |
*
|
| 272 |
* @param $list
|
| 273 |
* A list of blocks such as that returned by block_list().
|
| 274 |
* @return
|
| 275 |
* A renderable array.
|
| 276 |
*/
|
| 277 |
function _block_get_renderable_array($list = array()) {
|
| 278 |
$weight = 0;
|
| 279 |
$build = array();
|
| 280 |
foreach ($list as $key => $block) {
|
| 281 |
$build[$key] = $block->content;
|
| 282 |
unset($block->content);
|
| 283 |
|
| 284 |
// Add contextual links for this block; skipping the system main block.
|
| 285 |
if ($key != 'system_main') {
|
| 286 |
$build[$key]['#contextual_links']['block'] = menu_contextual_links('admin/structure/block/manage', array($block->module, $block->delta));
|
| 287 |
}
|
| 288 |
|
| 289 |
$build[$key] += array(
|
| 290 |
'#block' => $block,
|
| 291 |
'#weight' => ++$weight,
|
| 292 |
);
|
| 293 |
$build[$key]['#theme_wrappers'][] ='block';
|
| 294 |
}
|
| 295 |
$build['#sorted'] = TRUE;
|
| 296 |
return $build;
|
| 297 |
}
|
| 298 |
|
| 299 |
/**
|
| 300 |
* Update the 'block' DB table with the blocks currently exported by modules.
|
| 301 |
*
|
| 302 |
* @param $theme
|
| 303 |
* The theme to rehash blocks for. If not provided, defaults to the currently
|
| 304 |
* used theme.
|
| 305 |
*
|
| 306 |
* @return
|
| 307 |
* Blocks currently exported by modules.
|
| 308 |
*/
|
| 309 |
function _block_rehash($theme = NULL) {
|
| 310 |
global $theme_key;
|
| 311 |
|
| 312 |
drupal_theme_initialize();
|
| 313 |
|
| 314 |
if (!isset($theme)) {
|
| 315 |
// If theme is not specifically set, rehash for the current theme.
|
| 316 |
$theme = $theme_key;
|
| 317 |
}
|
| 318 |
|
| 319 |
$old_blocks = array();
|
| 320 |
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme));
|
| 321 |
foreach ($result as $old_block) {
|
| 322 |
$old_block = is_object($old_block) ? get_object_vars($old_block) : $old_block;
|
| 323 |
$old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
|
| 324 |
}
|
| 325 |
|
| 326 |
$blocks = array();
|
| 327 |
// Valid region names for the theme.
|
| 328 |
$regions = system_region_list($theme);
|
| 329 |
|
| 330 |
foreach (module_implements('block_info') as $module) {
|
| 331 |
$module_blocks = module_invoke($module, 'block_info');
|
| 332 |
if ($module_blocks) {
|
| 333 |
foreach ($module_blocks as $delta => $block) {
|
| 334 |
if (empty($old_blocks[$module][$delta])) {
|
| 335 |
// If it's a new block, add identifiers.
|
| 336 |
$block['module'] = $module;
|
| 337 |
$block['delta'] = $delta;
|
| 338 |
$block['theme'] = $theme;
|
| 339 |
if (!isset($block['pages'])) {
|
| 340 |
// {block}.pages is type 'text', so it cannot have a
|
| 341 |
// default value, and not null, so we need to provide
|
| 342 |
// value if the module did not.
|
| 343 |
$block['pages'] = '';
|
| 344 |
}
|
| 345 |
// Add defaults and save it into the database.
|
| 346 |
drupal_write_record('block', $block);
|
| 347 |
// Set region to none if not enabled.
|
| 348 |
$block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
|
| 349 |
// Add to the list of blocks we return.
|
| 350 |
$blocks[] = $block;
|
| 351 |
}
|
| 352 |
else {
|
| 353 |
// If it's an existing block, database settings should overwrite
|
| 354 |
// the code. But aside from 'info' everything that's definable in
|
| 355 |
// code is stored in the database and we do not store 'info', so we
|
| 356 |
// do not need to update the database here.
|
| 357 |
// Add 'info' to this block.
|
| 358 |
$old_blocks[$module][$delta]['info'] = $block['info'];
|
| 359 |
// If the region name does not exist, disable the block and assign it to none.
|
| 360 |
if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) {
|
| 361 |
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');
|
| 362 |
$old_blocks[$module][$delta]['status'] = 0;
|
| 363 |
$old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
|
| 364 |
}
|
| 365 |
else {
|
| 366 |
$old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE;
|
| 367 |
}
|
| 368 |
// Add this block to the list of blocks we return.
|
| 369 |
$blocks[] = $old_blocks[$module][$delta];
|
| 370 |
// Remove this block from the list of blocks to be deleted.
|
| 371 |
unset($old_blocks[$module][$delta]);
|
| 372 |
}
|
| 373 |
}
|
| 374 |
}
|
| 375 |
}
|
| 376 |
|
| 377 |
// Remove blocks that are no longer defined by the code from the database.
|
| 378 |
foreach ($old_blocks as $module => $old_module_blocks) {
|
| 379 |
foreach ($old_module_blocks as $delta => $block) {
|
| 380 |
db_delete('block')
|
| 381 |
->condition('module', $module)
|
| 382 |
->condition('delta', $delta)
|
| 383 |
->condition('theme', $theme)
|
| 384 |
->execute();
|
| 385 |
}
|
| 386 |
}
|
| 387 |
return $blocks;
|
| 388 |
}
|
| 389 |
|
| 390 |
/**
|
| 391 |
* Returns information from database about a user-created (custom) block.
|
| 392 |
*
|
| 393 |
* @param $bid
|
| 394 |
* ID of the block to get information for.
|
| 395 |
* @return
|
| 396 |
* Associative array of information stored in the database for this block.
|
| 397 |
* Array keys:
|
| 398 |
* - bid: Block ID.
|
| 399 |
* - info: Block description.
|
| 400 |
* - body: Block contents.
|
| 401 |
* - format: Filter ID of the filter format for the body.
|
| 402 |
*/
|
| 403 |
function block_custom_block_get($bid) {
|
| 404 |
return db_query("SELECT * FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
|
| 405 |
}
|
| 406 |
|
| 407 |
/**
|
| 408 |
* Define the custom block form.
|
| 409 |
*/
|
| 410 |
function block_custom_block_form($edit = array()) {
|
| 411 |
$edit += array(
|
| 412 |
'info' => '',
|
| 413 |
'body' => '',
|
| 414 |
);
|
| 415 |
$form['info'] = array(
|
| 416 |
'#type' => 'textfield',
|
| 417 |
'#title' => t('Block description'),
|
| 418 |
'#default_value' => $edit['info'],
|
| 419 |
'#maxlength' => 64,
|
| 420 |
'#description' => t('A brief description of your block. Used on the <a href="@overview">blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
|
| 421 |
'#required' => TRUE,
|
| 422 |
'#weight' => -19,
|
| 423 |
);
|
| 424 |
$form['body_field']['#weight'] = -17;
|
| 425 |
$form['body_field']['body'] = array(
|
| 426 |
'#type' => 'textarea',
|
| 427 |
'#title' => t('Block body'),
|
| 428 |
'#default_value' => $edit['body'],
|
| 429 |
'#text_format' => isset($edit['format']) ? $edit['format'] : filter_default_format(),
|
| 430 |
'#rows' => 15,
|
| 431 |
'#description' => t('The content of the block as shown to the user.'),
|
| 432 |
'#required' => TRUE,
|
| 433 |
'#weight' => -17,
|
| 434 |
'#access' => filter_access(filter_format_load($edit['format'])),
|
| 435 |
);
|
| 436 |
|
| 437 |
return $form;
|
| 438 |
}
|
| 439 |
|
| 440 |
/**
|
| 441 |
* Saves a user-created block in the database.
|
| 442 |
*
|
| 443 |
* @param $edit
|
| 444 |
* Associative array of fields to save. Array keys:
|
| 445 |
* - info: Block description.
|
| 446 |
* - body: Block contents.
|
| 447 |
* - format: Filter ID of the filter format for the body.
|
| 448 |
* @param $delta
|
| 449 |
* Block ID of the block to save.
|
| 450 |
* @return
|
| 451 |
* Always returns TRUE.
|
| 452 |
*/
|
| 453 |
function block_custom_block_save($edit, $delta) {
|
| 454 |
db_update('block_custom')
|
| 455 |
->fields(array(
|
| 456 |
'body' => $edit['body'],
|
| 457 |
'info' => $edit['info'],
|
| 458 |
'format' => $edit['body_format'],
|
| 459 |
))
|
| 460 |
->condition('bid', $delta)
|
| 461 |
->execute();
|
| 462 |
return TRUE;
|
| 463 |
}
|
| 464 |
|
| 465 |
/**
|
| 466 |
* Implement hook_form_FORM_ID_alter().
|
| 467 |
*/
|
| 468 |
function block_form_user_profile_form_alter(&$form, &$form_state) {
|
| 469 |
if ($form['#user_category'] == 'account') {
|
| 470 |
$account = $form['#user'];
|
| 471 |
$rids = array_keys($account->roles);
|
| 472 |
$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));
|
| 473 |
|
| 474 |
$blocks = array();
|
| 475 |
foreach ($result as $block) {
|
| 476 |
$data = module_invoke($block->module, 'block_info');
|
| 477 |
if ($data[$block->delta]['info']) {
|
| 478 |
$blocks[$block->module][$block->delta] = array(
|
| 479 |
'#type' => 'checkbox',
|
| 480 |
'#title' => check_plain($data[$block->delta]['info']),
|
| 481 |
'#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1),
|
| 482 |
);
|
| 483 |
}
|
| 484 |
}
|
| 485 |
// Only display the fieldset if there are any personalizable blocks.
|
| 486 |
if ($blocks) {
|
| 487 |
$form['block'] = array(
|
| 488 |
'#type' => 'fieldset',
|
| 489 |
'#title' => t('Personalize blocks'),
|
| 490 |
'#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.'),
|
| 491 |
'#weight' => 3,
|
| 492 |
'#collapsible' => TRUE,
|
| 493 |
'#tree' => TRUE,
|
| 494 |
);
|
| 495 |
$form['block'] += $blocks;
|
| 496 |
}
|
| 497 |
}
|
| 498 |
}
|
| 499 |
|
| 500 |
/**
|
| 501 |
* Implement hook_form_FORM_ID_alter().
|
| 502 |
*/
|
| 503 |
function block_form_system_themes_form_alter(&$form, &$form_state) {
|
| 504 |
// This function needs to fire before the theme changes are recorded in the
|
| 505 |
// database, otherwise it will populate the default list of blocks from the
|
| 506 |
// new theme, which is empty.
|
| 507 |
array_unshift($form['#submit'], 'block_system_themes_form_submit');
|
| 508 |
}
|
| 509 |
|
| 510 |
/**
|
| 511 |
* Initialize blocks for enabled themes.
|
| 512 |
*/
|
| 513 |
function block_system_themes_form_submit(&$form, &$form_state) {
|
| 514 |
if ($form_state['values']['op'] == t('Save configuration')) {
|
| 515 |
if (is_array($form_state['values']['status'])) {
|
| 516 |
foreach ($form_state['values']['status'] as $key => $choice) {
|
| 517 |
if ($choice || $form_state['values']['theme_default'] == $key) {
|
| 518 |
block_theme_initialize($key);
|
| 519 |
}
|
| 520 |
}
|
| 521 |
}
|
| 522 |
if ($form_state['values']['admin_theme'] && $form_state['values']['admin_theme'] !== variable_get('admin_theme', 0)) {
|
| 523 |
// If we're changing themes, make sure the theme has its blocks initialized.
|
| 524 |
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', 0, 1, array(':theme' => $form_state['values']['admin_theme']))->fetchField();
|
| 525 |
if (!$has_blocks) {
|
| 526 |
block_theme_initialize($form_state['values']['admin_theme']);
|
| 527 |
}
|
| 528 |
}
|
| 529 |
}
|
| 530 |
}
|
| 531 |
|
| 532 |
/**
|
| 533 |
* Assign an initial, default set of blocks for a theme.
|
| 534 |
*
|
| 535 |
* This function is called the first time a new theme is enabled. The new theme
|
| 536 |
* gets a copy of the default theme's blocks, with the difference that if a
|
| 537 |
* particular region isn't available in the new theme, the block is assigned
|
| 538 |
* to the new theme's default region.
|
| 539 |
*
|
| 540 |
* @param $theme
|
| 541 |
* The name of a theme.
|
| 542 |
*/
|
| 543 |
function block_theme_initialize($theme) {
|
| 544 |
// Initialize theme's blocks if none already registered.
|
| 545 |
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', 0, 1, array(':theme' => $theme))->fetchField();
|
| 546 |
if (!$has_blocks) {
|
| 547 |
$default_theme = variable_get('theme_default', 'garland');
|
| 548 |
$regions = system_region_list($theme);
|
| 549 |
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $default_theme), array('fetch' => PDO::FETCH_ASSOC));
|
| 550 |
foreach ($result as $block) {
|
| 551 |
// If the region isn't supported by the theme, assign the block to the theme's default region.
|
| 552 |
if (!array_key_exists($block['region'], $regions)) {
|
| 553 |
$block['region'] = system_default_region($theme);
|
| 554 |
}
|
| 555 |
$block['theme'] = $theme;
|
| 556 |
unset($block['bid']);
|
| 557 |
drupal_write_record('block', $block);
|
| 558 |
}
|
| 559 |
}
|
| 560 |
}
|
| 561 |
|
| 562 |
/**
|
| 563 |
* Return all blocks in the specified region for the current user.
|
| 564 |
*
|
| 565 |
* @param $region
|
| 566 |
* The name of a region.
|
| 567 |
*
|
| 568 |
* @return
|
| 569 |
* An array of block objects, indexed with <i>module</i>_<i>delta</i>.
|
| 570 |
* If you are displaying your blocks in one or two sidebars, you may check
|
| 571 |
* whether this array is empty to see how many columns are going to be
|
| 572 |
* displayed.
|
| 573 |
*
|
| 574 |
* @todo
|
| 575 |
* Now that the blocks table has a primary key, we should use that as the
|
| 576 |
* array key instead of <i>module</i>_<i>delta</i>.
|
| 577 |
*/
|
| 578 |
function block_list($region) {
|
| 579 |
$blocks = &drupal_static(__FUNCTION__, array());
|
| 580 |
|
| 581 |
if (empty($blocks)) {
|
| 582 |
$blocks = _block_load_blocks();
|
| 583 |
}
|
| 584 |
|
| 585 |
// Create an empty array if there were no entries.
|
| 586 |
if (!isset($blocks[$region])) {
|
| 587 |
$blocks[$region] = array();
|
| 588 |
}
|
| 589 |
|
| 590 |
$blocks[$region] = _block_render_blocks($blocks[$region]);
|
| 591 |
|
| 592 |
return $blocks[$region];
|
| 593 |
}
|
| 594 |
|
| 595 |
/**
|
| 596 |
* Load a block object from the database.
|
| 597 |
*
|
| 598 |
* @param $module
|
| 599 |
* Name of the module that implements the block to load.
|
| 600 |
* @param $delta
|
| 601 |
* Unique ID of the block within the context of $module.
|
| 602 |
*
|
| 603 |
* @return
|
| 604 |
* A block object.
|
| 605 |
*/
|
| 606 |
function block_load($module, $delta) {
|
| 607 |
$block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta',
|
| 608 |
array(
|
| 609 |
':module' => $module,
|
| 610 |
':delta' => $delta
|
| 611 |
))
|
| 612 |
->fetchObject();
|
| 613 |
|
| 614 |
// If the block does not exist in the database yet return a stub block
|
| 615 |
// object.
|
| 616 |
if (!$block) {
|
| 617 |
$block = new stdClass;
|
| 618 |
$block->module = $module;
|
| 619 |
$block->delta = $delta;
|
| 620 |
}
|
| 621 |
|
| 622 |
return $block;
|
| 623 |
}
|
| 624 |
|
| 625 |
/**
|
| 626 |
* Load blocks information from the database.
|
| 627 |
*/
|
| 628 |
function _block_load_blocks() {
|
| 629 |
global $theme_key;
|
| 630 |
|
| 631 |
$query = db_select('block', 'b');
|
| 632 |
$result = $query
|
| 633 |
->fields('b')
|
| 634 |
->condition('b.theme', $theme_key)
|
| 635 |
->condition('b.status', 1)
|
| 636 |
->orderBy('b.region')
|
| 637 |
->orderBy('b.weight')
|
| 638 |
->orderBy('b.module')
|
| 639 |
->addTag('block_load')
|
| 640 |
->addTag('translatable')
|
| 641 |
->execute();
|
| 642 |
|
| 643 |
$block_info = $result->fetchAllAssoc('bid');
|
| 644 |
// Allow modules to modify the block list.
|
| 645 |
drupal_alter('block_info', $block_info);
|
| 646 |
|
| 647 |
$blocks = array();
|
| 648 |
foreach ($block_info as $block) {
|
| 649 |
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
|
| 650 |
}
|
| 651 |
return $blocks;
|
| 652 |
}
|
| 653 |
|
| 654 |
/**
|
| 655 |
* Implement hook_block_info_alter().
|
| 656 |
*
|
| 657 |
* Check the page, user role, content type and user specific visibilty settings.
|
| 658 |
* Remove the block if the visibility conditions are not met.
|
| 659 |
*/
|
| 660 |
function block_block_info_alter(&$blocks) {
|
| 661 |
global $user, $theme_key;
|
| 662 |
|
| 663 |
// Build an array of roles for each block.
|
| 664 |
$block_roles = array();
|
| 665 |
$result = db_query('SELECT module, delta, rid FROM {block_role}');
|
| 666 |
foreach ($result as $record) {
|
| 667 |
$block_roles[$record->module][$record->delta][] = $record->rid;
|
| 668 |
}
|
| 669 |
|
| 670 |
// Build an array of node types for each block.
|
| 671 |
$block_node_types = array();
|
| 672 |
$result = db_query('SELECT module, delta, type FROM {block_node_type}');
|
| 673 |
foreach ($result as $record) {
|
| 674 |
$block_node_types[$record->module][$record->delta][] = $record->type;
|
| 675 |
}
|
| 676 |
|
| 677 |
foreach ($blocks as $key => $block) {
|
| 678 |
if ($block->theme != $theme_key || $block->status != 1) {
|
| 679 |
// This block was added by a contrib module, leave it in the list.
|
| 680 |
continue;
|
| 681 |
}
|
| 682 |
|
| 683 |
// If a block has no roles associated, it is displayed for every role.
|
| 684 |
// For blocks with roles associated, if none of the user's roles matches
|
| 685 |
// the settings from this block, remove it from the block list.
|
| 686 |
if (isset($block_roles[$block->module][$block->delta]) && !array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
|
| 687 |
// No match.
|
| 688 |
unset($blocks[$key]);
|
| 689 |
continue;
|
| 690 |
}
|
| 691 |
|
| 692 |
// If a block has no node types associated, it is displayed for every type.
|
| 693 |
// For blocks with node types associated, if the node type does not match
|
| 694 |
// the settings from this block, remove it from the block list.
|
| 695 |
if (isset($block_node_types[$block->module][$block->delta])) {
|
| 696 |
$node = menu_get_object();
|
| 697 |
if (!empty($node)) {
|
| 698 |
// This is a node or node edit page.
|
| 699 |
if (!in_array($node->type, $block_node_types[$block->module][$block->delta])) {
|
| 700 |
// This block should not be displayed for this node type.
|
| 701 |
unset($blocks[$key]);
|
| 702 |
continue;
|
| 703 |
}
|
| 704 |
}
|
| 705 |
elseif (arg(0) == 'node' && arg(1) == 'add' && in_array(arg(2), array_keys(node_type_get_types()))) {
|
| 706 |
// This is a node creation page
|
| 707 |
if (!in_array(arg(2), $block_node_types[$block->module][$block->delta])) {
|
| 708 |
// This block should not be displayed for this node type.
|
| 709 |
unset($blocks[$key]);
|
| 710 |
continue;
|
| 711 |
}
|
| 712 |
}
|
| 713 |
else {
|
| 714 |
// This is not a node page, remove the block.
|
| 715 |
unset($blocks[$key]);
|
| 716 |
continue;
|
| 717 |
}
|
| 718 |
}
|
| 719 |
|
| 720 |
// Use the user's block visibility setting, if necessary.
|
| 721 |
if ($block->custom != 0) {
|
| 722 |
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
|
| 723 |
$enabled = $user->block[$block->module][$block->delta];
|
| 724 |
}
|
| 725 |
else {
|
| 726 |
$enabled = ($block->custom == 1);
|
| 727 |
}
|
| 728 |
}
|
| 729 |
else {
|
| 730 |
$enabled = TRUE;
|
| 731 |
}
|
| 732 |
if (!$enabled) {
|
| 733 |
unset($blocks[$key]);
|
| 734 |
continue;
|
| 735 |
}
|
| 736 |
|
| 737 |
// Match path if necessary.
|
| 738 |
if ($block->pages) {
|
| 739 |
if ($block->visibility < 2) {
|
| 740 |
$path = drupal_get_path_alias($_GET['q']);
|
| 741 |
// Compare with the internal and path alias (if any).
|
| 742 |
$page_match = drupal_match_path($path, $block->pages);
|
| 743 |
if ($path != $_GET['q']) {
|
| 744 |
$page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
|
| 745 |
}
|
| 746 |
// When $block->visibility has a value of 0, the block is displayed on
|
| 747 |
// all pages except those listed in $block->pages. When set to 1, it
|
| 748 |
// is displayed only on those pages listed in $block->pages.
|
| 749 |
$page_match = !($block->visibility xor $page_match);
|
| 750 |
}
|
| 751 |
elseif (module_exists('php')) {
|
| 752 |
$page_match = php_eval($block->pages);
|
| 753 |
}
|
| 754 |
else {
|
| 755 |
$page_match = FALSE;
|
| 756 |
}
|
| 757 |
}
|
| 758 |
else {
|
| 759 |
$page_match = TRUE;
|
| 760 |
}
|
| 761 |
if (!$page_match) {
|
| 762 |
unset($blocks[$key]);
|
| 763 |
}
|
| 764 |
}
|
| 765 |
}
|
| 766 |
|
| 767 |
/**
|
| 768 |
* Render the content and subject for a set of blocks.
|
| 769 |
*
|
| 770 |
* @param $region_blocks
|
| 771 |
* An array of block objects such as returned for one region by _block_load_blocks().
|
| 772 |
*
|
| 773 |
* @return
|
| 774 |
* An array of visible blocks as expected by drupal_render().
|
| 775 |
*/
|
| 776 |
function _block_render_blocks($region_blocks) {
|
| 777 |
foreach ($region_blocks as $key => $block) {
|
| 778 |
// Render the block content if it has not been created already.
|
| 779 |
if (!isset($block->content)) {
|
| 780 |
// Erase the block from the static array - we'll put it back if it has
|
| 781 |
// content.
|
| 782 |
unset($region_blocks[$key]);
|
| 783 |
// Try fetching the block from cache. Block caching is not compatible
|
| 784 |
// with node_access modules. We also preserve the submission of forms in
|
| 785 |
// blocks, by fetching from cache only if the request method is 'GET'
|
| 786 |
// (or 'HEAD').
|
| 787 |
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'))) {
|
| 788 |
$array = $cache->data;
|
| 789 |
}
|
| 790 |
else {
|
| 791 |
$array = module_invoke($block->module, 'block_view', $block->delta);
|
| 792 |
|
| 793 |
// Allow modules to modify the block before it is viewed, via either
|
| 794 |
// hook_block_view_MODULE_DELTA_alter() or hook_block_view_alter().
|
| 795 |
drupal_alter("block_view_{$block->module}_{$block->delta}", $array, $block);
|
| 796 |
drupal_alter('block_view', $array, $block);
|
| 797 |
|
| 798 |
if (isset($cid)) {
|
| 799 |
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
|
| 800 |
}
|
| 801 |
}
|
| 802 |
|
| 803 |
if (isset($array) && is_array($array)) {
|
| 804 |
foreach ($array as $k => $v) {
|
| 805 |
$block->$k = $v;
|
| 806 |
}
|
| 807 |
}
|
| 808 |
if (isset($block->content) && $block->content) {
|
| 809 |
// Normalize to the drupal_render() structure.
|
| 810 |
if (is_string($block->content)) {
|
| 811 |
$block->content = array('#markup' => $block->content);
|
| 812 |
}
|
| 813 |
// Override default block title if a custom display title is present.
|
| 814 |
if ($block->title) {
|
| 815 |
// Check plain here to allow module generated titles to keep any
|
| 816 |
// markup.
|
| 817 |
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
|
| 818 |
}
|
| 819 |
if (!isset($block->subject)) {
|
| 820 |
$block->subject = '';
|
| 821 |
}
|
| 822 |
$region_blocks["{$block->module}_{$block->delta}"] = $block;
|
| 823 |
}
|
| 824 |
}
|
| 825 |
}
|
| 826 |
return $region_blocks;
|
| 827 |
}
|
| 828 |
|
| 829 |
/**
|
| 830 |
* Assemble the cache_id to use for a given block.
|
| 831 |
*
|
| 832 |
* The cache_id string reflects the viewing context for the current block
|
| 833 |
* instance, obtained by concatenating the relevant context information
|
| 834 |
* (user, page, ...) according to the block's cache settings (BLOCK_CACHE_*
|
| 835 |
* constants). Two block instances can use the same cached content when
|
| 836 |
* they share the same cache_id.
|
| 837 |
*
|
| 838 |
* Theme and language contexts are automatically differentiated.
|
| 839 |
*
|
| 840 |
* @param $block
|
| 841 |
* @return
|
| 842 |
* The string used as cache_id for the block.
|
| 843 |
*/
|
| 844 |
function _block_get_cache_id($block) {
|
| 845 |
global $user;
|
| 846 |
|
| 847 |
// User 1 being out of the regular 'roles define permissions' schema,
|
| 848 |
// it brings too many chances of having unwanted output get in the cache
|
| 849 |
// and later be served to other users. We therefore exclude user 1 from
|
| 850 |
// block caching.
|
| 851 |
if (variable_get('block_cache', 0) && !in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM)) && $user->uid != 1) {
|
| 852 |
// Start with common sub-patterns: block identification, theme, language.
|
| 853 |
$cid_parts[] = $block->module;
|
| 854 |
$cid_parts[] = $block->delta;
|
| 855 |
$cid_parts += drupal_render_cid_parts($block->cache);
|
| 856 |
|
| 857 |
return implode(':', $cid_parts);
|
| 858 |
}
|
| 859 |
}
|
| 860 |
|
| 861 |
/**
|
| 862 |
* Implement hook_flush_caches().
|
| 863 |
*/
|
| 864 |
function block_flush_caches() {
|
| 865 |
return array('cache_block');
|
| 866 |
}
|
| 867 |
|
| 868 |
/**
|
| 869 |
* Process variables for block.tpl.php
|
| 870 |
*
|
| 871 |
* Prepare the values passed to the theme_block function to be passed
|
| 872 |
* into a pluggable template engine. Uses block properties to generate a
|
| 873 |
* series of template file suggestions. If none are found, the default
|
| 874 |
* block.tpl.php is used.
|
| 875 |
*
|
| 876 |
* Most themes utilize their own copy of block.tpl.php. The default is located
|
| 877 |
* inside "modules/block/block.tpl.php". Look in there for the full list of
|
| 878 |
* variables.
|
| 879 |
*
|
| 880 |
* The $variables array contains the following arguments:
|
| 881 |
* - $block
|
| 882 |
*
|
| 883 |
* @see block.tpl.php
|
| 884 |
*/
|
| 885 |
function template_preprocess_block(&$variables) {
|
| 886 |
$block_counter = &drupal_static(__FUNCTION__, array());
|
| 887 |
$variables['block'] = $variables['elements']['#block'];
|
| 888 |
// All blocks get an independent counter for each region.
|
| 889 |
if (!isset($block_counter[$variables['block']->region])) {
|
| 890 |
$block_counter[$variables['block']->region] = 1;
|
| 891 |
}
|
| 892 |
// Same with zebra striping.
|
| 893 |
$variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';
|
| 894 |
$variables['block_id'] = $block_counter[$variables['block']->region]++;
|
| 895 |
|
| 896 |
// Create the $content variable that templates expect.
|
| 897 |
$variables['content'] = $variables['elements']['#children'];
|
| 898 |
|
| 899 |
$variables['classes_array'][] = drupal_html_class('block-' . $variables['block']->module);
|
| 900 |
|
| 901 |
$variables['template_files'][] = 'block-' . $variables['block']->region;
|
| 902 |
$variables['template_files'][] = 'block-' . $variables['block']->module;
|
| 903 |
$variables['template_files'][] = 'block-' . $variables['block']->module . '-' . $variables['block']->delta;
|
| 904 |
}
|
| 905 |
|
| 906 |
/**
|
| 907 |
* Implement hook_user_role_delete().
|
| 908 |
*
|
| 909 |
* Remove deleted role from blocks that use it.
|
| 910 |
*/
|
| 911 |
function block_user_role_delete($role) {
|
| 912 |
db_delete('block_role')
|
| 913 |
->condition('rid', $role->rid)
|
| 914 |
->execute();
|
| 915 |
}
|
| 916 |
|
| 917 |
/**
|
| 918 |
* Implement hook_filter_format_delete().
|
| 919 |
*/
|
| 920 |
function block_filter_format_delete($format, $fallback) {
|
| 921 |
db_update('block_custom')
|
| 922 |
->fields(array('format' => $fallback->format))
|
| 923 |
->condition('format', $format->format)
|
| 924 |
->execute();
|
| 925 |
}
|
| 926 |
|
| 927 |
/**
|
| 928 |
* Implement hook_menu_delete().
|
| 929 |
*/
|
| 930 |
function block_menu_delete($menu) {
|
| 931 |
db_delete('block')
|
| 932 |
->condition('module', 'menu')
|
| 933 |
->condition('delta', $menu['menu_name'])
|
| 934 |
->execute();
|
| 935 |
db_delete('block_role')
|
| 936 |
->condition('module', 'menu')
|
| 937 |
->condition('delta', $menu['menu_name'])
|
| 938 |
->execute();
|
| 939 |
}
|
| 940 |
|
| 941 |
/**
|
| 942 |
* Implement hook_form_FORM_ID_alter().
|
| 943 |
*/
|
| 944 |
function block_form_system_performance_settings_alter(&$form, &$form_state) {
|
| 945 |
$disabled = count(module_implements('node_grants'));
|
| 946 |
$form['caching']['block_cache'] = array(
|
| 947 |
'#type' => 'checkbox',
|
| 948 |
'#title' => t('Cache blocks'),
|
| 949 |
'#default_value' => variable_get('block_cache', FALSE),
|
| 950 |
'#disabled' => $disabled,
|
| 951 |
'#description' => $disabled ? t('Block caching is inactive because you have enabled modules defining content access restrictions.') : NULL,
|
| 952 |
'#weight' => -1,
|
| 953 |
);
|
| 954 |
}
|