Problem/Motivation
When using the reset option to remove all "flaggings" created for a particular flag, I am getting the following PHP type error:
TypeError: Argument 1 passed to Drupal\flag\FlagCountManager::resetLoadedCounts() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /var/www/html/web/modules/contrib/flag/src/FlagCountManager.php on line 246 in Drupal\flag\FlagCountManager->resetLoadedCounts() (line 313 of /var/www/html/web/modules/contrib/flag/src/FlagCountManager.php)
I believe this error occurs when the resetLoadedCounts() method attempts to process an orphaned flag, where the related flaggable entity has been deleted.
Steps to reproduce
- Create a flag.
- Flag an entity with the flag.
- Delete the flagged entity.
- Use the Reset feature provided for the flag to delete all "flaggings" created for the flag.
Proposed resolution
Check to ensure the parent entity exists before attempting to call the resetLoadedCounts() method, which requires a valid entity object.
Remaining tasks
Create a patch to address this issue.
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3317282-error-deleting-orphaned-flag-2.patch | 689 bytes | gturnbull |
| #2 | 3317282-error-deleting-orphaned-flag.patch | 911 bytes | gturnbull |
Comments
Comment #2
gturnbull commentedComment #3
joachim commentedThanks for the patch!
Code comments shouldn't go into that kind of detail about a past error.
Also, use // for inline comments not /*.
Check the return of getFlaggable(), but it's probably enough to just check for truthiness.
You already have the result of getFlaggable().
Comment #4
gturnbull commentedHi Joachim,
Thank you for your comments and feedback.
Here is an updated version of my patch.
Thanks again,
Gordon
Comment #5
gturnbull commentedComment #6
thirstysix commentedRTBC.
Works well with D10.x
Comment #8
berdirDoesn't hurt I guess, although something went wrong if you end up in this situation and you might get other weird problems.