function social_profile_profile_view_alter in Open Social 8.9
Same name and namespace in other branches
- 8 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.2 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.3 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.4 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.5 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.6 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.7 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 8.8 modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 10.3.x modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 10.0.x modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 10.1.x modules/social_features/social_profile/social_profile.module \social_profile_profile_view_alter()
- 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 651 - 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, it 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;
}
}
}
}
}