From dd528121dfb4e1ea8c7e1f322d26052f160693da Mon Sep 17 00:00:00 2001 From: Volodymyr Sydor Date: Fri, 7 Jul 2023 16:07:18 +0300 Subject: [PATCH] Issue #3373122 by SV: Display default placeholder image when it's not a public --- .../social_activity.services.yml | 2 ++ .../src/EmailTokenServices.php | 36 ++++++++++++++++--- phpstan-baseline.neon | 5 --- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/modules/social_features/social_activity/social_activity.services.yml b/modules/social_features/social_activity/social_activity.services.yml index 83050f2eff0..f148bef7f27 100644 --- a/modules/social_features/social_activity/social_activity.services.yml +++ b/modules/social_features/social_activity/social_activity.services.yml @@ -11,3 +11,5 @@ services: - '@date.formatter' - '@social_group.group_statistics' - '@module_handler' + - '@stream_wrapper_manager' + - '@config.factory' diff --git a/modules/social_features/social_activity/src/EmailTokenServices.php b/modules/social_features/social_activity/src/EmailTokenServices.php index f124f6467d7..0a2daf5514f 100644 --- a/modules/social_features/social_activity/src/EmailTokenServices.php +++ b/modules/social_features/social_activity/src/EmailTokenServices.php @@ -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; @@ -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. * @@ -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; } /** @@ -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. @@ -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; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index edc4a88acb1..9be6bb0d8e8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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