function social_core_block_access in Open Social 10.0.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_core/social_core.module \social_core_block_access()
- 8.8 modules/social_features/social_core/social_core.module \social_core_block_access()
- 10.3.x modules/social_features/social_core/social_core.module \social_core_block_access()
- 10.1.x modules/social_features/social_core/social_core.module \social_core_block_access()
- 10.2.x modules/social_features/social_core/social_core.module \social_core_block_access()
Hide blocks on extendable visibility path.
Implements hook_block_access().
File
- modules/
social_features/ social_core/ social_core.module, line 624 - The Social core module.
Code
function social_core_block_access(Block $block, $operation, AccountInterface $account) {
$blocks =& drupal_static(__FUNCTION__);
$blocks = [];
// First check if we have it cached before we run the modulehandler alter.
/** @var \Drupal\Core\Cache\CacheBackendInterface $cache */
$cache = \Drupal::service('cache.default');
if ($cache
->get('social_core.block_visibility_cache') === FALSE) {
// Allow other modules to change the block visibility.
$blocks = \Drupal::moduleHandler()
->invokeAll('social_core_block_visibility_path');
// Set the cache expire to 1 week.
$date = new DateTime(date('Y-m-d'));
$date
->modify('+7 day');
$cache
->set('social_core.block_visibility_cache', $blocks, $date
->getTimestamp(), [
'social_core.block_visibility_cache',
]);
}
else {
$blocks = $cache
->get('social_core.block_visibility_cache')->data;
}
// Alter the access based on the blocks array.
if ($operation === 'view' && isset($blocks[$block
->getPluginId()])) {
// Get the current path.
$current_path = \Drupal::service('path.current')
->getPath();
// Go through the request paths and see if they match.
foreach ($blocks[$block
->getPluginId()] as $page) {
if (\Drupal::service('path.matcher')
->matchPath($current_path, $page) === TRUE) {
return AccessResult::forbidden();
}
}
}
// No opinion.
return AccessResult::neutral();
}