View source
<?php
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\block\BlockInterface;
function restrict_ip_block_access(BlockInterface $block, $operation, AccountInterface $account) {
if ($operation == 'view') {
if (\Drupal::service('restrict_ip.service')
->userIsBlocked()) {
if ($block
->getPluginId() != 'system_main_block') {
return AccessResult::forbidden();
}
}
}
return AccessResult::neutral();
}
function restrict_ip_preprocess_page(&$build) {
if (\Drupal::service('restrict_ip.service')
->userIsBlocked()) {
$regions = system_region_list(\Drupal::service('theme.manager')
->getActiveTheme()
->getName(), REGIONS_ALL);
unset($regions['content']);
$whitelisted_regions = Drupal::service('module_handler')
->invokeAll('restrict_ip_whitelisted_regions');
foreach ($whitelisted_regions as $wr) {
unset($regions[$wr]);
}
foreach (array_keys($regions) as $region) {
if (isset($build['page'][$region])) {
unset($build['page'][$region]);
}
}
if (isset($build['tabs'])) {
if (isset($build['tabs']['#primary'])) {
$build['tabs']['#primary'] = [];
}
if (isset($build['tabs']['#secondary'])) {
$build['tabs']['#primary'] = [];
}
}
$build['title_prefix'] = [];
$build['title_suffix'] = [];
$build['main_menu'] = [];
$build['secondary_menu'] = [];
$build['action_links'] = [];
}
}
function restrict_ip_preprocess_html(&$build) {
if (\Drupal::service('restrict_ip.service')
->userIsBlocked()) {
if (isset($build['page_top'])) {
unset($build['page_top']);
}
if (isset($build['page_top'])) {
unset($build['page_bottom']);
}
}
}
function restrict_ip_js_alter(&$javascript, AttachedAssetsInterface $assets) {
if (\Drupal::service('restrict_ip.service')
->userIsBlocked()) {
$whitelisted_js_keys = [
'core/assets/vendor/jquery/jquery.min.js',
'core/assets/vendor/jquery/jquery.js',
drupal_get_path('module', 'restrict_ip') . '/js/mail_fix.js',
];
$whitelisted_js_keys += Drupal::service('module_handler')
->invokeAll('restrict_ip_whitelisted_js_keys');
foreach (array_keys($javascript) as $key) {
if (!in_array($key, $whitelisted_js_keys)) {
unset($javascript[$key]);
}
}
}
}