TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

Created on 14 June 2022, over 2 years ago
Updated 11 March 2024, 9 months ago

While updating entities during batch operation (add new field values) the following error occurs:
TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given in method_exists() (line 512 of /modules/contrib/field_collection/field_collection.module).

πŸ“Œ Task
Status

RTBC

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
  • PHP 8.0

    The issue particularly affects sites running on PHP version 8.0.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¨πŸ‡¦Canada nedjo

    Thanks for noting the error and for contributing the proposed fix.

    The error in field_collection_field_update() (an implementation of hook_field_update()) indicates the $top_host variable is, at some point, assigned a NULL value.

    The current patch silently accepts a (presumably, invalid?) NULL value for $top_host. That may disguise a problem. It would be preferable to track down and address the source of that NULL value.

    There are two points where $top_host is assigned a value. First, it's seeded with the value passed in by the code invoking the hook: $top_host = $host_entity;. Second, it's conditionally reset to the return value of a ::hostEntity() method call:

      while (method_exists($top_host, 'hostEntity')) {
        $top_host = $top_host->hostEntity();
      }
    

    While it's possible the first assignment is the source of this bug - that is, the hook is being invoked with broken data- that seems less likely on the face of it. That leaves the possibility that the ::hostEntity() method method is returning a NULL value. And, indeed, the return value of FieldCollectionItemEntity::hostEntity() is only conditionally an entity, and otherwise NULL.

    On that basis, the correct fix here would seem to be to reassign the value of $top_host only if the ::hostEntity() call has returned a non-NULL value.

    Patch attached. No interdiff as this does not continue from the prior patch.

  • πŸ‡ΊπŸ‡ΈUnited States emerham

    Ran into this problem when updating a single node that used paragraphs with field collections. Was unable to find the issue but this patch solved the problem.

  • Status changed to RTBC 9 months ago
  • πŸ‡ΊπŸ‡¦Ukraine init90 Ukraine

    WOrks for me.

Production build 0.71.5 2024