You are here

function block_exclude_pages_block_access in Block Exclude Pages 8

Same name and namespace in other branches
  1. 2.x block_exclude_pages.module \block_exclude_pages_block_access()

Implements hook_block_access().

File

./block_exclude_pages.module, line 98
Contains block_exclude_pages.module..

Code

function block_exclude_pages_block_access(Block $block, $operation, AccountInterface $account) {

  /* ############  testing/debuggin - only: ################# */
  $debug_output = [];

  // $debug = block_exclude_pages_debug_check();
  $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) {

    // - Temp comment to keep old verions. will delet in next version:
    // $path = explode('/', trim(\Drupal::service('path.alias_manager')->getAliasByPath($nodeid), '/'));
    $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 ? $debug : $block
    ->getVisibilityConditions()
    ->getConfiguration();
  if (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));
      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 (block_exclude_pages_check_excluded_path($exclude, $path, $nodeid)) {
            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 (block_exclude_pages_check_excluded_path($exclude, $path, $nodeid)) {
          $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 = [
        '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));
      }
    }
  }
}