You are here

function yandex_metrics_show_counter_for_role in Yandex.Metrics 8.2

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

Returns FALSE if we need to disable counter for role.

Return value

bool

1 call to yandex_metrics_show_counter_for_role()
yandex_metrics_page_build in ./yandex_metrics.module
Implements hook_page_build(). Adds Yandex.Metrics counter code to the site footer.

File

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

Code

function yandex_metrics_show_counter_for_role() {
  $user = \Drupal::currentUser();
  $visibility = (bool) Drupal::config('yandex_metrics.settings')
    ->get('visibility.role.visibility');
  $enabled = (bool) $visibility;
  $roles = Drupal::config('yandex_metrics.settings')
    ->get('visibility.role.roles');
  $has_active_role = FALSE;
  foreach ($roles as $key => $value) {
    if ($key === $value) {
      $has_active_role = TRUE;
      break;
    }
  }
  if ($has_active_role) {

    // One or more roles are selected.
    foreach ($user
      ->getRoles() as $rid) {

      // Is the current user a member of one of these roles?
      if (isset($roles[$rid]) && $rid === $roles[$rid]) {

        // Current user is a member of a role that should be tracked/excluded from tracking.
        $enabled = !$visibility;
        break;
      }
    }
  }
  else {

    // No role is selected for tracking, therefore all roles should be tracked.
    $enabled = TRUE;
  }
  return $enabled;
}