Skip to content

Commit

Permalink
Issue #3373122 by SV: Display default placeholder image when it's not…
Browse files Browse the repository at this point in the history
… a public
  • Loading branch information
volodymyr-sydor committed Jul 10, 2023
1 parent 5bdfb22 commit 94df78b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Expand Up @@ -11,3 +11,5 @@ services:
- '@date.formatter'
- '@social_group.group_statistics'
- '@module_handler'
- '@stream_wrapper_manager'
- '@config.factory'
36 changes: 32 additions & 4 deletions modules/social_features/social_activity/src/EmailTokenServices.php
Expand Up @@ -3,13 +3,16 @@
namespace Drupal\social_activity;

use Drupal\comment\Entity\Comment;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\group\Entity\Group;
use Drupal\image\Entity\ImageStyle;
use Drupal\message\Entity\Message;
Expand Down Expand Up @@ -53,6 +56,16 @@ class EmailTokenServices {
*/
protected ModuleHandlerInterface $moduleHandler;

/**
* The stream wrapper manager.
*/
protected StreamWrapperManagerInterface $streamWrapperManager;

/**
* The configfactory.
*/
protected ConfigFactory $config;

/**
* Constructs a EmailTokenServices object.
*
Expand All @@ -64,17 +77,25 @@ class EmailTokenServices {
* GroupStatistics object.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager.
* @param \Drupal\Core\Config\ConfigFactory $config
* The config factory service.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
DateFormatter $date_formatter,
GroupStatistics $group_statistics,
ModuleHandlerInterface $module_handler
ModuleHandlerInterface $module_handler,
StreamWrapperManagerInterface $stream_wrapper_manager,
ConfigFactory $config
) {
$this->entityTypeManager = $entity_type_manager;
$this->dateFormatter = $date_formatter;
$this->groupStatistics = $group_statistics;
$this->moduleHandler = $module_handler;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -212,8 +233,15 @@ public function getUserPreview(User $user) {
// Add the profile image.
/** @var \Drupal\image\Entity\ImageStyle $image_style */
$image_style = ImageStyle::load('social_medium');
if (!empty($profile->field_profile_image->entity)) {
$image_url = $image_style->buildUrl($profile->field_profile_image->entity->getFileUri());

/** @var \Drupal\file\FileInterface $image */
$image = !$profile->get('field_profile_image')->isEmpty() ? $profile->get('field_profile_image')->entity : '';

if (
$image instanceof FileInterface &&
$this->streamWrapperManager->getScheme($image->getFileUri()) !== 'private'
) {
$image_url = $image_style->buildUrl($image->getFileUri());
}
elseif ($default_image = social_profile_get_default_image()) {
// Add default image.
Expand All @@ -228,9 +256,9 @@ public function getUserPreview(User $user) {
'#profile_name' => $user->getDisplayName(),
'#profile_home' => Url::fromRoute('entity.user.canonical', ['user' => $user->id()]),
'#profile_image' => $image_url ?? NULL,
'#profile_class' => $this->moduleHandler->moduleExists('lazy') ? 'no-lazy' : '',
'#profile_function' => $profile->getFieldValue('field_profile_function', 'value'),
'#profile_organization' => $profile->getFieldValue('field_profile_organization', 'value'),
'#profile_class' => $this->moduleHandler->moduleExists('lazy') ? $this->config->get('lazy.settings')->get('skipClass') : '',
];

return $preview_info;
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -3515,11 +3515,6 @@ parameters:
count: 1
path: modules/social_features/social_activity/social_activity.tokens.inc

-
message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:getFileUri\\(\\)\\.$#"
count: 1
path: modules/social_features/social_activity/src/EmailTokenServices.php

-
message: "#^Cannot call method getFileUri\\(\\) on Drupal\\\\file\\\\Entity\\\\File\\|null\\.$#"
count: 1
Expand Down

0 comments on commit 94df78b

Please sign in to comment.