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).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Natallia created an issue. See original summary.

Natallia’s picture

Natallia’s picture

ram4nd’s picture

Status: Active » Needs review
ram4nd’s picture

Assigned: Natallia » Unassigned
Steven Jones’s picture

nedjo’s picture

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.

emerham’s picture

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.

init90’s picture

Status: Needs review » Reviewed & tested by the community

WOrks for me.