Custom notification displays to all users instead of specific UIDs

Created on 26 April 2023, over 1 year ago
Updated 28 April 2023, over 1 year ago

Problem/Motivation

I'm creating a custom notification to send to users referenced in a User Reference field on a node.

All works as expected, although the notification itself is showing to all users (sending to all users?). Below is the main segment of my code....

$refUserAccessId is only user who should be seeing the notification. (dumping $refUserAccessId shows correct ids)

      $refUserAccessList = $node->get('field_report_user_access')->referencedEntities();
      foreach ($refUserAccessList as $refUserAccess) {
        if (!empty($refUserAccess)) {

          // Get the user ID of the referenced user.
          $refUserAccessId = $refUserAccess->id();

            $message = [
              'id' => $node->id(),
              'bundle' => 'report',
              'content' => '[user:name] created a new private report and gave you access.',
              'content_link' => $node->toUrl()->toString(),
            ];

            // log the notification for the referenced user.
            $notificationService = \Drupal::service('notifications_widget.logger');
            $notificationService->logNotification($message, 'create', $node, $refUserAccessId);
        }
      }

I'm wondering if this is a major bug or just related to my specific use case?

💬 Support request
Status

Closed: works as designed

Version

2.0

Component

Miscellaneous

Created by

🇺🇸United States chucksimply

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @chucksimply
  • 🇺🇸United States chucksimply
  • Assigned to shailja179
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India shailja179 India

    @chucksimply, where are you showing these notification? In block file or some other place you must have written below code.

    To add permission for user specific, you can add condition just before notificationlist array like this. Please try this.

     if ($content_admin && $end_user == FALSE) {
    
              if (!empty($notification->message)) {
                $notificationlist[] = [
                  'id'      => $notification->id,
                  'message' => $notification->message,
                  'status'  => $notification->status,
                ];
               }
  • 🇺🇸United States chucksimply

    @shailja179 - the notification output is in the default notifications block that comes with the module.

    Im not exactly sure how the code you provided will fix the issue.

    Shouldn’t the last argument in $notificationService->logNotification($message, 'create', $node, $refUserAccessId); determine who receives the notification? Or am I missing something?

  • Status changed to Active over 1 year ago
  • 🇺🇸United States chucksimply
  • 🇮🇳India shailja179 India

    Yes, Your code should work actually.

  • 🇧🇪Belgium BramDriesen Belgium 🇧🇪

    I was looking into a implementation we did and the only difference I noticed was that we casted the user id to be an int. I believe this is default a string? For the rest this should indeed work. I would also suggest moving the message part outside the loop, as you are not setting anything custom in it. Not sure about the token replacement though, I think it would be safer to replace that with the author value of the node that triggered the notification. I also suggest doing that replacement with a $this->t() function.

    $user_access_list = $node->get('field_report_user_access')->referencedEntities();
    
    $message = [
      'id' => $node->id(),
      'bundle' => 'report',
      'content' => '[user:name] created a new private report and gave you access.',
      'content_link' => $node->toUrl()->toString(),
    ];
    
    foreach ($user_access_list as $user) {
      if (!empty($user)) {
        // Log the notification for the referenced user.
        $notificationService = \Drupal::service('notifications_widget.logger');
        $notificationService->logNotification($message, 'create', $node, (int) $user->id());
      }
    }
    
  • Issue was unassigned.
  • 🇺🇸United States chucksimply
  • Status changed to Closed: works as designed over 1 year ago
  • 🇺🇸United States chucksimply

    @BramDriesen - Thanks for the response, and feedback on my code. Yes, the default was being output as a string, and the integer suggestion worked.

    Although I did test it on a different setup, and the string did actually work... so maybe it was another issue as well? Not sure... so marking as closed / works as designed. Thanks again!

Production build 0.71.5 2024