Skip to content

Commit

Permalink
PROD-26095: Change preview for comment and post to fix mentions in em…
Browse files Browse the repository at this point in the history
…ail notifications
  • Loading branch information
Kovalskiy266 authored and ribel committed Aug 18, 2023
1 parent 9f4a6ab commit 6ea82af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function filterMentions(string $text): string {
'#render_value' => $output['value'],
'#render_plain' => $output['render_plain'] ?? FALSE,
];
$mentions = $this->renderer->render($build);
$mentions = $this->renderer->renderPlain($build);
$text = str_replace($match['source']['string'], $mentions, $text);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ services:
- '@module_handler'
- '@stream_wrapper_manager'
- '@config.factory'
- '@plugin.manager.filter'
54 changes: 50 additions & 4 deletions modules/social_features/social_activity/src/EmailTokenServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\filter\FilterPluginManager;
use Drupal\group\Entity\Group;
use Drupal\image\Entity\ImageStyle;
use Drupal\message\Entity\Message;
Expand All @@ -29,7 +30,6 @@
*/
class EmailTokenServices {
use StringTranslationTrait;

/**
* Entity type manager services.
*
Expand Down Expand Up @@ -66,6 +66,13 @@ class EmailTokenServices {
*/
protected ConfigFactory $config;

/**
* The filter plugin manager service.
*
* @var \Drupal\filter\FilterPluginManager
*/
protected $filterPluginManager;

/**
* Constructs a EmailTokenServices object.
*
Expand All @@ -81,21 +88,25 @@ class EmailTokenServices {
* The stream wrapper manager.
* @param \Drupal\Core\Config\ConfigFactory $config
* The config factory service.
* @param \Drupal\filter\FilterPluginManager $filter_plugin_manager
* FilterPluginManager object.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
DateFormatter $date_formatter,
GroupStatistics $group_statistics,
ModuleHandlerInterface $module_handler,
StreamWrapperManagerInterface $stream_wrapper_manager,
ConfigFactory $config
ConfigFactory $config,
FilterPluginManager $filter_plugin_manager
) {
$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;
$this->filterPluginManager = $filter_plugin_manager;
}

/**
Expand Down Expand Up @@ -134,10 +145,11 @@ public function getCommentPreview(Comment $comment) {

if ($comment->hasField('field_comment_body') && !$comment->get('field_comment_body')->isEmpty()) {
if ($summary = _social_comment_get_summary($comment->getFieldValue('field_comment_body', 'value'))) {
$processed_text = $this->processMentionsPreview($summary, $comment->language()->getId());
// Prepare the preview information.
$preview_info = [
'#theme' => 'message_post_comment_preview',
'#summary' => $summary,
'#summary' => !empty($processed_text) ? $processed_text : $summary,
];
}
}
Expand Down Expand Up @@ -200,10 +212,11 @@ public function getPostPreview(Post $post) {
// Get the summary of the comment.
if ($post->hasField('field_post') && !$post->get('field_post')->isEmpty()) {
if ($summary = _social_comment_get_summary($post->getFieldValue('field_post', 'value'))) {
$processed_text = $this->processMentionsPreview($summary, $post->language()->getId());
// Prepare the preview information.
$preview_info = [
'#theme' => 'message_post_comment_preview',
'#summary' => $summary,
'#summary' => !empty($processed_text) ? $processed_text : $summary,
];
}
}
Expand Down Expand Up @@ -305,4 +318,37 @@ public function getCtaButton(Url $url, TranslatableMarkup $text) {
];
}

/**
* Process mentions in text, if the Social Mentions module exists.
*
* @param string $text
* Text we need to process.
* @param string $language_id
* Langcode of entity.
*
* @return \Drupal\filter\FilterProcessResult|string
* Result with processed text.
*/
public function processMentionsPreview(string $text, string $language_id) {
$result = '';
if ($this->moduleHandler->moduleExists('social_mentions')) {
/** @var \Drupal\filter\Plugin\FilterInterface $mentions_filter */
$mentions_filter = $this->filterPluginManager->createInstance(
'filter_mentions',
[
'settings' => [
'mentions_filter' => [
'ProfileMention' => 1,
'UserMention' => 1,
],
],
],
);
$result = $mentions_filter->process(
$text, $language_id
);
}
return $result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getRecipients(array $data, int $last_id, int $limit): array {
else {
$entity_storage = $this->entityTypeManager->getStorage($related_object['target_type']);
$entity = $entity_storage->load($related_object['target_id']);
if ($entity !== NULL) {
if ($entity instanceof EntityInterface) {
$mentions = $this->getMentionsFromRelatedEntity($entity);
}
}
Expand Down

0 comments on commit 6ea82af

Please sign in to comment.