You are here

function d8_google_optimize_hide_page_active in Drupal 8 Google Optimize Hide Page 8

Whether to put the snippet on the page.

Return value

bool d8_google_optimize_hide_page_active

1 call to d8_google_optimize_hide_page_active()
d8_google_optimize_hide_page_page_attachments in ./d8_google_optimize_hide_page.module
Implements hook_page_attachments().

File

./d8_google_optimize_hide_page.module, line 100
d8_google_optimize_hide_page.module

Code

function d8_google_optimize_hide_page_active() {
  if (!d8_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 = d8_google_optimize_hide_page_container_ids();
  if (empty($container_ids)) {

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

  // See if restricted to certain pages.
  if ($pages = d8_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 = d8_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;
}