You are here

function social_profile_profile_view_alter in Open Social 8.7

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

Implements hook_ENTITY_TYPE_view_alter().

File

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

Code

function social_profile_profile_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {

  /** @var \Drupal\Core\Session\AccountProxy $current_user */
  $current_user = \Drupal::currentUser();

  // If the current user has no access to viewing user profiles, he might not
  // have access to the users profile.
  if (!$current_user
    ->hasPermission('view any profile profile') && isset($display
    ->get('content')['field_profile_image'])) {

    // Try to load the profile picture.
    $fid = $entity
      ->get('field_profile_image')->target_id;

    // Must have a value and not be NULL.
    if (!is_null($fid)) {

      // Load the file.
      $file = File::load($fid);

      // Check if it's a real file.
      if ($file instanceof File) {

        // Potentially the file is in the private file system. In that case,
        // anonymous user don't have access to it.
        if (!$file
          ->access('view', $current_user)) {

          // Load default data.
          $replacement_data = social_profile_get_default_image();

          /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $imgitem */
          $imgitem = $build['field_profile_image'][0]['#item'];

          // Time to override the data that going to be rendered.
          $imgitem
            ->set('target_id', $replacement_data['id']);
          $imgitem
            ->set('width', $replacement_data['width']);
          $imgitem
            ->set('height', $replacement_data['height']);

          // Put replacement data back in the object that's about to be built.
          $build['field_profile_image'][0]['#item'] = $imgitem;
        }
      }
    }
  }
}