Type error when associated entity is not set

Created on 14 February 2025, 4 months ago

Problem/Motivation

When using a collector for a message template that doesn't have associated entity, the following error happens:

TypeError: Drupal\message_recipient_entity\Plugin\MessageRecipient\CollectorExtender\ExcludeOwner::getAssociatedEntity(): Return value must be of type Drupal\Core\Entity\ContentEntityInterface, null returned in Drupal\message_recipient_entity\Plugin\MessageRecipient\CollectorExtender\ExcludeOwner->getAssociatedEntity() (line 36 of ./modules/contrib/message_recipient/modules/message_recipient_entity/src/MessageAssociatedEntityTrait.php).

The problem is that MessageAssociatedEntityTrait::getAssociatedEntity() doesn't allow returning NULL, but in reality it can return NULL.

Steps to reproduce

My use case is creating a message via a cron job, where the message template uses the 'Users by roles' collector, but there is no associated entity set for the message. In this case, the error happens when collecting the recipients for the message.

Proposed resolution

Add NULL to the return type declaration for MessageAssociatedEntityTrait::getAssociatedEntity().

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇧🇪Belgium keszthelyi Brussels

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024