<?php
const BLOCK_REGION_NONE = -1;
const BLOCK_CUSTOM_FIXED = 0;
const BLOCK_CUSTOM_ENABLED = 1;
const BLOCK_CUSTOM_DISABLED = 2;
const BLOCK_VISIBILITY_NOTLISTED = 0;
const BLOCK_VISIBILITY_LISTED = 1;
const BLOCK_VISIBILITY_PHP = 2;
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" and "Help", and a block may appear in either 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>';
$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/documentation/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.') . '</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', 'stark');
$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;
}
}
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',
),
);
}
function block_permission() {
return array(
'administer blocks' => array(
'title' => t('Administer blocks'),
),
);
}
function block_menu() {
$default_theme = variable_get('theme_default', 'stark');
$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/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(
$key,
),
'file' => 'block.admin.inc',
);
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',
);
}
$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(
$key,
),
'theme callback' => '_block_custom_theme',
'theme arguments' => array(
$key,
),
'file' => 'block.admin.inc',
);
}
return $items;
}
function _block_themes_access($theme) {
return user_access('administer blocks') && drupal_theme_access($theme);
}
function _block_custom_theme($theme = NULL) {
return $theme;
}
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;
$blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE;
}
return $blocks;
}
function block_block_configure($delta = 0) {
if ($delta) {
$custom_block = block_custom_block_get($delta);
}
else {
$custom_block = array();
}
return block_custom_block_form($custom_block);
}
function block_block_save($delta = 0, $edit = array()) {
block_custom_block_save($edit, $delta);
}
function block_block_view($delta = '') {
$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);
return $data;
}
function block_page_build(&$page) {
global $theme;
drupal_theme_initialize();
$all_regions = system_region_list($theme);
$item = menu_get_item();
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
foreach (array_keys($all_regions) as $region) {
if ($blocks = block_get_blocks_by_region($region)) {
$page[$region] = $blocks;
}
}
drupal_static_reset('block_list');
}
else {
$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,
);
}
$page['page_top']['backlink'] = array(
'#type' => 'link',
'#title' => t('Exit block region demonstration'),
'#href' => 'admin/structure/block' . (variable_get('theme_default', 'stark') == $theme ? '' : '/list/' . $theme),
'#options' => array(
'attributes' => array(
'class' => array(
'block-demo-backlink',
'overlay-restore',
),
),
),
'#weight' => -10,
);
}
}
}
function block_get_blocks_by_region($region) {
$build = array();
if ($list = block_list($region)) {
$build = _block_get_renderable_region($list);
}
return $build;
}
function _block_get_renderable_region($list = array()) {
$weight = 0;
$build = array();
$not_cacheable = $GLOBALS['user']->uid == 1 || count(module_implements('node_grants')) || !in_array($_SERVER['REQUEST_METHOD'], array(
'GET',
'HEAD',
));
foreach ($list as $key => $block) {
$build[$key] = array(
'#block' => $block,
'#weight' => ++$weight,
'#theme_wrappers' => array(
'block',
),
);
if ($not_cacheable || in_array($block->cache, array(
DRUPAL_NO_CACHE,
DRUPAL_CACHE_CUSTOM,
))) {
$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',
'tags' => array(
'content' => TRUE,
),
),
);
}
if ($key != 'system_main' && $key != 'system_help') {
$build[$key]['#contextual_links']['block'] = array(
'admin/structure/block/manage',
array(
$block->module,
$block->delta,
),
);
}
}
$build['#sorted'] = TRUE;
return $build;
}
function _block_rehash($theme = NULL) {
global $theme_key;
drupal_theme_initialize();
if (!isset($theme)) {
$theme = $theme_key;
}
$regions = system_region_list($theme);
$blocks = array();
$current_blocks = array();
$bids = array();
$or = db_or();
foreach (module_implements('block_info') as $module) {
$module_blocks = module_invoke($module, 'block_info');
foreach ($module_blocks as $delta => $block) {
$condition = db_and()
->condition('module', $module)
->condition('delta', $delta);
$or
->condition($condition);
$block['module'] = $module;
$block['delta'] = $delta;
$block['theme'] = $theme;
$current_blocks[$module][$delta] = $block;
}
}
$code_blocks = $current_blocks;
$database_blocks = db_select('block', 'b')
->fields('b')
->condition($or)
->condition('theme', $theme)
->execute();
foreach ($database_blocks as $block) {
$block->info = $current_blocks[$block->module][$block->delta]['info'];
if (isset($current_blocks[$block->module][$block->delta]['cache'])) {
$block->cache = $current_blocks[$block->module][$block->delta]['cache'];
}
$current_blocks[$block->module][$block->delta] = get_object_vars($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'] = '';
}
if (!isset($block['weight'])) {
$block['weight'] = 0;
}
if (!empty($block['region']) && $block['region'] != BLOCK_REGION_NONE && !isset($regions[$block['region']]) && $block['status'] == 1) {
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');
$block['status'] = 0;
}
if (empty($block['status'])) {
$block['status'] = 0;
$block['region'] = BLOCK_REGION_NONE;
}
if (isset($block['bid'])) {
$primary_keys = array(
'bid',
);
unset($bids[$block['bid']]);
}
else {
$primary_keys = array();
}
drupal_write_record('block', $block, $primary_keys);
$blocks[] = $block;
}
}
if ($bids) {
db_delete('block')
->condition('bid', $bids, 'NOT IN')
->condition('theme', $theme)
->execute();
}
return $blocks;
}
function block_custom_block_get($bid) {
return db_query("SELECT * FROM {block_custom} WHERE bid = :bid", array(
':bid' => $bid,
))
->fetchAssoc();
}
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' => -18,
);
$form['body_field']['#weight'] = -17;
$form['body_field']['body'] = array(
'#type' => 'text_format',
'#title' => t('Block body'),
'#default_value' => $edit['body'],
'#format' => isset($edit['format']) ? $edit['format'] : NULL,
'#rows' => 15,
'#description' => t('The content of the block as shown to the user.'),
'#required' => TRUE,
'#weight' => -17,
);
return $form;
}
function block_custom_block_save($edit, $delta) {
db_update('block_custom')
->fields(array(
'body' => $edit['body']['value'],
'info' => $edit['info'],
'format' => $edit['body']['format'],
))
->condition('bid', $delta)
->execute();
return TRUE;
}
function block_form_user_profile_form_alter(&$form, &$form_state) {
$account = $form_state['controller']
->getEntity($form_state);
$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,
));
$account_data = drupal_container()
->get('user.data')
->get('block', $account
->id(), 'block');
$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->module][$block->delta]) ? $account_data[$block->module][$block->delta] : $block->custom == 1,
);
}
}
if ($blocks) {
$form['block'] = array(
'#type' => 'details',
'#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;
}
}
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;
}
function block_user_update($account) {
if (isset($account->block)) {
drupal_container()
->get('user.data')
->set('block', $account
->id(), 'block', $account->block);
}
}
function block_themes_enabled($theme_list) {
foreach ($theme_list as $theme) {
block_theme_initialize($theme);
}
}
function block_theme_initialize($theme) {
$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', 'stark');
$regions = system_region_list($theme, REGIONS_VISIBLE);
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(
':theme' => $default_theme,
), array(
'fetch' => PDO::FETCH_ASSOC,
));
foreach ($result as $block) {
if ($block['status'] && !isset($regions[$block['region']])) {
$block['region'] = system_default_region($theme);
}
$block['theme'] = $theme;
unset($block['bid']);
drupal_write_record('block', $block);
}
}
}
function block_list($region) {
$blocks =& drupal_static(__FUNCTION__);
if (!isset($blocks)) {
$blocks = _block_load_blocks();
}
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
return $blocks[$region];
}
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 (empty($block)) {
$block = new stdClass();
$block->module = $module;
$block->delta = $delta;
}
return $block;
}
function _block_load_blocks() {
global $theme_key;
$query = db_select('block', 'b');
$query
->addField('b', 'title', 'subject');
$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');
drupal_alter('block_list', $block_info);
$blocks = array();
foreach ($block_info as $block) {
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
return $blocks;
}
function block_block_list_alter(&$blocks) {
global $user, $theme_key;
$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;
}
$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;
}
if ($user->uid) {
$user_data = drupal_container()
->get('user.data')
->get('block', $user->uid, 'block');
}
foreach ($blocks as $key => $block) {
if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
continue;
}
if (isset($block_roles[$block->module][$block->delta]) && !array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
unset($blocks[$key]);
continue;
}
if ($block->custom != BLOCK_CUSTOM_FIXED) {
if ($user->uid && isset($user_data[$block->module][$block->delta])) {
$enabled = $user_data[$block->module][$block->delta];
}
else {
$enabled = $block->custom == BLOCK_CUSTOM_ENABLED;
}
}
else {
$enabled = TRUE;
}
if ($block->visibility == BLOCK_VISIBILITY_LISTED && empty($block->pages)) {
$enabled = FALSE;
}
if (!$enabled) {
unset($blocks[$key]);
continue;
}
if ($block->pages) {
$pages = drupal_strtolower($block->pages);
if ($block->visibility < BLOCK_VISIBILITY_PHP) {
$path = current_path();
$path_alias = drupal_strtolower(drupal_container()
->get('path.alias_manager')
->getPathAlias($path));
$page_match = drupal_match_path($path_alias, $pages) || $path != $path_alias && drupal_match_path($path, $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]);
continue;
}
if (!isset($block_langcodes[$block->module][$block->delta])) {
continue;
}
foreach ($block_langcodes[$block->module][$block->delta] as $language_type => $langcodes) {
if (isset($langcodes[language($language_type)->langcode])) {
continue 2;
}
}
unset($blocks[$key]);
}
}
function _block_get_renderable_block($element) {
$block = $element['#block'];
if (!isset($block->content)) {
$array = module_invoke($block->module, 'block_view', $block->delta);
drupal_alter(array(
'block_view',
"block_view_{$block->module}_{$block->delta}",
), $array, $block);
if (empty($array['content'])) {
$element += array(
'#access' => FALSE,
'#printed' => TRUE,
);
}
elseif (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->{$k} = $v;
}
}
}
if (isset($block->content) && $block->content) {
if (is_string($block->content)) {
$block->content = array(
'#markup' => $block->content,
);
}
if ($block->title) {
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
$element['content'] = $block->content;
unset($block->content);
$element['#block'] = $block;
}
return $element;
}
function block_cache_flush() {
return array(
'block',
);
}
function block_rebuild() {
foreach (list_themes() as $name => $data) {
if ($data->status) {
_block_rehash($name);
}
}
}
function template_preprocess_block(&$variables) {
$block_counter =& drupal_static(__FUNCTION__, array());
$variables['block'] = $variables['elements']['#block'];
if (!isset($block_counter[$variables['block']->region])) {
$block_counter[$variables['block']->region] = 1;
}
$variables['block_zebra'] = $block_counter[$variables['block']->region] % 2 ? 'odd' : 'even';
$variables['block_id'] = $block_counter[$variables['block']->region]++;
$variables['content'] = $variables['elements']['#children'];
$variables['attributes']['class'][] = drupal_html_class('block-' . $variables['block']->module);
$variables['content_attributes']['class'][] = 'content';
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->region;
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module;
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module . '__' . strtr($variables['block']->delta, '-', '_');
$variables['block_html_id'] = drupal_html_id('block-' . $variables['block']->module . '-' . $variables['block']->delta);
}
function block_user_role_delete($role) {
db_delete('block_role')
->condition('rid', $role->rid)
->execute();
}
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();
}
function block_admin_paths() {
$paths = array(
'admin/structure/block/demo/*' => FALSE,
);
return $paths;
}
function block_modules_uninstalled($modules) {
db_delete('block')
->condition('module', $modules, 'IN')
->execute();
db_delete('block_role')
->condition('module', $modules, 'IN')
->execute();
db_delete('block_language')
->condition('module', $modules, 'IN')
->execute();
}
function block_language_delete($language) {
db_delete('block_language')
->condition('langcode', $language->langcode)
->execute();
}
function block_library_info() {
$libraries['drupal.block'] = array(
'title' => 'Block',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'block') . '/block.js' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
),
);
return $libraries;
}