You are here

function social_profile_preprocess_profile in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  2. 8.2 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  3. 8.3 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  4. 8.4 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  5. 8.5 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  6. 8.6 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  7. 8.7 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  8. 8.8 modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  9. 10.3.x modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  10. 10.0.x modules/social_features/social_profile/social_profile.module \social_profile_preprocess_profile()
  11. 10.1.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 196
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');

  // 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()) {
    $variables['profile_contact_label'] = 'private_message';
    $variables['#cache']['contexts'][] = 'user';
  }

  // Email field.
  $global_show_email = \Drupal::config('social_profile.settings')
    ->get('social_profile_show_email');
  if (isset($profile->field_profile_show_email)) {
    $profile_show_email = $profile->field_profile_show_email->value;
  }
  if ($global_show_email || !$global_show_email && !empty($profile_show_email) || $current_user
    ->hasPermission('view profile email')) {

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

  // 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 (!in_array($route, [
    'entity.profile.type.user_profile_form',
  ])) {
    if ($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 (!empty($profile->field_profile_banner_image->entity)) {
    $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());
  }
}