You are here

function add_to_head_match_role in Add To Head 8

Same name and namespace in other branches
  1. 7 add_to_head.module \add_to_head_match_role()

Determines if code should be displayed for a particular role.

Parameters

array $profile: The profile to check against

array $user_roles: Allow injection of non-current user roles

Return value

boolean TRUE if the code should be displayed on the page; FALSE otherwise.

1 call to add_to_head_match_role()
_add_to_head_profile_visible in ./add_to_head.module
Is this profile visible?

File

./add_to_head.module, line 176
Add To Head allows arbitrary insertion of code into the head of the page based on path selection.

Code

function add_to_head_match_role($profile, $user_roles = NULL) {

  // Determine if the code should be visible given the current user's roles.
  $visibility = isset($profile['roles']) && isset($profile['roles']['visibility']) ? $profile['roles']['visibility'] : 'exclude';
  $roles = isset($profile['roles']) && isset($profile['roles']['list']) ? $profile['roles']['list'] : array();
  if (!isset($user_roles)) {
    $user_roles = \Drupal::currentUser()
      ->getRoles();
  }
  $common_roles = array_intersect($roles, $user_roles);
  if ($visibility == 'exclude') {

    // If any common roles, no match.
    return empty($common_roles);
  }
  else {

    // Visibility is include. Match on any common roles.
    return !empty($common_roles);
  }
}