| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
define('BLOCK_REGION_NONE', -1);
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
define('BLOCK_NO_CACHE', -1);
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
define('BLOCK_CACHE_PER_ROLE', 0x0001);
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
define('BLOCK_CACHE_PER_USER', 0x0002);
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
define('BLOCK_CACHE_PER_PAGE', 0x0004);
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
define('BLOCK_CACHE_GLOBAL', 0x0008);
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
function block_help($path, $arg) {
|
| 67 |
|
| 68 |
case 'admin/help#block':
|
| 69 |
$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>';
|
| 70 |
$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>';
|
| 71 |
$output .= '<p>' . t('When working with blocks, remember that:') . '</p>';
|
| 72 |
$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>';
|
| 73 |
$output .= '<li>' . t('disabled blocks, or blocks not in a region, are never shown.') . '</li>';
|
| 74 |
$output .= '<li>' . t('blocks can be configured to be visible only on certain pages.') . '</li>';
|
| 75 |
$output .= '<li>' . t('blocks can be configured to be visible only when specific conditions are true.') . '</li>';
|
| 76 |
$output .= '<li>' . t('blocks can be configured to be visible only for certain user roles.') . '</li>';
|
| 77 |
$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>';
|
| 78 |
$output .= '<li>' . t('some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.') . '</li></ul>';
|
| 79 |
$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>';
|
| 80 |
return $output;
|
| 81 |
case 'admin/structure/block':
|
| 82 |
$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>';
|
| 83 |
$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>';
|
| 84 |
return $output;
|
| 85 |
case 'admin/structure/block/add':
|
| 86 |
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>';
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
function block_theme() {
|
| 94 |
|
| 95 |
|
| 96 |
'arguments' => array('elements' => NULL),
|
| 97 |
'template' => 'block',
|
| 98 |
),
|
| 99 |
|
| 100 |
'template' => 'block-admin-display-form',
|
| 101 |
'file' => 'block.admin.inc',
|
| 102 |
'arguments' => array('form' => NULL),
|
| 103 |
),
|
| 104 |
);
|
| 105 |
}
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
function block_permission() {
|
| 111 |
|
| 112 |
|
| 113 |
'title' => t('Administer blocks'),
|
| 114 |
'description' => t('Select which blocks are displayed, and arrange them on the page.'),
|
| 115 |
),
|
| 116 |
);
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
function block_menu() {
|
| 123 |
$items['admin/structure/block'] = array(
|
| 124 |
'title' => 'Blocks',
|
| 125 |
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
|
| 126 |
'page callback' => 'block_admin_display',
|
| 127 |
'access arguments' => array('administer blocks'),
|
| 128 |
|
| 129 |
$items['admin/structure/block/list'] = array(
|
| 130 |
'title' => 'List',
|
| 131 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 132 |
'weight' => -10,
|
| 133 |
|
| 134 |
$items['admin/structure/block/list/js'] = array(
|
| 135 |
'title' => 'JavaScript List Form',
|
| 136 |
'page callback' => 'block_admin_display_js',
|
| 137 |
'access arguments' => array('administer blocks'),
|
| 138 |
'type' => MENU_CALLBACK,
|
| 139 |
|
| 140 |
$items['admin/structure/block/configure'] = array(
|
| 141 |
'title' => 'Configure block',
|
| 142 |
'page callback' => 'drupal_get_form',
|
| 143 |
'page arguments' => array('block_admin_configure'),
|
| 144 |
'access arguments' => array('administer blocks'),
|
| 145 |
'type' => MENU_CALLBACK,
|
| 146 |
|
| 147 |
$items['admin/structure/block/delete'] = array(
|
| 148 |
'title' => 'Delete block',
|
| 149 |
'page callback' => 'drupal_get_form',
|
| 150 |
'page arguments' => array('block_box_delete'),
|
| 151 |
'access arguments' => array('administer blocks'),
|
| 152 |
'type' => MENU_CALLBACK,
|
| 153 |
|
| 154 |
$items['admin/structure/block/add'] = array(
|
| 155 |
'title' => 'Add block',
|
| 156 |
'page callback' => 'drupal_get_form',
|
| 157 |
'page arguments' => array('block_add_block_form'),
|
| 158 |
'access arguments' => array('administer blocks'),
|
| 159 |
'type' => MENU_LOCAL_TASK,
|
| 160 |
|
| 161 |
$default = variable_get('theme_default', 'garland');
|
| 162 |
foreach (list_themes() as $key => $theme) {
|
| 163 |
$items['admin/structure/block/list/' . $key] = array(
|
| 164 |
'title' => check_plain($theme->info['name']),
|
| 165 |
'page arguments' => array($key),
|
| 166 |
'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
|
| 167 |
'weight' => $key == $default ? -10 : 0,
|
| 168 |
'access callback' => '_block_themes_access',
|
| 169 |
'access arguments' => array($theme),
|
| 170 |
|
| 171 |
}
|
| 172 |
return $items;
|
| 173 |
}
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
function _block_themes_access($theme) {
|
| 179 |
$admin_theme = variable_get('admin_theme');
|
| 180 |
return user_access('administer blocks') && ($theme->status || ($admin_theme && ($theme->name == $admin_theme)));
|
| 181 |
}
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
function block_block_list() {
|
| 187 |
$blocks = array();
|
| 188 |
|
| 189 |
$result = db_query('SELECT bid, info FROM {box} ORDER BY info');
|
| 190 |
foreach ($result as $block) {
|
| 191 |
$blocks[$block->bid]['info'] = $block->info;
|
| 192 |
|
| 193 |
$blocks[$block->bid]['cache'] = BLOCK_NO_CACHE;
|
| 194 |
}
|
| 195 |
return $blocks;
|
| 196 |
}
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
function block_block_configure($delta = 0, $edit = array()) {
|
| 202 |
$box = array('format' => FILTER_FORMAT_DEFAULT);
|
| 203 |
if ($delta) {
|
| 204 |
$box = block_box_get($delta);
|
| 205 |
}
|
| 206 |
return block_box_form($box);
|
| 207 |
}
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
function block_block_save($delta = 0, $edit = array()) {
|
| 213 |
block_box_save($edit, $delta);
|
| 214 |
}
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
function block_block_view($delta = 0, $edit = array()) {
|
| 222 |
$block = db_query('SELECT body, format FROM {box} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
|
| 223 |
$data['content'] = check_markup($block->body, $block->format, '', FALSE);
|
| 224 |
return $data;
|
| 225 |
}
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
function block_page_alter($page) {
|
| 233 |
global $theme;
|
| 234 |
|
| 235 |
|
| 236 |
drupal_theme_initialize();
|
| 237 |
|
| 238 |
|
| 239 |
$all_regions = system_region_list($theme);
|
| 240 |
|
| 241 |
|
| 242 |
foreach (array_keys($all_regions) as $region) {
|
| 243 |
|
| 244 |
if (!empty($page['#show_blocks']) || (strpos($region, 'sidebar_') !== 0)) {
|
| 245 |
|
| 246 |
if ($blocks = block_get_blocks_by_region($region)) {
|
| 247 |
$page[$region] = $blocks;
|
| 248 |
}
|
| 249 |
|
| 250 |
|
| 251 |
$item = menu_get_item();
|
| 252 |
if ($item['path'] == 'admin/structure/block') {
|
| 253 |
$visible_regions = system_region_list($theme, REGIONS_VISIBLE);
|
| 254 |
if (isset($visible_regions[$region])) {
|
| 255 |
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
|
| 256 |
$page[$region]['block_description'] = array(
|
| 257 |
'#markup' => $description,
|
| 258 |
'#weight' => 15,
|
| 259 |
|
| 260 |
}
|
| 261 |
}
|
| 262 |
}
|
| 263 |
}
|
| 264 |
}
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
function block_get_blocks_by_region($region) {
|
| 273 |
$weight = 0;
|
| 274 |
$build = array();
|
| 275 |
if ($list = block_list($region)) {
|
| 276 |
foreach ($list as $key => $block) {
|
| 277 |
$build[$key] = $block->content;
|
| 278 |
unset($block->content);
|
| 279 |
$build[$key] += array(
|
| 280 |
'#block' => $block,
|
| 281 |
'#weight' => ++$weight,
|
| 282 |
|
| 283 |
$build[$key]['#theme_wrappers'][] ='block';
|
| 284 |
}
|
| 285 |
$build['#sorted'] = TRUE;
|
| 286 |
}
|
| 287 |
return $build;
|
| 288 |
}
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
function _block_rehash() {
|
| 297 |
global $theme_key;
|
| 298 |
|
| 299 |
drupal_theme_initialize();
|
| 300 |
|
| 301 |
$old_blocks = array();
|
| 302 |
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme_key));
|
| 303 |
foreach ($result as $old_block) {
|
| 304 |
$old_block = is_object($old_block) ? get_object_vars($old_block) : $old_block;
|
| 305 |
$old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
|
| 306 |
}
|
| 307 |
|
| 308 |
$blocks = array();
|
| 309 |
|
| 310 |
$regions = system_region_list($theme_key);
|
| 311 |
|
| 312 |
foreach (module_implements('block_list') as $module) {
|
| 313 |
$module_blocks = module_invoke($module, 'block_list');
|
| 314 |
if ($module_blocks) {
|
| 315 |
foreach ($module_blocks as $delta => $block) {
|
| 316 |
if (empty($old_blocks[$module][$delta])) {
|
| 317 |
|
| 318 |
$block['module'] = $module;
|
| 319 |
$block['delta'] = $delta;
|
| 320 |
$block['theme'] = $theme_key;
|
| 321 |
if (!isset($block['pages'])) {
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
$block['pages'] = '';
|
| 326 |
}
|
| 327 |
|
| 328 |
drupal_write_record('block', $block);
|
| 329 |
|
| 330 |
$block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
|
| 331 |
|
| 332 |
$blocks[] = $block;
|
| 333 |
}
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
$old_blocks[$module][$delta]['info'] = $block['info'];
|
| 341 |
|
| 342 |
if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) {
|
| 343 |
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');
|
| 344 |
$old_blocks[$module][$delta]['status'] = 0;
|
| 345 |
$old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
|
| 346 |
}
|
| 347 |
|
| 348 |
$old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE;
|
| 349 |
|
| 350 |
|
| 351 |
$blocks[] = $old_blocks[$module][$delta];
|
| 352 |
|
| 353 |
unset($old_blocks[$module][$delta]);
|
| 354 |
|
| 355 |
}
|
| 356 |
}
|
| 357 |
}
|
| 358 |
|
| 359 |
|
| 360 |
foreach ($old_blocks as $module => $old_module_blocks) {
|
| 361 |
foreach ($old_module_blocks as $delta => $block) {
|
| 362 |
db_delete('block')
|
| 363 |
->condition('module', $module)
|
| 364 |
->condition('delta', $delta)
|
| 365 |
->condition('theme', $theme_key)
|
| 366 |
->execute();
|
| 367 |
}
|
| 368 |
}
|
| 369 |
return $blocks;
|
| 370 |
}
|
| 371 |
|
| 372 |
function block_box_get($bid) {
|
| 373 |
return db_query("SELECT * FROM {box} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
|
| 374 |
}
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
function block_box_form($edit = array()) {
|
| 380 |
|
| 381 |
'info' => '',
|
| 382 |
'body' => '',
|
| 383 |
);
|
| 384 |
$form['info'] = array(
|
| 385 |
'#type' => 'textfield',
|
| 386 |
'#title' => t('Block description'),
|
| 387 |
'#default_value' => $edit['info'],
|
| 388 |
'#maxlength' => 64,
|
| 389 |
'#description' => t('A brief description of your block. Used on the <a href="@overview">blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
|
| 390 |
'#required' => TRUE,
|
| 391 |
'#weight' => -19,
|
| 392 |
|
| 393 |
$form['body_field']['#weight'] = -17;
|
| 394 |
$form['body_field']['body'] = array(
|
| 395 |
'#type' => 'textarea',
|
| 396 |
'#title' => t('Block body'),
|
| 397 |
'#default_value' => $edit['body'],
|
| 398 |
'#text_format' => isset($edit['format']) ? $edit['format'] : FILTER_FORMAT_DEFAULT,
|
| 399 |
'#rows' => 15,
|
| 400 |
'#description' => t('The content of the block as shown to the user.'),
|
| 401 |
'#required' => TRUE,
|
| 402 |
'#weight' => -17,
|
| 403 |
'#access' => filter_access($edit['format']),
|
| 404 |
|
| 405 |
|
| 406 |
return $form;
|
| 407 |
}
|
| 408 |
|
| 409 |
function block_box_save($edit, $delta) {
|
| 410 |
db_update('box')
|
| 411 |
->fields(array(
|
| 412 |
'body' => $edit['body'],
|
| 413 |
'info' => $edit['info'],
|
| 414 |
'format' => $edit['body_format'],
|
| 415 |
))
|
| 416 |
->condition('bid', $delta)
|
| 417 |
->execute();
|
| 418 |
return TRUE;
|
| 419 |
}
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
function block_user_form(&$edit, $account, $category) {
|
| 425 |
if ($category == 'account') {
|
| 426 |
$rids = array_keys($account->roles);
|
| 427 |
$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));
|
| 428 |
$form['block'] = array(
|
| 429 |
'#type' => 'fieldset',
|
| 430 |
'#title' => t('Personalize blocks'),
|
| 431 |
'#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.'),
|
| 432 |
'#weight' => 3,
|
| 433 |
'#collapsible' => TRUE,
|
| 434 |
|
| 435 |
);
|
| 436 |
foreach ($result as $block) {
|
| 437 |
$data = module_invoke($block->module, 'block_list');
|
| 438 |
if ($data[$block->delta]['info']) {
|
| 439 |
$return = TRUE;
|
| 440 |
$form['block'][$block->module][$block->delta] = array(
|
| 441 |
'#type' => 'checkbox',
|
| 442 |
'#title' => check_plain($data[$block->delta]['info']),
|
| 443 |
'#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1),
|
| 444 |
|
| 445 |
}
|
| 446 |
}
|
| 447 |
|
| 448 |
if (!empty($return)) {
|
| 449 |
return $form;
|
| 450 |
}
|
| 451 |
}
|
| 452 |
}
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
function block_user_validate(&$edit, $account, $category) {
|
| 458 |
if (empty($edit['block'])) {
|
| 459 |
$edit['block'] = array();
|
| 460 |
}
|
| 461 |
return $edit;
|
| 462 |
}
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
function block_form_system_themes_form_alter(&$form, &$form_state) {
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
array_unshift($form['#submit'], 'block_system_themes_form_submit');
|
| 472 |
}
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
function block_system_themes_form_submit(&$form, &$form_state) {
|
| 478 |
if ($form_state['values']['op'] == t('Save configuration')) {
|
| 479 |
if (is_array($form_state['values']['status'])) {
|
| 480 |
foreach ($form_state['values']['status'] as $key => $choice) {
|
| 481 |
if ($choice || $form_state['values']['theme_default'] == $key) {
|
| 482 |
block_theme_initialize($key);
|
| 483 |
}
|
| 484 |
}
|
| 485 |
}
|
| 486 |
if ($form_state['values']['admin_theme'] && $form_state['values']['admin_theme'] !== variable_get('admin_theme', 0)) {
|
| 487 |
|
| 488 |
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $form_state['values']['admin_theme']), 0, 1)->fetchField();
|
| 489 |
if (!$has_blocks) {
|
| 490 |
block_theme_initialize($form_state['values']['admin_theme']);
|
| 491 |
}
|
| 492 |
}
|
| 493 |
}
|
| 494 |
}
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
function block_theme_initialize($theme) {
|
| 508 |
|
| 509 |
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $theme), 0, 1)->fetchField();
|
| 510 |
if (!$has_blocks) {
|
| 511 |
$default_theme = variable_get('theme_default', 'garland');
|
| 512 |
$regions = system_region_list($theme);
|
| 513 |
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $default_theme), array('fetch' => PDO::FETCH_ASSOC));
|
| 514 |
$query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'custom', 'cache'));
|
| 515 |
foreach ($result as $block) {
|
| 516 |
|
| 517 |
if (!array_key_exists($block['region'], $regions)) {
|
| 518 |
$block['region'] = system_default_region($theme);
|
| 519 |
}
|
| 520 |
$block['theme'] = $theme;
|
| 521 |
$query->values($block);
|
| 522 |
}
|
| 523 |
$query->execute();
|
| 524 |
}
|
| 525 |
}
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
function block_list($region) {
|
| 544 |
$blocks = &drupal_static(__FUNCTION__, array());
|
| 545 |
|
| 546 |
if (empty($blocks)) {
|
| 547 |
$blocks = _block_load_blocks();
|
| 548 |
}
|
| 549 |
|
| 550 |
|
| 551 |
if (!isset($blocks[$region])) {
|
| 552 |
$blocks[$region] = array();
|
| 553 |
}
|
| 554 |
|
| 555 |
$blocks[$region] = _block_render_blocks($blocks[$region]);
|
| 556 |
|
| 557 |
return $blocks[$region];
|
| 558 |
}
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
function _block_load_blocks() {
|
| 564 |
global $theme_key;
|
| 565 |
|
| 566 |
$query = db_select('block', 'b');
|
| 567 |
|
| 568 |
->fields('b')
|
| 569 |
->condition('b.theme', $theme_key)
|
| 570 |
->condition('b.status', 1)
|
| 571 |
->orderBy('b.region')
|
| 572 |
->orderBy('b.weight')
|
| 573 |
->orderBy('b.module')
|
| 574 |
->addTag('block_load')
|
| 575 |
->execute();
|
| 576 |
|
| 577 |
$block_list = $result->fetchAllAssoc('bid');
|
| 578 |
|
| 579 |
drupal_alter('block_list', $block_list);
|
| 580 |
|
| 581 |
$blocks = array();
|
| 582 |
foreach ($block_list as $block) {
|
| 583 |
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
|
| 584 |
}
|
| 585 |
return $blocks;
|
| 586 |
}
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
function block_block_list_alter(&$blocks) {
|
| 595 |
global $user, $theme_key;
|
| 596 |
|
| 597 |
|
| 598 |
$block_roles = array();
|
| 599 |
$result = db_query('SELECT module, delta, rid FROM {block_role}');
|
| 600 |
foreach ($result as $record) {
|
| 601 |
$block_roles[$record->module][$record->delta][] = $record->rid;
|
| 602 |
}
|
| 603 |
|
| 604 |
|
| 605 |
$block_node_types = array();
|
| 606 |
$result = db_query('SELECT module, delta, type FROM {block_node_type}');
|
| 607 |
foreach ($result as $record) {
|
| 608 |
$block_node_types[$record->module][$record->delta][] = $record->type;
|
| 609 |
}
|
| 610 |
|
| 611 |
foreach ($blocks as $key => $block) {
|
| 612 |
if ($block->theme != $theme_key || $block->status != 1) {
|
| 613 |
|
| 614 |
continue;
|
| 615 |
}
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
if (isset($block_roles[$block->module][$block->delta]) && !array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
|
| 621 |
|
| 622 |
unset($blocks[$key]);
|
| 623 |
continue;
|
| 624 |
}
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
if (isset($block_node_types[$block->module][$block->delta])) {
|
| 630 |
$node = menu_get_object();
|
| 631 |
if (!empty($node)) {
|
| 632 |
|
| 633 |
if (!in_array($node->type, $block_node_types[$block->module][$block->delta])) {
|
| 634 |
|
| 635 |
unset($blocks[$key]);
|
| 636 |
continue;
|
| 637 |
}
|
| 638 |
}
|
| 639 |
elseif (arg(0) == 'node' && arg(1) == 'add' && in_array(arg(2), array_keys(node_type_get_types()))) {
|
| 640 |
|
| 641 |
if (!in_array(arg(2), $block_node_types[$block->module][$block->delta])) {
|
| 642 |
|
| 643 |
unset($blocks[$key]);
|
| 644 |
continue;
|
| 645 |
}
|
| 646 |
}
|
| 647 |
|
| 648 |
|
| 649 |
unset($blocks[$key]);
|
| 650 |
continue;
|
| 651 |
|
| 652 |
}
|
| 653 |
|
| 654 |
|
| 655 |
if ($block->custom != 0) {
|
| 656 |
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
|
| 657 |
$enabled = $user->block[$block->module][$block->delta];
|
| 658 |
}
|
| 659 |
|
| 660 |
$enabled = ($block->custom == 1);
|
| 661 |
|
| 662 |
}
|
| 663 |
|
| 664 |
$enabled = TRUE;
|
| 665 |
|
| 666 |
if (!$enabled) {
|
| 667 |
unset($blocks[$key]);
|
| 668 |
continue;
|
| 669 |
}
|
| 670 |
|
| 671 |
|
| 672 |
if ($block->pages) {
|
| 673 |
if ($block->visibility < 2) {
|
| 674 |
$path = drupal_get_path_alias($_GET['q']);
|
| 675 |
|
| 676 |
$page_match = drupal_match_path($path, $block->pages);
|
| 677 |
if ($path != $_GET['q']) {
|
| 678 |
$page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
|
| 679 |
}
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
$page_match = !($block->visibility xor $page_match);
|
| 684 |
}
|
| 685 |
elseif (module_exists('php')) {
|
| 686 |
$page_match = php_eval($block->pages);
|
| 687 |
}
|
| 688 |
|
| 689 |
$page_match = FALSE;
|
| 690 |
|
| 691 |
}
|
| 692 |
|
| 693 |
$page_match = TRUE;
|
| 694 |
|
| 695 |
if (!$page_match) {
|
| 696 |
unset($blocks[$key]);
|
| 697 |
}
|
| 698 |
}
|
| 699 |
}
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
function _block_render_blocks($region_blocks) {
|
| 711 |
foreach ($region_blocks as $key => $block) {
|
| 712 |
|
| 713 |
if (!isset($block->content)) {
|
| 714 |
|
| 715 |
|
| 716 |
unset($region_blocks[$key]);
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
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'))) {
|
| 722 |
$array = $cache->data;
|
| 723 |
}
|
| 724 |
|
| 725 |
$array = module_invoke($block->module, 'block_view', $block->delta);
|
| 726 |
if (isset($cid)) {
|
| 727 |
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
|
| 728 |
}
|
| 729 |
|
| 730 |
|
| 731 |
if (isset($array) && is_array($array)) {
|
| 732 |
foreach ($array as $k => $v) {
|
| 733 |
$block->$k = $v;
|
| 734 |
}
|
| 735 |
}
|
| 736 |
if (isset($block->content) && $block->content) {
|
| 737 |
|
| 738 |
if (is_string($block->content)) {
|
| 739 |
$block->content = array('#markup' => $block->content);
|
| 740 |
}
|
| 741 |
|
| 742 |
if ($block->title) {
|
| 743 |
|
| 744 |
|
| 745 |
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
|
| 746 |
}
|
| 747 |
if (!isset($block->subject)) {
|
| 748 |
$block->subject = '';
|
| 749 |
}
|
| 750 |
$region_blocks["{$block->module}_{$block->delta}"] = $block;
|
| 751 |
}
|
| 752 |
}
|
| 753 |
}
|
| 754 |
return $region_blocks;
|
| 755 |
}
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
function _block_get_cache_id($block) {
|
| 773 |
global $theme, $base_root, $user;
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) {
|
| 780 |
$cid_parts = array();
|
| 781 |
|
| 782 |
|
| 783 |
$cid_parts[] = $block->module;
|
| 784 |
$cid_parts[] = $block->delta;
|
| 785 |
$cid_parts[] = $theme;
|
| 786 |
if (module_exists('locale')) {
|
| 787 |
global $language;
|
| 788 |
$cid_parts[] = $language->language;
|
| 789 |
}
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
if ($block->cache & BLOCK_CACHE_PER_ROLE) {
|
| 795 |
$cid_parts[] = 'r.' . implode(',', array_keys($user->roles));
|
| 796 |
}
|
| 797 |
elseif ($block->cache & BLOCK_CACHE_PER_USER) {
|
| 798 |
$cid_parts[] = "u.$user->uid";
|
| 799 |
}
|
| 800 |
|
| 801 |
if ($block->cache & BLOCK_CACHE_PER_PAGE) {
|
| 802 |
$cid_parts[] = $base_root . request_uri();
|
| 803 |
}
|
| 804 |
|
| 805 |
return implode(':', $cid_parts);
|
| 806 |
}
|
| 807 |
}
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
function block_flush_caches() {
|
| 813 |
return array('cache_block');
|
| 814 |
}
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
function template_preprocess_block(&$variables) {
|
| 834 |
$block_counter = &drupal_static(__FUNCTION__, array());
|
| 835 |
$variables['block'] = $variables['elements']['#block'];
|
| 836 |
|
| 837 |
if (!isset($block_counter[$variables['block']->region])) {
|
| 838 |
$block_counter[$variables['block']->region] = 1;
|
| 839 |
}
|
| 840 |
|
| 841 |
$variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';
|
| 842 |
$variables['block_id'] = $block_counter[$variables['block']->region]++;
|
| 843 |
|
| 844 |
|
| 845 |
$variables['content'] = $variables['elements']['#children'];
|
| 846 |
|
| 847 |
$variables['classes_array'][] = 'block-' . $variables['block']->module;
|
| 848 |
|
| 849 |
$variables['template_files'][] = 'block-' . $variables['block']->region;
|
| 850 |
$variables['template_files'][] = 'block-' . $variables['block']->module;
|
| 851 |
$variables['template_files'][] = 'block-' . $variables['block']->module . '-' . $variables['block']->delta;
|
| 852 |
}
|
| 853 |
|