You are here

function social_core_block_access in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_block_access()
  2. 10.3.x modules/social_features/social_core/social_core.module \social_core_block_access()
  3. 10.0.x modules/social_features/social_core/social_core.module \social_core_block_access()
  4. 10.1.x modules/social_features/social_core/social_core.module \social_core_block_access()
  5. 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 467
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();
}