I need to displaying some notifications on a Craft site, and I want to allow users to mark each notification as read. There won't be many users or notifications, so a plugin seems overkill. Instead I've created a Notifications section with a Read by (Users) field called notificationReadBy
.
Notifications display on the template inside an entry form, with a hidden multi-select field containing all users who've marked the notification as read (i.e. already submitted the form) plus the current user's id in case they want to mark as read too. That works fine to build a list of users who've already read the notification.
Next step is to hide the notification altogether if the current user has already marked it as read, and is listed in the Read by field. So I thought something like this might work...
{% for entry in craft.entries
.section('notifications')
.all() %}
{% set readBy = entry.notificationReadBy %}
{% if currentUser.id not in readBy %}
{# Show the notification #}
{% endif %}{% endfor %}
Does that make sense? Anyway, it's not working as expected and is displaying the entry regardless of whether the current user is listed in Read by or not. Any ideas? Maybe it's neater to move the query up to the entries tag, using the .search()
param, but I can't get that working either?
devMod
enabled.{{ dump(entry.notificationReadBy) }}