diff --git a/modules/custom/mentions/src/Plugin/Filter/MentionsFilter.php b/modules/custom/mentions/src/Plugin/Filter/MentionsFilter.php index 0d4958e380d..a1ea14769c7 100644 --- a/modules/custom/mentions/src/Plugin/Filter/MentionsFilter.php +++ b/modules/custom/mentions/src/Plugin/Filter/MentionsFilter.php @@ -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); } } diff --git a/modules/social_features/social_activity/social_activity.services.yml b/modules/social_features/social_activity/social_activity.services.yml index f148bef7f27..c23c3b48808 100644 --- a/modules/social_features/social_activity/social_activity.services.yml +++ b/modules/social_features/social_activity/social_activity.services.yml @@ -13,3 +13,4 @@ services: - '@module_handler' - '@stream_wrapper_manager' - '@config.factory' + - '@plugin.manager.filter' diff --git a/modules/social_features/social_activity/src/EmailTokenServices.php b/modules/social_features/social_activity/src/EmailTokenServices.php index 0a2daf5514f..2531d3feeb6 100644 --- a/modules/social_features/social_activity/src/EmailTokenServices.php +++ b/modules/social_features/social_activity/src/EmailTokenServices.php @@ -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; @@ -29,7 +30,6 @@ */ class EmailTokenServices { use StringTranslationTrait; - /** * Entity type manager services. * @@ -66,6 +66,13 @@ class EmailTokenServices { */ protected ConfigFactory $config; + /** + * The filter plugin manager service. + * + * @var \Drupal\filter\FilterPluginManager + */ + protected $filterPluginManager; + /** * Constructs a EmailTokenServices object. * @@ -81,6 +88,8 @@ 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, @@ -88,7 +97,8 @@ public function __construct( 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; @@ -96,6 +106,7 @@ public function __construct( $this->moduleHandler = $module_handler; $this->streamWrapperManager = $stream_wrapper_manager; $this->config = $config; + $this->filterPluginManager = $filter_plugin_manager; } /** @@ -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, ]; } } @@ -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, ]; } } @@ -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; + } + } diff --git a/modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php b/modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php index f384b1fb55b..4ab6f22f4a5 100644 --- a/modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php +++ b/modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php @@ -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); } }