928 lines
33 KiB
Plaintext
928 lines
33 KiB
Plaintext
<?php
|
||
// $Id$
|
||
|
||
/**
|
||
* @file
|
||
* Controls the visual building blocks a page is constructed with.
|
||
*/
|
||
|
||
/**
|
||
* Denotes that a block is not enabled in any region and should not be shown.
|
||
*/
|
||
define('BLOCK_REGION_NONE', -1);
|
||
|
||
/**
|
||
* Implements hook_help().
|
||
*/
|
||
function block_help($path, $arg) {
|
||
switch ($path) {
|
||
case 'admin/help#block':
|
||
$output = '';
|
||
$output .= '<h3>' . t('About') . '</h3>';
|
||
$output .= '<p>' . t('The Block module allows you to create boxes of content, which are rendered into an area, or region, of one or more pages of a website. The core Seven administration theme, for example, implements the regions "Content", "Help", "Dashboard main", and "Dashboard sidebar", and a block may appear in any one of these regions. 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. For more information, see the online handbook entry for <a href="@block">Block module</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/', '@blocks' => url('admin/structure/block'))) . '</p>';
|
||
$output .= '<h3>' . t('Uses') . '</h3>';
|
||
$output .= '<dl>';
|
||
$output .= '<dt>' . t('Positioning content') . '</dt>';
|
||
$output .= '<dd>' . t('When working with blocks, remember that all themes do <em>not</em> implement the same regions, or display regions in the same way. Blocks are positioned on a per-theme basis. Users with the <em>Administer blocks</em> permission can disable blocks. Disabled blocks are listed on the <a href="@blocks">Blocks administration page</a>, but are not displayed in any region.', array('@block' => 'http://drupal.org/handbook/modules/block/', '@blocks' => url('admin/structure/block'))) . '</dd>';
|
||
$output .= '<dt>' . t('Controlling visibility') . '</dt>';
|
||
$output .= '<dd>' . t('Blocks can be configured to be visible only on certain pages, only to users of certain roles, or only on pages displaying certain <a href="@content-type">content types</a>. Administrators can also allow specific blocks to be enabled or disabled by users when they edit their <a href="@user">My account</a> page. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.', array('@content-type' => url('admin/structure/types'), '@user' => url('user'))) . '</dd>';
|
||
$output .= '<dt>' . t('Creating custom blocks') . '</dt>';
|
||
$output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can <a href="@block-add">add custom blocks</a>, which are then listed on the <a href="@blocks">Blocks administration page</a>. Once created, custom blocks behave just like default and module-generated blocks.', array('@blocks' => url('admin/structure/block'), '@block-add' => url('admin/structure/block/add'))) . '</dd>';
|
||
$output .= '</dl>';
|
||
return $output;
|
||
case 'admin/structure/block/add':
|
||
return '<p>' . t('Use this page to create a new custom block. New blocks are disabled by default; assign them to a region, on this page or the <a href="@blocks">Blocks administration page</a>, to make them visible.', array('@blocks' => url('admin/structure/block'))) . '</p>';
|
||
}
|
||
if ($arg[0] == 'admin' && $arg[1] == 'structure' && $arg['2'] == 'block' && (empty($arg[3]) || $arg[3] == 'list')) {
|
||
$demo_theme = !empty($arg[4]) ? $arg[4] : variable_get('theme_default', 'garland');
|
||
$themes = list_themes();
|
||
$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>';
|
||
$output .= '<p>' . l(t('Demonstrate block regions (@theme)', array('@theme' => $themes[$demo_theme]->info['name'])), 'admin/structure/block/demo/' . $demo_theme) . '</p>';
|
||
return $output;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Implements hook_theme().
|
||
*/
|
||
function block_theme() {
|
||
return array(
|
||
'block' => array(
|
||
'render element' => 'elements',
|
||
'template' => 'block',
|
||
),
|
||
'block_admin_display_form' => array(
|
||
'template' => 'block-admin-display-form',
|
||
'file' => 'block.admin.inc',
|
||
'render element' => 'form',
|
||
),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Implements hook_permission().
|
||
*/
|
||
function block_permission() {
|
||
return array(
|
||
'administer blocks' => array(
|
||
'title' => t('Administer blocks'),
|
||
),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Implements hook_menu().
|
||
*/
|
||
function block_menu() {
|
||
$default_theme = variable_get('theme_default', 'garland');
|
||
$items['admin/structure/block'] = array(
|
||
'title' => 'Blocks',
|
||
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
|
||
'page callback' => 'block_admin_display',
|
||
'page arguments' => array($default_theme),
|
||
'access arguments' => array('administer blocks'),
|
||
'file' => 'block.admin.inc',
|
||
);
|
||
$items['admin/structure/block/list'] = array(
|
||
'title' => 'List',
|
||
'type' => MENU_DEFAULT_LOCAL_TASK,
|
||
'weight' => -10,
|
||
);
|
||
$items['admin/structure/block/manage/%/%'] = array(
|
||
'title' => 'Configure block',
|
||
'page callback' => 'drupal_get_form',
|
||
'page arguments' => array('block_admin_configure', 4, 5),
|
||
'access arguments' => array('administer blocks'),
|
||
'file' => 'block.admin.inc',
|
||
);
|
||
$items['admin/structure/block/manage/%/%/configure'] = array(
|
||
'title' => 'Configure block',
|
||
'type' => MENU_DEFAULT_LOCAL_TASK,
|
||
'context' => MENU_CONTEXT_INLINE,
|
||
);
|
||
$items['admin/structure/block/manage/%/%/delete'] = array(
|
||
'title' => 'Delete block',
|
||
'page callback' => 'drupal_get_form',
|
||
'page arguments' => array('block_custom_block_delete', 4, 5),
|
||
'access arguments' => array('administer blocks'),
|
||
'type' => MENU_LOCAL_TASK,
|
||
'context' => MENU_CONTEXT_NONE,
|
||
'file' => 'block.admin.inc',
|
||
);
|
||
$items['admin/structure/block/add'] = array(
|
||
'title' => 'Add block',
|
||
'page callback' => 'drupal_get_form',
|
||
'page arguments' => array('block_add_block_form'),
|
||
'access arguments' => array('administer blocks'),
|
||
'type' => MENU_LOCAL_ACTION,
|
||
'file' => 'block.admin.inc',
|
||
);
|
||
foreach (list_themes() as $key => $theme) {
|
||
$items['admin/structure/block/list/' . $key] = array(
|
||
'title' => check_plain($theme->info['name']),
|
||
'page arguments' => array($key),
|
||
'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
|
||
'weight' => $key == $default_theme ? -10 : 0,
|
||
'access callback' => '_block_themes_access',
|
||
'access arguments' => array($theme),
|
||
'file' => 'block.admin.inc',
|
||
);
|
||
$items['admin/structure/block/demo/' . $key] = array(
|
||
'title' => check_plain($theme->info['name']),
|
||
'page callback' => 'block_admin_demo',
|
||
'page arguments' => array($key),
|
||
'type' => MENU_CALLBACK,
|
||
'access callback' => '_block_themes_access',
|
||
'access arguments' => array($theme),
|
||
'theme callback' => '_block_custom_theme',
|
||
'theme arguments' => array($key),
|
||
'file' => 'block.admin.inc',
|
||
);
|
||
}
|
||
return $items;
|
||
}
|
||
|
||
/**
|
||
* Menu item access callback - only admin or enabled themes can be accessed.
|
||
*/
|
||
function _block_themes_access($theme) {
|
||
return user_access('administer blocks') && drupal_theme_access($theme);
|
||
}
|
||
|
||
/**
|
||
* Theme callback for the block configuration pages.
|
||
*
|
||
* @param $theme
|
||
* The theme whose blocks are being configured. If not set, the default theme
|
||
* is assumed.
|
||
* @return
|
||
* The theme that should be used for the block configuration page, or NULL
|
||
* to indicate that the default theme should be used.
|
||
*/
|
||
function _block_custom_theme($theme = NULL) {
|
||
// We return exactly what was passed in, to guarantee that the page will
|
||
// always be displayed using the theme whose blocks are being configured.
|
||
return $theme;
|
||
}
|
||
|
||
/**
|
||
* Implements hook_block_info().
|
||
*/
|
||
function block_block_info() {
|
||
$blocks = array();
|
||
|
||
$result = db_query('SELECT bid, info FROM {block_custom} ORDER BY info');
|
||
foreach ($result as $block) {
|
||
$blocks[$block->bid]['info'] = $block->info;
|
||
// Not worth caching.
|
||
$blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE;
|
||
}
|
||
return $blocks;
|
||
}
|
||
|
||
/**
|
||
* Implements hook_block_configure().
|
||
*/
|
||
function block_block_configure($delta = 0) {
|
||
$custom_block = array('format' => filter_default_format());
|
||
if ($delta) {
|
||
$custom_block = block_custom_block_get($delta);
|
||
}
|
||
return block_custom_block_form($custom_block);
|
||
}
|
||
|
||
/**
|
||
* Implements hook_block_save().
|
||
*/
|
||
function block_block_save($delta = 0, $edit = array()) {
|
||
block_custom_block_save($edit, $delta);
|
||
}
|
||
|
||
/**
|
||
* Implements hook_block_view().
|
||
*
|
||
* Generates the administrator-defined blocks for display.
|
||
*/
|
||
function block_block_view($delta = 0, $edit = array()) {
|
||
$block = db_query('SELECT body, format FROM {block_custom} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
|
||
$data['content'] = check_markup($block->body, $block->format, '', TRUE);
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* Implements hook_page_build().
|
||
*
|
||
* Render blocks into their regions.
|
||
*/
|
||
function block_page_build(&$page) {
|
||
global $theme;
|
||
|
||
// The theme system might not yet be initialized. We need $theme.
|
||
drupal_theme_initialize();
|
||
|
||
// Populate all block regions
|
||
$all_regions = system_region_list($theme);
|
||
|
||
$item = menu_get_item();
|
||
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
|
||
// Load all region content assigned via blocks.
|
||
foreach (array_keys($all_regions) as $region) {
|
||
// Assign blocks to region.
|
||
if ($blocks = block_get_blocks_by_region($region)) {
|
||
$page[$region] = $blocks;
|
||
}
|
||
}
|
||
}
|
||
else {
|
||
// Append region description if we are rendering the regions demo page.
|
||
$item = menu_get_item();
|
||
if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
|
||
$visible_regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
|
||
foreach ($visible_regions as $region) {
|
||
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
|
||
$page[$region]['block_description'] = array(
|
||
'#markup' => $description,
|
||
'#weight' => 15,
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Get a renderable array of a region containing all enabled blocks.
|
||
*
|
||
* @param $region
|
||
* The requested region.
|
||
*/
|
||
function block_get_blocks_by_region($region) {
|
||
$build = array();
|
||
if ($list = block_list($region)) {
|
||
$build = _block_get_renderable_array($list);
|
||
}
|
||
return $build;
|
||
}
|
||
|
||
/**
|
||
* Get an array of blocks suitable for drupal_render().
|
||
*
|
||
* @param $list
|
||
* A list of blocks such as that returned by block_list().
|
||
* @return
|
||
* A renderable array.
|
||
*/
|
||
function _block_get_renderable_array($list = array()) {
|
||
$weight = 0;
|
||
$build = array();
|
||
foreach ($list as $key => $block) {
|
||
$build[$key] = $block->content;
|
||
unset($block->content);
|
||
|
||
// Add contextual links for this block; skipping the system main block.
|
||
if ($key != 'system_main') {
|
||
$build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($block->module, $block->delta));
|
||
}
|
||
|
||
$build[$key] += array(
|
||
'#block' => $block,
|
||
'#weight' => ++$weight,
|
||
);
|
||
$build[$key]['#theme_wrappers'][] ='block';
|
||
}
|
||
$build['#sorted'] = TRUE;
|
||
return $build;
|
||
}
|
||
|
||
/**
|
||
* Update the 'block' DB table with the blocks currently exported by modules.
|
||
*
|
||
* @param $theme
|
||
* The theme to rehash blocks for. If not provided, defaults to the currently
|
||
* used theme.
|
||
*
|
||
* @return
|
||
* Blocks currently exported by modules.
|
||
*/
|
||
function _block_rehash($theme = NULL) {
|
||
global $theme_key;
|
||
|
||
drupal_theme_initialize();
|
||
|
||
if (!isset($theme)) {
|
||
// If theme is not specifically set, rehash for the current theme.
|
||
$theme = $theme_key;
|
||
}
|
||
|
||
$old_blocks = array();
|
||
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme));
|
||
foreach ($result as $old_block) {
|
||
$old_block = is_object($old_block) ? get_object_vars($old_block) : $old_block;
|
||
$old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
|
||
}
|
||
|
||
$blocks = array();
|
||
// Valid region names for the theme.
|
||
$regions = system_region_list($theme);
|
||
|
||
foreach (module_implements('block_info') as $module) {
|
||
$module_blocks = module_invoke($module, 'block_info');
|
||
if ($module_blocks) {
|
||
foreach ($module_blocks as $delta => $block) {
|
||
if (empty($old_blocks[$module][$delta])) {
|
||
// If it's a new block, add identifiers.
|
||
$block['module'] = $module;
|
||
$block['delta'] = $delta;
|
||
$block['theme'] = $theme;
|
||
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'] = '';
|
||
}
|
||
// Add defaults and save it into the database.
|
||
drupal_write_record('block', $block);
|
||
// Set region to none if not enabled.
|
||
$block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
|
||
// Add to the list of blocks we return.
|
||
$blocks[] = $block;
|
||
}
|
||
else {
|
||
// 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'];
|
||
// 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;
|
||
}
|
||
// Add this block to the list of blocks we return.
|
||
$blocks[] = $old_blocks[$module][$delta];
|
||
// Remove this block from the list of blocks to be deleted.
|
||
unset($old_blocks[$module][$delta]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 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) {
|
||
db_delete('block')
|
||
->condition('module', $module)
|
||
->condition('delta', $delta)
|
||
->condition('theme', $theme)
|
||
->execute();
|
||
}
|
||
}
|
||
return $blocks;
|
||
}
|
||
|
||
/**
|
||
* Returns information from database about a user-created (custom) block.
|
||
*
|
||
* @param $bid
|
||
* ID of the block to get information for.
|
||
* @return
|
||
* Associative array of information stored in the database for this block.
|
||
* Array keys:
|
||
* - bid: Block ID.
|
||
* - info: Block description.
|
||
* - body: Block contents.
|
||
* - format: Filter ID of the filter format for the body.
|
||
*/
|
||
function block_custom_block_get($bid) {
|
||
return db_query("SELECT * FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
|
||
}
|
||
|
||
/**
|
||
* Define the custom block form.
|
||
*/
|
||
function block_custom_block_form($edit = array()) {
|
||
$edit += array(
|
||
'info' => '',
|
||
'body' => '',
|
||
);
|
||
$form['info'] = array(
|
||
'#type' => 'textfield',
|
||
'#title' => t('Block description'),
|
||
'#default_value' => $edit['info'],
|
||
'#maxlength' => 64,
|
||
'#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
|
||
'#required' => TRUE,
|
||
'#weight' => -19,
|
||
);
|
||
$form['body_field']['#weight'] = -17;
|
||
$form['body_field']['body'] = array(
|
||
'#type' => 'textarea',
|
||
'#title' => t('Block body'),
|
||
'#default_value' => $edit['body'],
|
||
'#text_format' => isset($edit['format']) ? $edit['format'] : filter_default_format(),
|
||
'#rows' => 15,
|
||
'#description' => t('The content of the block as shown to the user.'),
|
||
'#required' => TRUE,
|
||
'#weight' => -17,
|
||
'#access' => filter_access(filter_format_load($edit['format'])),
|
||
);
|
||
|
||
return $form;
|
||
}
|
||
|
||
/**
|
||
* Saves a user-created block in the database.
|
||
*
|
||
* @param $edit
|
||
* Associative array of fields to save. Array keys:
|
||
* - info: Block description.
|
||
* - body: Block contents.
|
||
* - format: Filter ID of the filter format for the body.
|
||
* @param $delta
|
||
* Block ID of the block to save.
|
||
* @return
|
||
* Always returns TRUE.
|
||
*/
|
||
function block_custom_block_save($edit, $delta) {
|
||
db_update('block_custom')
|
||
->fields(array(
|
||
'body' => $edit['body'],
|
||
'info' => $edit['info'],
|
||
'format' => $edit['body_format'],
|
||
))
|
||
->condition('bid', $delta)
|
||
->execute();
|
||
return TRUE;
|
||
}
|
||
|
||
/**
|
||
* Implements hook_form_FORM_ID_alter().
|
||
*/
|
||
function block_form_user_profile_form_alter(&$form, &$form_state) {
|
||
if ($form['#user_category'] == 'account') {
|
||
$account = $form['#user'];
|
||
$rids = array_keys($account->roles);
|
||
$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));
|
||
|
||
$blocks = array();
|
||
foreach ($result as $block) {
|
||
$data = module_invoke($block->module, 'block_info');
|
||
if ($data[$block->delta]['info']) {
|
||
$blocks[$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),
|
||
);
|
||
}
|
||
}
|
||
// Only display the fieldset if there are any personalizable blocks.
|
||
if ($blocks) {
|
||
$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,
|
||
);
|
||
$form['block'] += $blocks;
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Initialize blocks for enabled themes.
|
||
*/
|
||
function block_themes_enabled($theme_list) {
|
||
foreach ($theme_list as $theme) {
|
||
block_theme_initialize($theme);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Assign an initial, default set of blocks for a theme.
|
||
*
|
||
* 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.
|
||
*
|
||
* @param $theme
|
||
* The name of a theme.
|
||
*/
|
||
function block_theme_initialize($theme) {
|
||
// Initialize theme's blocks if none already registered.
|
||
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', 0, 1, array(':theme' => $theme))->fetchField();
|
||
if (!$has_blocks) {
|
||
$default_theme = variable_get('theme_default', 'garland');
|
||
$regions = system_region_list($theme);
|
||
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $default_theme), array('fetch' => PDO::FETCH_ASSOC));
|
||
foreach ($result as $block) {
|
||
// 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);
|
||
}
|
||
$block['theme'] = $theme;
|
||
unset($block['bid']);
|
||
drupal_write_record('block', $block);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 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
|
||
* 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>.
|
||
*/
|
||
function block_list($region) {
|
||
$blocks = &drupal_static(__FUNCTION__);
|
||
|
||
if (!isset($blocks)) {
|
||
$blocks = _block_load_blocks();
|
||
}
|
||
|
||
// Create an empty array if there are no entries.
|
||
if (!isset($blocks[$region])) {
|
||
$blocks[$region] = array();
|
||
}
|
||
else {
|
||
$blocks[$region] = _block_render_blocks($blocks[$region]);
|
||
}
|
||
|
||
return $blocks[$region];
|
||
}
|
||
|
||
/**
|
||
* Load a block object from the database.
|
||
*
|
||
* @param $module
|
||
* Name of the module that implements the block to load.
|
||
* @param $delta
|
||
* Unique ID of the block within the context of $module. Pass NULL to return
|
||
* an empty $block object for $module.
|
||
*
|
||
* @return
|
||
* A block object.
|
||
*/
|
||
function block_load($module, $delta) {
|
||
if (isset($delta)) {
|
||
$block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta', array(':module' => $module, ':delta' => $delta))->fetchObject();
|
||
}
|
||
|
||
// If the block does not exist in the database yet return a stub block
|
||
// object.
|
||
if (empty($block)) {
|
||
$block = new stdClass;
|
||
$block->module = $module;
|
||
$block->delta = $delta;
|
||
}
|
||
|
||
return $block;
|
||
}
|
||
|
||
/**
|
||
* Load blocks information from the database.
|
||
*/
|
||
function _block_load_blocks() {
|
||
global $theme_key;
|
||
|
||
$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')
|
||
->addTag('translatable')
|
||
->execute();
|
||
|
||
$block_info = $result->fetchAllAssoc('bid');
|
||
// Allow modules to modify the block list.
|
||
drupal_alter('block_info', $block_info);
|
||
|
||
$blocks = array();
|
||
foreach ($block_info as $block) {
|
||
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
|
||
}
|
||
return $blocks;
|
||
}
|
||
|
||
/**
|
||
* Implements hook_block_info_alter().
|
||
*
|
||
* Check the page, user role, content type and user specific visibilty settings.
|
||
* Remove the block if the visibility conditions are not met.
|
||
*/
|
||
function block_block_info_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;
|
||
}
|
||
|
||
// 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;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
// 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.
|
||
if (isset($block_roles[$block->module][$block->delta]) && !array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
|
||
// No match.
|
||
unset($blocks[$key]);
|
||
continue;
|
||
}
|
||
|
||
// 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;
|
||
}
|
||
}
|
||
|
||
// Use the user's block visibility setting, if necessary.
|
||
if ($block->custom != 0) {
|
||
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
|
||
$enabled = $user->block[$block->module][$block->delta];
|
||
}
|
||
else {
|
||
$enabled = ($block->custom == 1);
|
||
}
|
||
}
|
||
else {
|
||
$enabled = TRUE;
|
||
}
|
||
if (!$enabled) {
|
||
unset($blocks[$key]);
|
||
continue;
|
||
}
|
||
|
||
// Match path if necessary.
|
||
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);
|
||
}
|
||
// 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);
|
||
}
|
||
elseif (module_exists('php')) {
|
||
$page_match = php_eval($block->pages);
|
||
}
|
||
else {
|
||
$page_match = FALSE;
|
||
}
|
||
}
|
||
else {
|
||
$page_match = TRUE;
|
||
}
|
||
if (!$page_match) {
|
||
unset($blocks[$key]);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Render the content and subject for a set of blocks.
|
||
*
|
||
* @param $region_blocks
|
||
* An array of block objects such as returned for one region by _block_load_blocks().
|
||
*
|
||
* @return
|
||
* An array of visible blocks as expected by drupal_render().
|
||
*/
|
||
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)) {
|
||
// Erase the block from the static array - we'll put it back if it has
|
||
// content.
|
||
unset($region_blocks[$key]);
|
||
// 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);
|
||
|
||
// Allow modules to modify the block before it is viewed, via either
|
||
// hook_block_view_MODULE_DELTA_alter() or hook_block_view_alter().
|
||
drupal_alter("block_view_{$block->module}_{$block->delta}", $array, $block);
|
||
drupal_alter('block_view', $array, $block);
|
||
|
||
if (isset($cid)) {
|
||
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
|
||
}
|
||
}
|
||
|
||
if (isset($array) && is_array($array)) {
|
||
foreach ($array as $k => $v) {
|
||
$block->$k = $v;
|
||
}
|
||
}
|
||
if (isset($block->content) && $block->content) {
|
||
// Normalize to the drupal_render() structure.
|
||
if (is_string($block->content)) {
|
||
$block->content = array('#markup' => $block->content);
|
||
}
|
||
// 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 = '';
|
||
}
|
||
$region_blocks["{$block->module}_{$block->delta}"] = $block;
|
||
}
|
||
}
|
||
}
|
||
return $region_blocks;
|
||
}
|
||
|
||
/**
|
||
* 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.
|
||
*
|
||
* Theme and language contexts are automatically differentiated.
|
||
*
|
||
* @param $block
|
||
* @return
|
||
* The string used as cache_id for the block.
|
||
*/
|
||
function _block_get_cache_id($block) {
|
||
global $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) && !in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM)) && $user->uid != 1) {
|
||
// Start with common sub-patterns: block identification, theme, language.
|
||
$cid_parts[] = $block->module;
|
||
$cid_parts[] = $block->delta;
|
||
$cid_parts += drupal_render_cid_parts($block->cache);
|
||
|
||
return implode(':', $cid_parts);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Implements hook_flush_caches().
|
||
*/
|
||
function block_flush_caches() {
|
||
return array('cache_block');
|
||
}
|
||
|
||
/**
|
||
* 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) {
|
||
$block_counter = &drupal_static(__FUNCTION__, array());
|
||
$variables['block'] = $variables['elements']['#block'];
|
||
// 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]++;
|
||
|
||
// Create the $content variable that templates expect.
|
||
$variables['content'] = $variables['elements']['#children'];
|
||
|
||
$variables['classes_array'][] = drupal_html_class('block-' . $variables['block']->module);
|
||
|
||
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->region;
|
||
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module;
|
||
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module . '__' . $variables['block']->delta;
|
||
}
|
||
|
||
/**
|
||
* Implements hook_user_role_delete().
|
||
*
|
||
* Remove deleted role from blocks that use it.
|
||
*/
|
||
function block_user_role_delete($role) {
|
||
db_delete('block_role')
|
||
->condition('rid', $role->rid)
|
||
->execute();
|
||
}
|
||
|
||
/**
|
||
* Implements hook_filter_format_delete().
|
||
*/
|
||
function block_filter_format_delete($format, $fallback) {
|
||
db_update('block_custom')
|
||
->fields(array('format' => $fallback->format))
|
||
->condition('format', $format->format)
|
||
->execute();
|
||
}
|
||
|
||
/**
|
||
* Implements hook_menu_delete().
|
||
*/
|
||
function block_menu_delete($menu) {
|
||
db_delete('block')
|
||
->condition('module', 'menu')
|
||
->condition('delta', $menu['menu_name'])
|
||
->execute();
|
||
db_delete('block_role')
|
||
->condition('module', 'menu')
|
||
->condition('delta', $menu['menu_name'])
|
||
->execute();
|
||
}
|
||
|
||
/**
|
||
* Implements hook_form_FORM_ID_alter().
|
||
*/
|
||
function block_form_system_performance_settings_alter(&$form, &$form_state) {
|
||
$disabled = count(module_implements('node_grants'));
|
||
$form['caching']['block_cache'] = array(
|
||
'#type' => 'checkbox',
|
||
'#title' => t('Cache blocks'),
|
||
'#default_value' => variable_get('block_cache', FALSE),
|
||
'#disabled' => $disabled,
|
||
'#description' => $disabled ? t('Block caching is inactive because you have enabled modules defining content access restrictions.') : NULL,
|
||
'#weight' => -1,
|
||
);
|
||
}
|