You are here

function datalayer_get_user_data in dataLayer 8

Same name and namespace in other branches
  1. 7 datalayer.module \datalayer_get_user_data()

Return all user data based on configured URL patterns.

Return value

array The user data.

1 call to datalayer_get_user_data()
datalayer_get_data_from_page in ./datalayer.module
Add data for output.

File

./datalayer.module, line 203
Client-side data space.

Code

function datalayer_get_user_data() {
  $user = \Drupal::currentUser();
  $user_data = [];
  if (!$user
    ->isAnonymous()) {
    $user = User::load($user
      ->id());
    $datalayer_settings = \Drupal::config('datalayer.settings');
    $roles = $datalayer_settings
      ->get('expose_user_details_roles');
    $exp_user_urls = $datalayer_settings
      ->get('expose_user_details');
    $exp_user_roles = $roles ? array_filter($roles) : [];

    // If exposed roles are configured, get those. Otherwise, get all roles.
    $matched_roles = !empty($exp_user_roles) ? array_intersect($user
      ->getRoles(), $exp_user_roles) : $user
      ->getRoles();

    // Honor settings.
    if ($exp_user_urls && count($matched_roles)) {
      $path = Url::fromRoute("<current>")
        ->toString();
      $path_matcher = \Drupal::service('path.matcher');
      $alias_manager = \Drupal::service('path.alias_manager');
      $path_alias = $alias_manager
        ->getAliasByPath($path);
      if ($path_matcher
        ->matchPath($path, $exp_user_urls) || $path_matcher
        ->matchPath($path_alias, $exp_user_urls)) {

        // Output various entity properties. Allow additions/alterations.
        // NOTE: Properties mean different things on different entity types.
        $properties = _datalayer_collect_meta_properties('current_user');
        $user_meta = $datalayer_settings
          ->get('current_user_meta');
        $selected_properties = _datalayer_get_selected_properties($properties, $user_meta);
        $user_prefix = 'user';
        $user_data = _datalayer_collect_meta_values($selected_properties, $user, $user_prefix);
        if (in_array('roles', $selected_properties)) {
          $user_data[$user_prefix . 'Roles'] = array_values($matched_roles);
        }
        if ($datalayer_settings
          ->get('expose_user_details_fields')) {
          $user_data[$user_prefix . 'Fields'] = _datalayer_get_field_values($user);
        }
      }
    }
  }
  return $user_data;
}