Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3373122 by SV: Users profile images not shown in email notifications #3459

Merged
merged 2 commits into from Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -220,6 +220,7 @@ function social_activity_theme($existing, $type, $theme, $path) {
'profile_name' => '',
'profile_home' => '',
'profile_image' => '',
'profile_class' => '',
'profile_function' => '',
'profile_organization' => '',
],
Expand Down
Expand Up @@ -10,3 +10,6 @@ services:
- '@entity_type.manager'
- '@date.formatter'
- '@social_group.group_statistics'
- '@module_handler'
- '@stream_wrapper_manager'
- '@config.factory'
45 changes: 42 additions & 3 deletions modules/social_features/social_activity/src/EmailTokenServices.php
Expand Up @@ -3,12 +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 @@ -47,6 +51,21 @@ class EmailTokenServices {
*/
protected GroupStatistics $groupStatistics;

/**
* The module handler.
*/
protected ModuleHandlerInterface $moduleHandler;

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

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

/**
* Constructs a EmailTokenServices object.
*
Expand All @@ -56,15 +75,27 @@ class EmailTokenServices {
* DateFormatter object.
* @param \Drupal\social_group\GroupStatistics $group_statistics
* 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
GroupStatistics $group_statistics,
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 @@ -202,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 @@ -220,6 +258,7 @@ public function getUserPreview(User $user) {
'#profile_image' => $image_url ?? NULL,
'#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
@@ -1,6 +1,6 @@
<div class="card__block card__block--list teaser--small">
<span class="list-item__avatar list-item__avatar--medium">
<img src="{{ profile_image }}" alt={{ "profile image"|t }}/>
<img src="{{ profile_image }}" alt={{ "profile image"|t }} class="{{ profile_class }}" />
</span>
<span class="list-item__text">
<span class="teaser--small__title">
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