function block_exclude_pages_block_access in Block Exclude Pages 2.x
Same name and namespace in other branches
- 8 block_exclude_pages.module \block_exclude_pages_block_access()
Implements hook_block_access().
File
- ./
block_exclude_pages.module, line 49 - Contains block_exclude_pages.module.
Code
function block_exclude_pages_block_access(Block $block, $operation, AccountInterface $account) {
/* ############ testing/debuggin - only: ################# */
$debug_output = [];
$debug = FALSE;
/* ######################################################## */
$nodeid = \Drupal::service('path.current')
->getPath();
$path = explode('/', trim(\Drupal::request()->query
->get('q'), '/'));
if ($path[0] == "" && \Drupal::service('path.matcher')
->isFrontPage() != TRUE) {
$path = explode('/', trim(\Drupal::service('path_alias.manager')
->getAliasByPath($nodeid), '/'));
}
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
// - unset language id if present in path.
if ($path[0] == $language) {
unset($path[0]);
}
// - join paths.
$path = "/" . implode("/", $path);
// - get block's visibility conditions.
$conditions = $debug ? block_exclude_pages_debug_check() : $block
->getVisibilityConditions()
->getConfiguration();
if (is_array($conditions) && (count($conditions) > 0 && !empty($conditions['request_path']))) {
$pages = explode("\n", $conditions['request_path']['pages']);
$pttr = '#^\\!#';
foreach ($pages as $p) {
// - check if exclude conditions is set.
if (preg_match($pttr, $p) !== 1) {
if ($debug) {
array_push($debug_output, $p . " - SKIPPED");
}
continue;
}
// - exclude item found, now test if on page path.
$exclude = trim(preg_replace($pttr, "", $p));
$pathmatcher = \Drupal::service('path.matcher');
if ($debug) {
// Used for testing and debugging only:
$paths = block_exclude_pages_debug_dummy_path();
array_push($debug_output, "----------------------------- [ !" . $exclude . " ]------------------------------");
foreach ($paths as $path) {
if ($pathmatcher
->matchPath($nodeid, $exclude) || $pathmatcher
->matchPath($path, $exclude)) {
if ($exclude == $nodeid) {
array_push($debug_output, "!" . $exclude . " : " . $nodeid . " - BLOCKED <<<<<<<<<<<<<<<<<<<<<<<<<");
}
else {
array_push($debug_output, "!" . $exclude . " : " . $path . " - BLOCKED <<<<<<<<<<<<<<<<<<<<<<<<<");
}
}
else {
array_push($debug_output, "!" . $exclude . " : " . $path . " - PASSED");
}
}
}
else {
// - Set the visibility of the block:
if ($pathmatcher
->matchPath($nodeid, $exclude) || $pathmatcher
->matchPath($path, $exclude)) {
$config['pages'] = $exclude;
$config['context_mapping'] = [];
if (isset($conditions['request_path']['negate']) && $conditions['request_path']['negate'] == TRUE) {
$config['negate'] = FALSE;
}
else {
$config['negate'] = TRUE;
}
$block
->setVisibilityConfig('request_path', $config);
break;
}
}
}
// - output testing/debug info:
if ($debug) {
$debug_log = [
'type' => 'debug_log',
'block_id' => $block
->id(),
'block_data' => $block,
'debug_output' => $debug_output,
];
if (function_exists('ksm')) {
ksm($debug_log);
}
else {
\Drupal::logger('block_exclude_pages')
->notice(print_r($debug_log, 1));
}
}
}
}