2001-03-10 11:07:52 +00:00
<?php
2000-12-23 15:20:10 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
2009-08-28 19:44:05 +00:00
* Controls the visual building blocks a page is constructed with.
2004-08-21 06:42:38 +00:00
*/
2008-05-26 17:12:55 +00:00
/**
* Denotes that a block is not enabled in any region and should not be shown.
*/
2011-11-29 09:56:53 +00:00
const BLOCK_REGION_NONE = -1;
2006-11-08 19:27:59 +00:00
2010-06-25 20:25:54 +00:00
/**
* Users cannot control whether or not they see this block.
*/
2011-11-29 09:56:53 +00:00
const BLOCK_CUSTOM_FIXED = 0;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block by default, but lets individual users hide it.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_CUSTOM_ENABLED = 1;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Hides this block by default but lets individual users show it.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_CUSTOM_DISABLED = 2;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block on every page except the listed pages.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_VISIBILITY_NOTLISTED = 0;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block on only the listed pages.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_VISIBILITY_LISTED = 1;
2010-06-25 20:25:54 +00:00
/**
2011-11-03 10:53:06 +00:00
* Shows this block if the associated PHP code returns TRUE.
2010-06-25 20:25:54 +00:00
*/
2011-11-29 09:56:53 +00:00
const BLOCK_VISIBILITY_PHP = 2;
2010-06-25 20:25:54 +00:00
2004-05-11 20:10:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-05-11 20:10:14 +00:00
*/
2007-06-30 19:46:58 +00:00
function block_help($path, $arg) {
switch ($path) {
2003-10-09 18:53:22 +00:00
case 'admin/help#block':
2009-11-19 05:54:42 +00:00
$output = '';
2009-11-22 21:24:37 +00:00
$output .= '<h3>' . t('About') . '</h3>';
2012-03-21 05:51:30 +00:00
$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/documentation/modules/block', '@blocks' => url('admin/structure/block'))) . '</p>';
2009-11-19 05:54:42 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Positioning content') . '</dt>';
2012-03-21 05:51:30 +00:00
$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/documentation/modules/block', '@blocks' => url('admin/structure/block'))) . '</dd>';
2009-11-19 05:54:42 +00:00
$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>';
2009-12-18 07:17:50 +00:00
$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>';
2009-11-19 05:54:42 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2009-07-20 18:51:36 +00:00
case 'admin/structure/block/add':
2010-03-09 03:48:59 +00:00
return '<p>' . t('Use this page to create a new custom block.') . '</p>';
2003-08-05 18:33:39 +00:00
}
2009-11-10 17:27:54 +00:00
if ($arg[0] == 'admin' && $arg[1] == 'structure' && $arg['2'] == 'block' && (empty($arg[3]) || $arg[3] == 'list')) {
2011-12-13 03:29:45 +00:00
$demo_theme = !empty($arg[4]) ? $arg[4] : variable_get('theme_default', 'stark');
2009-10-14 02:13:15 +00:00
$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;
}
2001-01-13 16:33:19 +00:00
}
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function block_theme() {
return array(
2009-04-26 01:15:04 +00:00
'block' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2009-04-26 01:15:04 +00:00
'template' => 'block',
2009-05-24 17:39:35 +00:00
),
2007-10-05 09:35:09 +00:00
'block_admin_display_form' => array(
'template' => 'block-admin-display-form',
2009-05-21 23:07:16 +00:00
'file' => 'block.admin.inc',
2009-10-23 22:24:19 +00:00
'render element' => 'form',
2007-04-06 13:27:23 +00:00
),
);
}
2004-05-11 20:10:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2004-05-11 20:10:14 +00:00
*/
2009-07-05 18:00:11 +00:00
function block_permission() {
2008-02-20 13:46:43 +00:00
return array(
2008-10-09 15:15:55 +00:00
'administer blocks' => array(
'title' => t('Administer blocks'),
),
2008-02-20 13:46:43 +00:00
);
2001-06-29 22:08:57 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2004-04-21 13:56:38 +00:00
*/
2007-01-24 14:48:36 +00:00
function block_menu() {
2011-12-13 03:29:45 +00:00
$default_theme = variable_get('theme_default', 'stark');
2009-07-20 18:51:36 +00:00
$items['admin/structure/block'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Blocks',
'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
2007-10-05 09:35:09 +00:00
'page callback' => 'block_admin_display',
2009-10-14 02:13:15 +00:00
'page arguments' => array($default_theme),
2007-01-24 14:48:36 +00:00
'access arguments' => array('administer blocks'),
2009-08-24 00:14:23 +00:00
'file' => 'block.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-11-11 08:28:50 +00:00
$items['admin/structure/block/manage/%/%'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Configure block',
2007-10-05 12:57:20 +00:00
'page callback' => 'drupal_get_form',
2009-11-11 08:28:50 +00:00
'page arguments' => array('block_admin_configure', 4, 5),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2009-08-24 00:14:23 +00:00
'file' => 'block.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-11-11 08:28:50 +00:00
$items['admin/structure/block/manage/%/%/configure'] = array(
2009-10-17 05:50:29 +00:00
'title' => 'Configure block',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);
2009-11-11 08:28:50 +00:00
$items['admin/structure/block/manage/%/%/delete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Delete block',
2007-10-05 12:57:20 +00:00
'page callback' => 'drupal_get_form',
2009-11-11 08:28:50 +00:00
'page arguments' => array('block_custom_block_delete', 4, 5),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2009-11-11 08:28:50 +00:00
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_NONE,
2009-08-24 00:14:23 +00:00
'file' => 'block.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add block',
2007-10-05 09:35:09 +00:00
'page callback' => 'drupal_get_form',
2007-01-24 14:48:36 +00:00
'page arguments' => array('block_add_block_form'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer blocks'),
2009-08-23 01:05:12 +00:00
'type' => MENU_LOCAL_ACTION,
2009-08-24 00:14:23 +00:00
'file' => 'block.admin.inc',
2007-01-24 14:48:36 +00:00
);
foreach (list_themes() as $key => $theme) {
2009-07-20 18:51:36 +00:00
$items['admin/structure/block/list/' . $key] = array(
2007-12-19 19:09:52 +00:00
'title' => check_plain($theme->info['name']),
'page arguments' => array($key),
2009-10-14 02:13:15 +00:00
'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
'weight' => $key == $default_theme ? -10 : 0,
'access callback' => '_block_themes_access',
2011-04-12 20:42:29 +00:00
'access arguments' => array($key),
2009-10-14 02:13:15 +00:00
'file' => 'block.admin.inc',
);
2010-03-12 05:14:14 +00:00
if ($key != $default_theme) {
$items['admin/structure/block/list/' . $key . '/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',
);
}
2009-10-14 02:13:15 +00:00
$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,
2007-12-19 19:09:52 +00:00
'access callback' => '_block_themes_access',
2011-04-12 20:42:29 +00:00
'access arguments' => array($key),
2009-09-30 13:09:30 +00:00
'theme callback' => '_block_custom_theme',
'theme arguments' => array($key),
2009-08-24 00:14:23 +00:00
'file' => 'block.admin.inc',
2007-12-19 19:09:52 +00:00
);
2004-09-16 07:17:56 +00:00
}
2004-06-18 15:04:37 +00:00
return $items;
2001-06-20 20:00:40 +00:00
}
2007-12-19 19:09:52 +00:00
/**
2011-11-03 10:53:06 +00:00
* Access callback: Only admin or enabled themes can be accessed.
*
* Path:
* - admin/structure/block/list/% (for each theme)
* - admin/structure/block/demo/% (for each theme)
*
* @param $theme
* Either the name of a theme or a full theme object.
*
* @see block_menu()
2007-12-19 19:09:52 +00:00
*/
function _block_themes_access($theme) {
2009-09-30 13:09:30 +00:00
return user_access('administer blocks') && drupal_theme_access($theme);
}
/**
2011-11-03 10:53:06 +00:00
* Theme callback: Uses the theme specified in the parameter.
*
2009-09-30 13:09:30 +00:00
* @param $theme
* The theme whose blocks are being configured. If not set, the default theme
* is assumed.
2011-11-03 10:53:06 +00:00
*
2009-09-30 13:09:30 +00:00
* @return
* The theme that should be used for the block configuration page, or NULL
* to indicate that the default theme should be used.
2011-11-03 10:53:06 +00:00
*
* @see block_menu()
2009-09-30 13:09:30 +00:00
*/
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;
2007-12-19 19:09:52 +00:00
}
2004-05-11 20:10:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_info().
2004-05-11 20:10:14 +00:00
*/
2009-08-29 05:46:04 +00:00
function block_block_info() {
2008-12-16 23:57:33 +00:00
$blocks = array();
2004-10-31 07:34:47 +00:00
2009-08-28 19:44:05 +00:00
$result = db_query('SELECT bid, info FROM {block_custom} ORDER BY info');
2009-05-28 11:31:20 +00:00
foreach ($result as $block) {
2008-12-16 23:57:33 +00:00
$blocks[$block->bid]['info'] = $block->info;
// Not worth caching.
2009-08-31 17:06:10 +00:00
$blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE;
2008-12-16 23:57:33 +00:00
}
return $blocks;
}
2004-10-31 07:34:47 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_configure().
2008-12-16 23:57:33 +00:00
*/
2009-08-23 09:45:07 +00:00
function block_block_configure($delta = 0) {
2008-12-16 23:57:33 +00:00
if ($delta) {
2009-08-28 19:44:05 +00:00
$custom_block = block_custom_block_get($delta);
2008-12-16 23:57:33 +00:00
}
2010-09-28 03:30:37 +00:00
else {
$custom_block = array();
}
2009-08-28 19:44:05 +00:00
return block_custom_block_form($custom_block);
2002-05-02 19:13:45 +00:00
}
2008-12-16 23:57:33 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_save().
2008-12-16 23:57:33 +00:00
*/
function block_block_save($delta = 0, $edit = array()) {
2009-08-28 19:44:05 +00:00
block_custom_block_save($edit, $delta);
2008-12-16 23:57:33 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_view().
2008-12-16 23:57:33 +00:00
*
* Generates the administrator-defined blocks for display.
*/
2010-11-22 08:11:49 +00:00
function block_block_view($delta = '') {
2010-11-23 16:12:15 +00:00
$block = db_query('SELECT body, format FROM {block_custom} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
$data['subject'] = NULL;
$data['content'] = check_markup($block->body, $block->format, '', TRUE);
2008-12-16 23:57:33 +00:00
return $data;
}
2009-01-27 00:22:27 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_page_build().
2009-01-27 00:22:27 +00:00
*
2011-11-03 10:53:06 +00:00
* Renders blocks into their regions.
2009-01-27 00:22:27 +00:00
*/
2009-08-31 16:46:32 +00:00
function block_page_build(&$page) {
2009-01-27 00:22:27 +00:00
global $theme;
// The theme system might not yet be initialized. We need $theme.
2009-07-14 10:22:17 +00:00
drupal_theme_initialize();
2009-01-27 00:22:27 +00:00
2011-03-28 00:28:24 +00:00
// Fetch a list of regions for the current theme.
2009-07-18 02:36:01 +00:00
$all_regions = system_region_list($theme);
2009-01-27 00:22:27 +00:00
2009-10-14 02:13:15 +00:00
$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;
}
2009-08-26 10:53:45 +00:00
}
2010-06-12 08:42:46 +00:00
// Once we've finished attaching all blocks to the page, clear the static
// cache to allow modules to alter the block list differently in different
// contexts. For example, any code that triggers hook_page_build() more
// than once in the same page request may need to alter the block list
// differently each time, so that only certain parts of the page are
// actually built. We do not clear the cache any earlier than this, though,
// because it is used each time block_get_blocks_by_region() gets called
// above.
drupal_static_reset('block_list');
2009-10-14 02:13:15 +00:00
}
else {
// Append region description if we are rendering the regions demo page.
2009-08-26 10:53:45 +00:00
$item = menu_get_item();
2009-10-14 02:13:15 +00:00
if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
$visible_regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
foreach ($visible_regions as $region) {
2009-08-26 10:53:45 +00:00
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
$page[$region]['block_description'] = array(
'#markup' => $description,
'#weight' => 15,
);
2009-01-27 00:22:27 +00:00
}
#655416 by casey, yoroy, BTMash, Bojhan, Gábor Hojtsy, sun, ksenzee, oseldman, jrguitar21, et al: 'Demonstrate block preview' should not open in the overlay, and should reflect the regions in the front-end theme.
2010-07-31 12:54:59 +00:00
$page['page_top']['backlink'] = array(
'#type' => 'link',
'#title' => t('Exit block region demonstration'),
2011-12-13 03:29:45 +00:00
'#href' => 'admin/structure/block' . (variable_get('theme_default', 'stark') == $theme ? '' : '/list/' . $theme),
#655416 by casey, yoroy, BTMash, Bojhan, Gábor Hojtsy, sun, ksenzee, oseldman, jrguitar21, et al: 'Demonstrate block preview' should not open in the overlay, and should reflect the regions in the front-end theme.
2010-07-31 12:54:59 +00:00
// Add the "overlay-restore" class to indicate this link should restore
// the context in which the region demonstration page was opened.
'#options' => array('attributes' => array('class' => array('block-demo-backlink', 'overlay-restore'))),
'#weight' => -10,
);
2009-01-27 00:22:27 +00:00
}
}
}
/**
2011-11-03 10:53:06 +00:00
* Gets a renderable array of a region containing all enabled blocks.
2009-01-27 00:22:27 +00:00
*
* @param $region
* The requested region.
2011-11-03 10:53:06 +00:00
*
* @return
* A renderable array of a region containing all enabled blocks.
2009-01-27 00:22:27 +00:00
*/
function block_get_blocks_by_region($region) {
$build = array();
if ($list = block_list($region)) {
2011-09-29 03:29:59 +00:00
$build = _block_get_renderable_region($list);
2009-10-13 13:54:55 +00:00
}
return $build;
}
/**
2011-11-03 10:53:06 +00:00
* Gets an array of blocks suitable for drupal_render().
2009-10-13 13:54:55 +00:00
*
* @param $list
* A list of blocks such as that returned by block_list().
2011-11-03 10:53:06 +00:00
*
2009-10-13 13:54:55 +00:00
* @return
* A renderable array.
*/
2011-09-29 03:29:59 +00:00
function _block_get_renderable_region($list = array()) {
2009-10-13 13:54:55 +00:00
$weight = 0;
$build = array();
2012-02-29 16:49:38 +00:00
// 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'). 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.
$not_cacheable = $GLOBALS['user']->uid == 1 ||
count(module_implements('node_grants')) ||
!in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'));
2009-10-13 13:54:55 +00:00
foreach ($list as $key => $block) {
2011-09-29 03:29:59 +00:00
$build[$key] = array(
'#block' => $block,
'#weight' => ++$weight,
'#theme_wrappers' => array('block'),
);
2012-02-29 16:49:38 +00:00
if ($not_cacheable || in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))) {
2011-09-29 03:29:59 +00:00
// Non-cached blocks get built immediately. Provides more content
// that can be easily manipulated during hook_page_alter().
$build[$key] = _block_get_renderable_block($build[$key]);
}
else {
$build[$key] += array(
'#pre_render' => array('_block_get_renderable_block'),
'#cache' => array(
'keys' => array($block->module, $block->delta),
'granularity' => $block->cache,
'bin' => 'block',
2012-06-13 01:37:07 +00:00
'tags' => array('content' => TRUE),
2011-09-29 03:29:59 +00:00
),
);
}
2009-10-17 05:50:29 +00:00
2010-09-12 00:20:36 +00:00
// Add contextual links for this block; skip the main content block, since
// contextual links are basically output as tabs/local tasks already. Also
// skip the help block, since we assume that most users do not need or want
// to perform contextual actions on the help block, and the links needlessly
// draw attention on it.
if ($key != 'system_main' && $key != 'system_help') {
2009-11-07 13:35:21 +00:00
$build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($block->module, $block->delta));
2009-10-17 05:50:29 +00:00
}
2009-01-27 00:22:27 +00:00
}
2009-10-13 13:54:55 +00:00
$build['#sorted'] = TRUE;
2009-01-27 00:22:27 +00:00
return $build;
}
2002-10-30 08:12:28 +00:00
/**
2011-11-03 10:53:06 +00:00
* Updates the 'block' DB table with the blocks currently exported by modules.
2002-11-01 10:47:20 +00:00
*
2011-05-24 01:07:02 +00:00
* @param $theme
2009-10-14 02:13:15 +00:00
* The theme to rehash blocks for. If not provided, defaults to the currently
* used theme.
2009-11-10 17:27:54 +00:00
*
2004-05-11 20:10:14 +00:00
* @return
2006-04-04 23:32:40 +00:00
* Blocks currently exported by modules.
2002-10-30 08:12:28 +00:00
*/
2009-10-14 02:13:15 +00:00
function _block_rehash($theme = NULL) {
2005-08-16 18:06:18 +00:00
global $theme_key;
2009-11-10 17:27:54 +00:00
2009-07-14 10:22:17 +00:00
drupal_theme_initialize();
2009-10-14 02:13:15 +00:00
if (!isset($theme)) {
// If theme is not specifically set, rehash for the current theme.
$theme = $theme_key;
}
$regions = system_region_list($theme);
2002-10-26 15:17:26 +00:00
2010-04-22 09:12:35 +00:00
// These are the blocks the function will return.
$blocks = array();
// These are the blocks defined by code and modified by the database.
$current_blocks = array();
// These are {block}.bid values to be kept.
$bids = array();
$or = db_or();
// Gather the blocks defined by modules.
2009-08-29 05:46:04 +00:00
foreach (module_implements('block_info') as $module) {
$module_blocks = module_invoke($module, 'block_info');
2010-04-22 09:12:35 +00:00
foreach ($module_blocks as $delta => $block) {
// Compile a condition to retrieve this block from the database.
$condition = db_and()
->condition('module', $module)
->condition('delta', $delta);
$or->condition($condition);
// Add identifiers.
$block['module'] = $module;
$block['delta'] = $delta;
$block['theme'] = $theme;
$current_blocks[$module][$delta] = $block;
2002-10-26 15:17:26 +00:00
}
}
2010-04-22 09:12:35 +00:00
// Save the blocks defined in code for alter context.
$code_blocks = $current_blocks;
$database_blocks = db_select('block', 'b')
->fields('b')
->condition($or)
->condition('theme', $theme)
->execute();
foreach ($database_blocks as $block) {
// Preserve info which is not in the database.
$block->info = $current_blocks[$block->module][$block->delta]['info'];
2010-06-15 16:19:28 +00:00
// The cache mode can only by set from hook_block_info(), so that has
// precedence over the database's value.
if (isset($current_blocks[$block->module][$block->delta]['cache'])) {
$block->cache = $current_blocks[$block->module][$block->delta]['cache'];
}
2010-04-22 09:12:35 +00:00
// Blocks stored in the database override the blocks defined in code.
$current_blocks[$block->module][$block->delta] = get_object_vars($block);
// Preserve this block.
$bids[$block->bid] = $block->bid;
}
drupal_alter('block_info', $current_blocks, $theme, $code_blocks);
foreach ($current_blocks as $module => $module_blocks) {
foreach ($module_blocks as $delta => $block) {
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'] = '';
}
// Make sure weight is set.
if (!isset($block['weight'])) {
$block['weight'] = 0;
}
2012-04-28 01:08:33 +00:00
if (!empty($block['region']) && $block['region'] != BLOCK_REGION_NONE && !isset($regions[$block['region']]) && $block['status'] == 1) {
2010-04-22 09:12:35 +00:00
drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block['info'], '%region' => $block['region'])), 'warning');
// Disabled modules are moved into the BLOCK_REGION_NONE later so no
2012-04-28 01:08:33 +00:00
// need to move the block to another region.
2010-04-22 09:12:35 +00:00
$block['status'] = 0;
}
// Set region to none if not enabled and make sure status is set.
if (empty($block['status'])) {
$block['status'] = 0;
$block['region'] = BLOCK_REGION_NONE;
}
// There is no point saving disabled blocks. Still, we need to save them
2010-06-15 16:19:28 +00:00
// because the 'title' attribute is saved to the {blocks} table.
2010-04-22 09:12:35 +00:00
if (isset($block['bid'])) {
// If the block has a bid property, it comes from the database and
// the record needs to be updated, so set the primary key to 'bid'
// before passing to drupal_write_record().
$primary_keys = array('bid');
// Remove a block from the list of blocks to keep if it became disabled.
unset($bids[$block['bid']]);
}
else {
$primary_keys = array();
}
drupal_write_record('block', $block, $primary_keys);
// Add to the list of blocks we return.
$blocks[] = $block;
2007-11-06 11:40:15 +00:00
}
2006-08-20 06:49:15 +00:00
}
2010-04-22 09:12:35 +00:00
if ($bids) {
// Remove disabled that are no longer defined by the code from the
// database.
db_delete('block')
->condition('bid', $bids, 'NOT IN')
->condition('theme', $theme)
->execute();
}
2002-10-26 15:17:26 +00:00
return $blocks;
}
2001-01-26 13:38:46 +00:00
2009-10-03 22:08:03 +00:00
/**
* Returns information from database about a user-created (custom) block.
*
* @param $bid
* ID of the block to get information for.
2011-11-03 10:53:06 +00:00
*
2009-10-03 22:08:03 +00:00
* @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.
*/
2009-08-28 19:44:05 +00:00
function block_custom_block_get($bid) {
return db_query("SELECT * FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
2004-05-11 20:10:14 +00:00
}
2007-01-23 16:44:58 +00:00
/**
2011-11-03 10:53:06 +00:00
* Form constructor for the custom block form.
*
* @param $edit
* (optional) An associative array of information retrieved by
* block_custom_get_block() if an existing block is being edited, or an empty
* array otherwise. Defaults to array().
*
* @ingroup forms
2007-01-23 16:44:58 +00:00
*/
2009-08-28 19:44:05 +00:00
function block_custom_block_form($edit = array()) {
2007-03-27 05:13:55 +00:00
$edit += array(
'info' => '',
'body' => '',
);
2006-01-24 08:38:29 +00:00
$form['info'] = array(
'#type' => 'textfield',
'#title' => t('Block description'),
2007-01-25 22:14:06 +00:00
'#default_value' => $edit['info'],
2006-01-24 08:38:29 +00:00
'#maxlength' => 64,
2010-01-08 11:10:32 +00:00
'#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
2006-01-24 08:38:29 +00:00
'#required' => TRUE,
2011-09-11 21:06:35 +00:00
'#weight' => -18,
2006-01-24 08:38:29 +00:00
);
2007-04-09 13:58:03 +00:00
$form['body_field']['#weight'] = -17;
$form['body_field']['body'] = array(
2010-03-07 23:14:20 +00:00
'#type' => 'text_format',
2005-12-05 09:11:33 +00:00
'#title' => t('Block body'),
2007-01-25 22:14:06 +00:00
'#default_value' => $edit['body'],
2010-03-07 23:14:20 +00:00
'#format' => isset($edit['format']) ? $edit['format'] : NULL,
2005-12-05 09:11:33 +00:00
'#rows' => 15,
'#description' => t('The content of the block as shown to the user.'),
2009-03-17 03:42:30 +00:00
'#required' => TRUE,
2005-12-05 09:11:33 +00:00
'#weight' => -17,
);
2002-05-02 19:13:45 +00:00
2005-10-07 06:11:12 +00:00
return $form;
2002-05-02 19:13:45 +00:00
}
2009-10-03 22:08:03 +00:00
/**
* Saves a user-created block in the database.
*
* @param $edit
* Associative array of fields to save. Array keys:
* - info: Block description.
2010-05-13 07:53:02 +00:00
* - body: Associative array of body value and format. Array keys:
* - value: Block contents.
* - format: Filter ID of the filter format for the body.
2009-10-03 22:08:03 +00:00
* @param $delta
* Block ID of the block to save.
2011-11-03 10:53:06 +00:00
*
2009-10-03 22:08:03 +00:00
* @return
* Always returns TRUE.
*/
2009-08-28 19:44:05 +00:00
function block_custom_block_save($edit, $delta) {
db_update('block_custom')
2009-05-28 11:31:20 +00:00
->fields(array(
2010-05-13 07:53:02 +00:00
'body' => $edit['body']['value'],
2009-05-28 11:31:20 +00:00
'info' => $edit['info'],
2010-05-13 07:53:02 +00:00
'format' => $edit['body']['format'],
2009-05-28 11:31:20 +00:00
))
->condition('bid', $delta)
->execute();
2006-05-26 09:21:10 +00:00
return TRUE;
2002-05-02 19:13:45 +00:00
}
2004-05-11 20:10:14 +00:00
/**
2011-11-03 10:53:06 +00:00
* Implements hook_form_FORM_ID_alter() for user_profile_form().
2004-05-11 20:10:14 +00:00
*/
2009-09-22 07:50:16 +00:00
function block_form_user_profile_form_alter(&$form, &$form_state) {
2011-11-03 07:34:18 +00:00
$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->data['block'][$block->module][$block->delta]) ? $account->data['block'][$block->module][$block->delta] : ($block->custom == 1),
2009-10-10 16:48:39 +00:00
);
2008-10-06 11:30:12 +00:00
}
}
2011-11-03 07:34:18 +00:00
// 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;
}
2008-10-06 11:30:12 +00:00
}
2012-05-15 02:11:31 +00:00
/**
* Implements hook_field_extra_fields().
*/
function block_field_extra_fields() {
$extra['user']['user']['form']['block'] = array(
'label' => t('Personalize blocks'),
'description' => t('Block module form element.'),
'weight' => 3,
);
return $extra;
}
2010-11-22 06:12:39 +00:00
/**
* Implements hook_user_presave().
*/
2012-04-26 03:51:09 +00:00
function block_user_presave($account) {
if (isset($account->block)) {
$account->data['block'] = $account->block;
2010-11-22 06:12:39 +00:00
}
}
2009-04-21 09:31:31 +00:00
/**
2011-11-03 10:53:06 +00:00
* Initializes blocks for enabled themes.
*
* @param $theme_list
* An array of theme names.
2009-04-21 09:31:31 +00:00
*/
2009-12-01 00:39:35 +00:00
function block_themes_enabled($theme_list) {
foreach ($theme_list as $theme) {
block_theme_initialize($theme);
2009-04-21 09:31:31 +00:00
}
}
/**
2011-11-03 10:53:06 +00:00
* Assigns an initial, default set of blocks for a theme.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* This function is called the first time a new theme is enabled. The new theme
* gets a copy of the default theme's blocks, with the difference that if a
* particular region isn't available in the new theme, the block is assigned
* to the new theme's default region.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* @param $theme
* The name of a theme.
*/
2009-07-14 10:22:17 +00:00
function block_theme_initialize($theme) {
2009-04-21 09:31:31 +00:00
// Initialize theme's blocks if none already registered.
2009-09-18 00:04:24 +00:00
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', 0, 1, array(':theme' => $theme))->fetchField();
2009-05-16 15:23:16 +00:00
if (!$has_blocks) {
2011-12-13 03:29:45 +00:00
$default_theme = variable_get('theme_default', 'stark');
2011-07-03 17:57:21 +00:00
// Apply only to new theme's visible regions.
$regions = system_region_list($theme, REGIONS_VISIBLE);
2009-05-28 11:31:20 +00:00
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $default_theme), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $block) {
2009-04-21 09:31:31 +00:00
// If the region isn't supported by the theme, assign the block to the theme's default region.
2010-10-28 02:27:09 +00:00
if ($block['status'] && !isset($regions[$block['region']])) {
2009-04-21 09:31:31 +00:00
$block['region'] = system_default_region($theme);
}
2009-05-28 11:31:20 +00:00
$block['theme'] = $theme;
2009-08-22 13:25:37 +00:00
unset($block['bid']);
drupal_write_record('block', $block);
2009-04-21 09:31:31 +00:00
}
}
}
2005-08-16 18:06:18 +00:00
/**
2011-11-03 10:53:06 +00:00
* Returns all blocks in the specified region for the current user.
2005-08-16 18:06:18 +00:00
*
* @param $region
* The name of a region.
*
* @return
2011-06-22 06:46:30 +00:00
* An array of block objects, indexed with the module name and block delta
* concatenated with an underscore, thus: MODULE_DELTA. 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.
2005-08-16 18:06:18 +00:00
*
* @todo
2011-11-03 10:53:06 +00:00
* Now that the block table has a primary key, we should use that as the
2011-06-22 06:46:30 +00:00
* array key instead of MODULE_DELTA.
2005-08-16 18:06:18 +00:00
*/
function block_list($region) {
2009-11-11 08:52:53 +00:00
$blocks = &drupal_static(__FUNCTION__);
2003-11-19 16:13:07 +00:00
2009-11-11 08:52:53 +00:00
if (!isset($blocks)) {
2008-03-21 08:41:25 +00:00
$blocks = _block_load_blocks();
}
2009-11-11 08:52:53 +00:00
// Create an empty array if there are no entries.
2008-03-21 08:41:25 +00:00
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
return $blocks[$region];
}
2009-10-16 23:48:38 +00:00
/**
2011-11-03 10:53:06 +00:00
* Loads a block object from the database.
2009-10-16 23:48:38 +00:00
*
* @param $module
* Name of the module that implements the block to load.
* @param $delta
2009-11-11 08:28:50 +00:00
* Unique ID of the block within the context of $module. Pass NULL to return
2011-09-15 02:11:13 +00:00
* an empty block object for $module.
2009-10-16 23:48:38 +00:00
*
* @return
* A block object.
*/
function block_load($module, $delta) {
2009-11-11 08:28:50 +00:00
if (isset($delta)) {
$block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta', array(':module' => $module, ':delta' => $delta))->fetchObject();
}
2009-10-16 23:48:38 +00:00
// If the block does not exist in the database yet return a stub block
// object.
2009-11-11 08:28:50 +00:00
if (empty($block)) {
2010-06-17 13:16:57 +00:00
$block = new stdClass();
2009-10-16 23:48:38 +00:00
$block->module = $module;
$block->delta = $delta;
}
return $block;
}
2008-03-21 08:41:25 +00:00
/**
2011-11-03 10:53:06 +00:00
* Loads blocks' information from the database.
*
* @return
* An array of blocks grouped by region.
2008-03-21 08:41:25 +00:00
*/
function _block_load_blocks() {
2009-06-08 09:23:55 +00:00
global $theme_key;
2008-03-21 08:41:25 +00:00
2009-05-28 11:31:20 +00:00
$query = db_select('block', 'b');
2011-09-29 03:29:59 +00:00
$query->addField('b', 'title', 'subject');
2009-05-28 11:31:20 +00:00
$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')
2009-10-16 19:06:25 +00:00
->addTag('translatable')
2009-05-28 11:31:20 +00:00
->execute();
2009-06-08 09:23:55 +00:00
2009-08-29 05:46:04 +00:00
$block_info = $result->fetchAllAssoc('bid');
2009-06-08 09:23:55 +00:00
// Allow modules to modify the block list.
2010-04-22 09:12:35 +00:00
drupal_alter('block_list', $block_info);
2009-06-08 09:23:55 +00:00
$blocks = array();
2009-08-29 05:46:04 +00:00
foreach ($block_info as $block) {
2009-06-08 09:23:55 +00:00
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
return $blocks;
}
/**
2010-04-22 09:12:35 +00:00
* Implements hook_block_list_alter().
2009-06-08 09:23:55 +00:00
*
2011-11-03 10:53:06 +00:00
* Checks the page, user role, and user-specific visibilty settings.
* Removes the block if the visibility conditions are not met.
2009-06-08 09:23:55 +00:00
*/
2010-04-22 09:12:35 +00:00
function block_block_list_alter(&$blocks) {
2009-06-08 09:23:55 +00:00
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;
}
2012-04-19 17:45:46 +00:00
// Build an array of langcodes allowed per block.
$result = db_query('SELECT module, delta, type, langcode FROM {block_language}');
$block_langcodes = array();
foreach ($result as $record) {
$block_langcodes[$record->module][$record->delta][$record->type][$record->langcode] = TRUE;
}
2009-06-08 09:23:55 +00:00
foreach ($blocks as $key => $block) {
2010-03-28 11:16:29 +00:00
if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
2009-06-08 09:23:55 +00:00
// This block was added by a contrib module, leave it in the list.
continue;
2008-03-21 08:41:25 +00:00
}
2009-06-08 09:23:55 +00:00
// If a block has no roles associated, it is displayed for every role.
// For blocks with roles associated, if none of the user's roles matches
// the settings from this block, remove it from the block list.
2009-07-01 08:04:19 +00:00
if (isset($block_roles[$block->module][$block->delta]) && !array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
2009-06-08 09:23:55 +00:00
// No match.
unset($blocks[$key]);
continue;
2009-07-01 08:04:19 +00:00
}
2008-05-15 21:30:02 +00:00
// Use the user's block visibility setting, if necessary.
2010-06-25 20:25:54 +00:00
if ($block->custom != BLOCK_CUSTOM_FIXED) {
2010-11-22 06:12:39 +00:00
if ($user->uid && isset($user->data['block'][$block->module][$block->delta])) {
$enabled = $user->data['block'][$block->module][$block->delta];
2004-10-31 07:34:47 +00:00
}
else {
2010-06-25 20:25:54 +00:00
$enabled = ($block->custom == BLOCK_CUSTOM_ENABLED);
2004-02-25 21:19:31 +00:00
}
2008-03-21 08:41:25 +00:00
}
else {
$enabled = TRUE;
}
2011-08-26 09:09:24 +00:00
// Limited visibility blocks must list at least one page.
if ($block->visibility == BLOCK_VISIBILITY_LISTED && empty($block->pages)) {
$enabled = FALSE;
}
2009-06-08 09:23:55 +00:00
if (!$enabled) {
unset($blocks[$key]);
continue;
}
2004-08-06 11:10:15 +00:00
2008-05-15 21:30:02 +00:00
// Match path if necessary.
2008-03-21 08:41:25 +00:00
if ($block->pages) {
2010-04-22 22:08:43 +00:00
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower($block->pages);
2010-06-25 20:25:54 +00:00
if ($block->visibility < BLOCK_VISIBILITY_PHP) {
2012-04-29 15:16:27 +00:00
// Compare the lowercase path alias (if any) and internal path.
$path = current_path();
$path_alias = drupal_strtolower(drupal_get_path_alias($path));
$page_match = drupal_match_path($path_alias, $pages) || (($path != $path_alias) && drupal_match_path($path, $pages));
2010-06-25 20:25:54 +00:00
// When $block->visibility has a value of 0 (BLOCK_VISIBILITY_NOTLISTED),
// the block is displayed on all pages except those listed in $block->pages.
// When set to 1 (BLOCK_VISIBILITY_LISTED), it is displayed only on those
// pages listed in $block->pages.
2008-03-21 08:41:25 +00:00
$page_match = !($block->visibility xor $page_match);
2004-08-06 11:10:15 +00:00
}
2009-05-07 15:29:08 +00:00
elseif (module_exists('php')) {
$page_match = php_eval($block->pages);
}
2004-08-06 11:10:15 +00:00
else {
2009-05-07 15:29:08 +00:00
$page_match = FALSE;
2004-08-06 11:10:15 +00:00
}
2008-03-21 08:41:25 +00:00
}
else {
$page_match = TRUE;
}
2009-06-08 09:23:55 +00:00
if (!$page_match) {
unset($blocks[$key]);
2012-04-19 17:45:46 +00:00
continue;
}
// Language visibility settings.
// No language setting for this block, leave it in the list.
if (!isset($block_langcodes[$block->module][$block->delta])) {
continue;
}
foreach ($block_langcodes[$block->module][$block->delta] as $language_type => $langcodes) {
2012-06-14 08:22:49 +00:00
if (isset($langcodes[drupal_container()->get($language_type)->langcode])) {
2012-04-19 17:45:46 +00:00
// Found a language type - langcode combination in the configuration
// that is applicable to the current request.
continue 2;
}
2009-06-08 09:23:55 +00:00
}
2012-04-19 17:45:46 +00:00
// Had language configuration but none matched.
unset($blocks[$key]);
2008-03-21 08:41:25 +00:00
}
}
2005-09-14 21:37:11 +00:00
2008-03-21 08:41:25 +00:00
/**
2011-11-03 10:53:06 +00:00
* Builds the content and subject for a block.
*
* For cacheable blocks, this is called during #pre_render.
2008-03-21 08:41:25 +00:00
*
2011-09-29 03:29:59 +00:00
* @param $element
* A renderable array.
2011-11-03 10:53:06 +00:00
*
2008-03-21 08:41:25 +00:00
* @return
2011-09-29 03:29:59 +00:00
* A renderable array.
2008-03-21 08:41:25 +00:00
*/
2011-09-29 03:29:59 +00:00
function _block_get_renderable_block($element) {
$block = $element['#block'];
2009-10-17 05:50:29 +00:00
2011-09-29 03:29:59 +00:00
// Render the block content if it has not been created already.
if (!isset($block->content)) {
$array = module_invoke($block->module, 'block_view', $block->delta);
2009-10-17 05:50:29 +00:00
2011-09-29 03:29:59 +00:00
// Allow modules to modify the block before it is viewed, via either
// hook_block_view_alter() or hook_block_view_MODULE_DELTA_alter().
drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);
2007-08-19 08:08:45 +00:00
2011-10-06 07:01:47 +00:00
if (empty($array['content'])) {
2011-09-29 03:29:59 +00:00
// Blocks without content should emit no markup at all.
$element += array(
'#access' => FALSE,
'#printed' => TRUE,
);
}
elseif (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
2003-11-19 16:13:07 +00:00
}
}
}
2007-08-19 08:08:45 +00:00
2011-09-29 03:29:59 +00:00
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);
}
// Add the content renderable array to the main element.
$element['content'] = $block->content;
unset($block->content);
$element['#block'] = $block;
2007-08-19 08:08:45 +00:00
}
2011-09-29 03:29:59 +00:00
return $element;
2007-11-26 16:36:44 +00:00
}
2009-02-03 12:30:14 +00:00
/**
2012-05-03 15:09:39 +00:00
* Implements hook_cache_flush().
2009-02-03 12:30:14 +00:00
*/
2012-05-03 15:09:39 +00:00
function block_cache_flush() {
return array('block');
}
/**
* Implements hook_rebuild().
*/
function block_rebuild() {
2010-06-15 16:19:28 +00:00
// Rehash blocks for active themes. We don't use list_themes() here,
// because if MAINTENANCE_MODE is defined it skips reading the database,
// and we can't tell which themes are active.
$themes = db_query("SELECT name FROM {system} WHERE type = 'theme' AND status = 1");
foreach ($themes as $theme) {
_block_rehash($theme->name);
}
2009-02-03 12:30:14 +00:00
}
2009-04-26 01:15:04 +00:00
/**
2011-11-03 10:53:06 +00:00
* Processes variables for block.tpl.php.
2009-04-26 01:15:04 +00:00
*
2011-11-03 10:53:06 +00:00
* Prepares the values passed to the theme_block function to be passed
2009-04-26 01:15:04 +00:00
* into a pluggable template engine. Uses block properties to generate a
* series of template file suggestions. If none are found, the default
* block.tpl.php is used.
*
* Most themes utilize their own copy of block.tpl.php. The default is located
* inside "modules/block/block.tpl.php". Look in there for the full list of
* variables.
*
* The $variables array contains the following arguments:
* - $block
*
* @see block.tpl.php
*/
function template_preprocess_block(&$variables) {
2009-06-07 02:29:07 +00:00
$block_counter = &drupal_static(__FUNCTION__, array());
2009-06-12 09:02:55 +00:00
$variables['block'] = $variables['elements']['#block'];
2009-04-26 01:15:04 +00:00
// All blocks get an independent counter for each region.
if (!isset($block_counter[$variables['block']->region])) {
$block_counter[$variables['block']->region] = 1;
}
// Same with zebra striping.
$variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';
$variables['block_id'] = $block_counter[$variables['block']->region]++;
2009-06-12 09:02:55 +00:00
// Create the $content variable that templates expect.
$variables['content'] = $variables['elements']['#children'];
2009-05-21 21:12:25 +00:00
2009-10-05 01:18:26 +00:00
$variables['classes_array'][] = drupal_html_class('block-' . $variables['block']->module);
2009-05-28 16:44:07 +00:00
2011-09-24 20:15:40 +00:00
// Add default class for block content.
$variables['content_attributes_array']['class'][] = 'content';
2010-01-13 05:40:03 +00:00
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->region;
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module;
2011-05-22 00:10:25 +00:00
// Hyphens (-) and underscores (_) play a special role in theme suggestions.
// Theme suggestions should only contain underscores, because within
// drupal_find_theme_templates(), underscores are converted to hyphens to
2011-07-19 05:56:55 +00:00
// match template file names, and then converted back to underscores to match
2011-05-22 00:10:25 +00:00
// pre-processing and other function names. So if your theme suggestion
// contains a hyphen, it will end up as an underscore after this conversion,
// and your function names won't be recognized. So, we need to convert
// hyphens to underscores in block deltas for the theme suggestions.
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module . '__' . strtr($variables['block']->delta, '-', '_');
2010-04-26 14:10:40 +00:00
// Create a valid HTML ID and make sure it is unique.
$variables['block_html_id'] = drupal_html_id('block-' . $variables['block']->module . '-' . $variables['block']->delta);
2009-05-28 16:44:07 +00:00
}
2009-08-26 10:29:26 +00:00
2009-08-27 20:25:29 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_role_delete().
2009-08-27 20:25:29 +00:00
*
2011-11-03 10:53:06 +00:00
* Removes deleted role from blocks that use it.
2009-08-27 20:25:29 +00:00
*/
function block_user_role_delete($role) {
db_delete('block_role')
->condition('rid', $role->rid)
->execute();
}
2009-10-09 08:02:25 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu_delete().
2009-10-09 08:02:25 +00:00
*/
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();
}
#655416 by casey, yoroy, BTMash, Bojhan, Gábor Hojtsy, sun, ksenzee, oseldman, jrguitar21, et al: 'Demonstrate block preview' should not open in the overlay, and should reflect the regions in the front-end theme.
2010-07-31 12:54:59 +00:00
/**
* Implements hook_admin_paths().
*/
function block_admin_paths() {
$paths = array(
// Exclude the block demonstration page from admin (overlay) treatment.
// This allows us to present this page in its true form, full page.
'admin/structure/block/demo/*' => FALSE,
);
return $paths;
}
2010-03-09 12:09:52 +00:00
/**
* Implements hook_modules_uninstalled().
*
2012-04-19 17:45:46 +00:00
* Cleans up {block}, {block_role} and {block_language} tables
* from modules' blocks.
2010-03-09 12:09:52 +00:00
*/
function block_modules_uninstalled($modules) {
db_delete('block')
->condition('module', $modules, 'IN')
->execute();
db_delete('block_role')
->condition('module', $modules, 'IN')
->execute();
2012-04-19 17:45:46 +00:00
db_delete('block_language')
->condition('module', $modules, 'IN')
->execute();
}
/**
* Implements hook_language_delete().
*
* Delete the potential block visibility settings of the deleted language.
*/
function block_language_delete($language) {
// Remove the block visibility settings for the deleted language.
db_delete('block_language')
->condition('langcode', $language->langcode)
->execute();
2010-03-09 12:09:52 +00:00
}