Error during cron-job: Call to a member function id() on array

Created on 13 January 2023, over 1 year ago
Updated 26 January 2023, over 1 year ago

Problem/Motivation

When the cron job of Bynder fails to process all media in updateMediaEntities, the error reporting breaks.

The error:

Call to a member function id() on array in Drupal\bynder\BynderService->updateMetadataLastMediaEntities() (line 234 of .../bynder/src/BynderService.php)

This is the piece of code that breaks:

$updated_entities = $this->updateMediaEntities($bynder_media_entities);

$missing_remote_entities = [];
// Log warning in case a media entity has been removed in the remote system.
foreach ($bynder_media_entities as $missing_remote_entity) {
  $missing_remote_entities[$missing_remote_entity->id()] = $missing_remote_entity;

The reason is that the array-structure is different then expected.
The actual array structure of $bynder_media_entities is:

$bynder_media_entities = {array[11]} 
 1C56D778-DE75-47FA-A660B9AA828B28FF = {array[1]} 
  3671 = {Drupal\media\Entity\Media} 

Notice that the Media entity is nested inside the array with the UUID key.
This

Steps to reproduce

Proposed resolution

Add $missing_remote_entity = reset($missing_remote_entity); in the foreach loop when reporting erros.

$updated_entities = $this->updateMediaEntities($bynder_media_entities);

$missing_remote_entities = [];
// Log warning in case a media entity has been removed in the remote system.
foreach ($bynder_media_entities as $missing_remote_entity) {
  $missing_remote_entity = reset($missing_remote_entity); # <-- new
  $missing_remote_entities[$missing_remote_entity->id()] = $missing_remote_entity;

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

🇳🇱Netherlands ndf Amsterdam

Live updates comments and jobs are added and updated live.
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.

  • 🇳🇱Netherlands dennisdk

    Patch #2 is also working fine for me. It fixes the error on cron and will display the warnings again of the removed media entities instead of throwing a fatal.

  • Status changed to Fixed over 1 year ago
  • 🇨🇭Switzerland berdir Switzerland

    The reason for the structure change is that there can be multiple medias now for the same remote media. I changed the reset to a loop so you don't have to track this down multiple times in a row.

    Note that if you get this error, you have broken media in your system that unless you copied it down to Drupal, no longer displays an image and you probably want to replace and delete it.

    • Berdir committed aa2044d3 on 4.0.x authored by ndf
      Issue #3333390 by ndf, Berdir: Error during cron-job: Call to a member...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024