After deleting a flagged entity from my site using the Drupal API, I am getting the php error below when an outdated / cached search engine link is used to access the entity:

Request URI: https://my-site.com/ca/flag/flag/wishlist/35598

Notice: Trying to get property of non-object in flag_entity->applies_to_entity() (line 131 of /var/www/company_xxx/html/sites/all/modules/contrib/flag/includes/flag/flag_entity.inc).

I understand that the entity no longer exists, and that the url being used is invalid, but I wish the Flag module could better handle these situations without having to throw a php error.

See patch below which I created to fix this bug. If you are getting the same error message, I hope this helps.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gturnbull created an issue. See original summary.

gturnbull’s picture

gturnbull’s picture

Issue summary: View changes
gturnbull’s picture

Issue summary: View changes
gturnbull’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 2839047-property-non-object-flag-entity.patch, failed testing.

gturnbull’s picture

gturnbull’s picture

Status: Needs work » Needs review
joachim’s picture

Version: 7.x-3.9 » 7.x-3.x-dev
Status: Needs review » Needs work
Issue tags: -Notice: Trying to get property of non-object

Good catch, and thanks for working on a patch for this.

  1. +++ b/includes/flag/flag_entity.inc
    @@ -128,8 +128,13 @@ class flag_entity extends flag_flag {
    +    // Ensure the entity exists
    +    // The entity can be empty if an outdated search engine link is used to access a flagged entity that has been deleted using the Drupal API
    +    // For example, if the url http://my-site.com/ca/flag/flag/wishlist/35598 is loaded, after entity 35598 has been deleted
    

    We don't need to be as wordy here. There's no need to say 'deleted by the Drupal API'; just 'deleted' will do!

    And a simpler example than yours is a flag link that's out of date -- user A can load a page with a link to flag a node, and user B can delete that node between that and user A clicking the flag link.

    Also, please remember to wrap comments to 80 chars.

  2. +++ b/includes/flag/flag_entity.inc
    @@ -128,8 +128,13 @@ class flag_entity extends flag_flag {
    +    if (is_object($entity)) {
    

    I'd rather we check is_null(), as that's what fetch_entity() returns when no entity can be loaded, and also if we made this check separately rather than nesting if()s.

Lastly, please please please don't tag issues before reading about how to use the tags on issues! They are not for repeating the title, or for random keywords!

gturnbull’s picture

Hi joachim,
Thanks for taking the time to review my bug report and associated patch. Your comments and feedback are greatly appreciated.

I'm out of the office this week, but will update, test, and post a revised version of my patch early next week.

Thanks again.

Gordon

gturnbull’s picture

gturnbull’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: 2839047-property-non-object-flag-entity-3.patch, failed testing.

somersoft’s picture

web226’s picture

Thanks @somersoft the patch in #14 worked for me with Drupal 7.65, Flag 7.x-3.9 and PHP 7.2