You are here

function google_optimize_hide_page_active in Google Optimize 8

Whether to put the snippet on the page.

Return value

bool google_optimize_hide_page_active

1 call to google_optimize_hide_page_active()
google_optimize_page_attachments in ./google_optimize.module
Implements hook_page_attachments().

File

./google_optimize.module, line 128
google_optimize.module

Code

function google_optimize_hide_page_active() {
  if (!google_optimize_hide_page_enabled()) {

    // Not enabled so do nothing.
    return FALSE;
  }
  $admin_context = \Drupal::service('router.admin_context');
  if ($admin_context
    ->isAdminRoute()) {

    // This is an admin page.
    return FALSE;
  }
  $container_ids = google_optimize_container_ids();
  if (empty($container_ids)) {

    // No container configured, so do nothing.
    return FALSE;
  }

  // See if restricted to certain pages.
  if ($pages = google_optimize_hide_page_pages()) {
    $current_path = \Drupal::service('path.current')
      ->getPath();
    if (strpos($current_path, '/node/') !== FALSE) {
      $current_path = \Drupal::service('path_alias.manager')
        ->getAliasByPath($current_path);
    }
    if (!($match = \Drupal::service('path.matcher')
      ->matchPath($current_path, $pages))) {

      // Not for this page.
      return FALSE;
    }
  }

  // See if restricted to certain role(s)
  if ($roles = google_optimize_hide_page_roles()) {
    $current_user = \Drupal::currentUser();
    $user_roles = $current_user
      ->getRoles();
    foreach ($user_roles as $role) {
      if (in_array($role, $roles, TRUE)) {

        // Add for this user.
        return TRUE;
      }
    }

    // Not in the list of allowed roles
    return FALSE;
  }
  return TRUE;
}