You are here

function yandex_metrics_show_counter in Yandex.Metrics 8.3

Same name and namespace in other branches
  1. 8.2 yandex_metrics.module \yandex_metrics_show_counter()
  2. 6.2 yandex_metrics.module \yandex_metrics_show_counter()
  3. 6 yandex_metrics.module \yandex_metrics_show_counter()
  4. 7.3 yandex_metrics.module \yandex_metrics_show_counter()
  5. 7 yandex_metrics.module \yandex_metrics_show_counter()
  6. 7.2 yandex_metrics.module \yandex_metrics_show_counter()

Returns FALSE if we need to disable counter on page.

Return value

bool

1 call to yandex_metrics_show_counter()
yandex_metrics_page_bottom in ./yandex_metrics.module
Implements hook_page_bottom(). Adds Yandex.Metrics counter code to the site footer.

File

./yandex_metrics.module, line 43
The main code of Yandex.Metrics Counter module.

Code

function yandex_metrics_show_counter() {
  $pages = Drupal::config('yandex_metrics.settings')
    ->get('visibility.path.pages');
  $visibility = Drupal::config('yandex_metrics.settings')
    ->get('visibility.path.visibility');
  $urls_equal = FALSE;
  if (!empty($pages)) {
    $pages_in_lowcase = Unicode::strtolower($pages);
    $current_path = Drupal::service('path.alias_manager')
      ->getAliasByPath(\Drupal::service('path.current')
      ->getPath());
    $current_path = Unicode::strtolower($current_path);

    // In D8 alias is with leading slash?
    // @todo: Make sure we follow D8 standards of aliases.
    $current_path = ltrim($current_path, "/");

    // Compare internal and path alias.
    $path_match = \Drupal::service('path.matcher')
      ->matchPath($current_path, $pages_in_lowcase);
    if ($path_match) {
      $urls_equal = TRUE;
    }
    else {

      // If path alias doesn't equal with current_path() then compare internal and current_path().
      $path_match = \Drupal::service('path.matcher')
        ->matchPath(\Drupal::service('path.current')
        ->getPath(), $pages_in_lowcase);
      if ($current_path != \Drupal::service('path.current')
        ->getPath() && $path_match) {
        $urls_equal = TRUE;
      }
    }
  }
  if (!$visibility && $urls_equal) {
    return FALSE;
  }
  elseif (!$visibility && !$urls_equal) {
    return TRUE;
  }
  elseif ($visibility && $urls_equal) {
    return TRUE;
  }
  elseif ($visibility && !$urls_equal) {
    return FALSE;
  }
}