Problem sending notifications when the message is too long (more than 255 characters)

Created on 28 May 2024, 6 months ago
Updated 29 May 2024, 6 months ago

Problem/Motivation

In the context of my project, I have a specific usage of your module, because I'm only using the NotificationsWidgetService service to send notifications in certain circumstances not covered by the basic configuration. And it's a very good thing to have this possibility.

However, sometimes the message is too long and I get the following sql error:

Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'message' at row 1: ...

because 63 characters are stored automatically for the link => <a class=‘noti-store-msg’ href=‘javascript:;’ data-link="></a> which leaves only 192 characters available, which isn't necessarily enough when:

  • the url of the entity can be long, depending on the alias pattern used,
  • the message content is long, if it is partially customised.

Steps to reproduce

It's easy to reproduce this bug by forcing, in a Controller, a notification to be sent, as long as you have an entity with a title and a long URL.

$content = sprintf(
  'The page %s  has been updated by %s',
  $entity->getTitle(),
  $user->getAccountName()
);

$message = [
  'id' => $entity->id(),
  'bundle' => $entity->bundle(),
  'content' => $content,
  'content_link' => $entity->toUrl()->toString(),
];

// Here we force a notification to be sent to the superadmin user 1
\Drupal::service('notifications_widget.logger')
  ->logNotification($message, 'update', $entity, 1, $user);

Proposed resolution

There are two possible approaches to solving this problem:

1/ The first - the simplest :

Change the schema of the notifications table to store data of type text and not a varchar(255).

2/ The second - more complex and longer to implement:

Do not store this message in this way, but store the useful information in separate columns and:

  • Use the necessary information in the NotificationsWidgetBlock block:

i.e.

$notificationList[] = [
  'id'      => $notification->id,
  'nas_id'  => $nasId,
  'message' => $notification->message,
  'entity_url' => $notification->url,
  'status'  => $status,
];
  • Pass only useful information to the templates that send the notifications

i.e.

{% for notifications in notification_list %}
  <li data-id="{{  notifications.id }}" data-nas-id = "{{ notifications.nas_id }}" data-read-status = "{{ notifications.status ? 'read' : 'unread' }}" class="notification-items {{ notifications.status ? 'read' : 'unread' }}">
    <i class="fa fa-dot-circle-o" aria-hidden="true"></i>
    <span class="notification-msg">
      <a class="noti-store-msg" href="javascript:;" data-link="{{ notifications.url }}">
        {{ notifications.message }}
      </a>
    </span>
    <span class="glyphicon glyphicon-trash notification-remove"></span>
  </li>
{% endfor %}

This second approach would also make it easier to integrate messages.

Data model changes

One way or another, the data model needs to be reviewed.

🐛 Bug report
Status

Active

Version

1.9

Component

Code

Created by

🇫🇷France zewebmaster Nantes

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

Comments & Activities

Production build 0.71.5 2024