Implements hook_block_list_alter().
Checks the page, user role, and user-specific visibility settings. Removes the block if the visibility conditions are not met.
function block_block_list_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 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;
}
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) {
// 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;
}
// Use the user's block visibility setting, if necessary.
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;
}
// Limited visibility blocks must list at least one page.
if ($block->visibility == BLOCK_VISIBILITY_LISTED && empty($block->pages)) {
$enabled = FALSE;
}
if (!$enabled) {
unset($blocks[$key]);
continue;
}
// Match path if necessary.
if ($block->pages) {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower($block->pages);
if ($block->visibility < BLOCK_VISIBILITY_PHP) {
// Compare the lowercase path alias (if any) and internal path.
$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);
// 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.
$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;
}
// 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) {
if (isset($langcodes[language($language_type)->langcode])) {
// Found a language type - langcode combination in the configuration
// that is applicable to the current request.
continue 2;
}
}
// Had language configuration but none matched.
unset($blocks[$key]);
}
}