You are here

function social_profile_preprocess_profile in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  2. 8 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  3. 8.2 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  4. 8.3 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  5. 8.4 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  6. 8.5 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  7. 8.6 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  8. 8.7 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  9. 8.8 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  10. 10.3.x modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  11. 10.0.x modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  12. 10.2.x modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()

Prepares variables for profile templates.

Default template: profile.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • profile: The profile object.
  • view_mode: View mode; e.g., 'full', 'teaser', etc.

File

modules/social_features/social_profile/social_profile.module, line 345
The Social profile module.

Code

function social_profile_preprocess_profile(array &$variables) {

  /** @var \Drupal\profile\Entity\Profile $profile */
  $profile = $variables['profile'];
  $user = User::load($profile
    ->getOwnerId());
  $current_user = \Drupal::currentUser();

  // See social_user_user_format_name_alter(), DisplayName is either first name
  // + last name, or username if both first and last name aren't filled out.
  $variables['profile_name'] = $user
    ->getDisplayName();
  $variables['profile_contact_url'] = _social_profile_get_contact_url($user);
  $variables['profile_stream_url'] = Url::fromUserInput('/user/' . $profile
    ->getOwnerId() . '/stream');
  $variables['profile_home'] = Url::fromRoute('entity.user.canonical', [
    'user' => $user
      ->id(),
  ]);

  // Set a condition for the label to show in the teaser
  // The actual label text should be changeable in the template.
  // Disable for the LU's own profile.
  $variables['profile_contact_label'] = 'profile_info';
  if (\Drupal::moduleHandler()
    ->moduleExists('social_private_message') && $current_user
    ->id() != $profile
    ->getOwnerId() && $current_user
    ->hasPermission('use private messaging system') && User::load($profile
    ->getOwnerId())
    ->hasPermission('use private messaging system')) {
    $variables['profile_contact_label'] = 'private_message';
    $variables['#cache']['contexts'][] = 'user';
  }
  if (_social_profile_check_property_access($profile, $current_user, 'email')) {

    // Derived from MailToFormatter.php.
    $variables['user_mail'] = Link::fromTextAndUrl($user
      ->getEmail(), Url::fromUri('mailto:' . $user
      ->getEmail()));
  }

  // Language field.
  $language_manager = \Drupal::languageManager();
  if ($language_manager
    ->isMultilingual() && _social_profile_check_property_access($profile, $current_user, 'language')) {

    // Add the user language.
    $variables['user_lang'] = $language_manager
      ->getLanguageName($user
      ->getPreferredLangcode());
  }

  // Edit profile URL.
  // Get the current route name to check if the user is on the edit or delete
  // page.
  $route = \Drupal::routeMatch()
    ->getRouteName();
  if ($route !== 'profile.user_page.single' && $profile
    ->access('update', $current_user)) {
    $variables['profile_edit_url'] = Url::fromUserInput('/user/' . $profile
      ->getOwnerId() . '/profile');
    $variables['#cache']['contexts'][] = 'route.name';
  }

  // Add the hero styled image if we have access to it.
  if (!empty($profile->field_profile_banner_image->entity) && $profile->field_profile_banner_image
    ->access('view')) {
    $variables['profile_hero_styled_image_url'] = ImageStyle::load('social_xx_large')
      ->buildUrl($profile->field_profile_banner_image->entity
      ->getFileUri());
  }

  // Add the profile image.
  if (!empty($profile->field_profile_image->entity)) {
    $variables['profile_image_url'] = ImageStyle::load('social_medium')
      ->buildUrl($profile->field_profile_image->entity
      ->getFileUri());
  }

  // Profile information URL.
  $variables['profile_info_url'] = Url::fromRoute('view.user_information.user_information', [
    'user' => $profile
      ->getOwnerId(),
  ]);

  // Prepare variables for statistic block.
  if ($variables['elements']['#view_mode'] === 'statistic') {

    /** @var \Drupal\social_profile\UserStatistics $user_statistics */
    $user_statistics = \Drupal::service('social_profile.user_statistics');
    $variables['profile_topics'] = $user_statistics
      ->nodeCount($user
      ->id(), 'topic');
    $variables['profile_events'] = $user_statistics
      ->nodeCount($user
      ->id(), 'event');
    $variables['profile_groups'] = \Drupal::service('social_group.helper_service')
      ->countGroupMembershipsForUser($user
      ->id());
  }
}