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

  1. Create a flag.
  2. Flag an entity with the flag.
  3. Delete the flagged entity.
  4. 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

Comments

gturnbull created an issue. See original summary.

gturnbull’s picture

StatusFileSize
new911 bytes
joachim’s picture

Status: Active » Needs work

Thanks for the patch!

  1. +++ b/src/FlagCountManager.php
    @@ -243,7 +243,14 @@ class FlagCountManager implements FlagCountManagerInterface, EventSubscriberInte
    +      /* Workaround to correct the following error, caused by orphaned flags.
    +      TypeError: Argument 1 passed to Drupal\flag\FlagCountManager::resetLoadedCounts()
    +      must implement interface Drupal\Core\Entity\EntityInterface, null given.
    +      */
    

    Code comments shouldn't go into that kind of detail about a past error.

    Also, use // for inline comments not /*.

  2. +++ b/src/FlagCountManager.php
    @@ -243,7 +243,14 @@ class FlagCountManager implements FlagCountManagerInterface, EventSubscriberInte
    +      if(is_object($entity)) {
    

    Check the return of getFlaggable(), but it's probably enough to just check for truthiness.

  3. +++ b/src/FlagCountManager.php
    @@ -243,7 +243,14 @@ class FlagCountManager implements FlagCountManagerInterface, EventSubscriberInte
    +        $this->resetLoadedCounts($flagging->getFlaggable(), $flagging->getFlag());
    

    You already have the result of getFlaggable().

gturnbull’s picture

StatusFileSize
new689 bytes

Hi Joachim,
Thank you for your comments and feedback.

Here is an updated version of my patch.

Thanks again,
Gordon

gturnbull’s picture

Status: Needs work » Needs review
thirstysix’s picture

Status: Needs review » Reviewed & tested by the community

RTBC.
Works well with D10.x

  • berdir committed 1577a2da on 8.x-4.x
    Issue #3317282 by gturnbull: Error when deleting orphaned flags
    
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Doesn't hurt I guess, although something went wrong if you end up in this situation and you might get other weird problems.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.