Problem/Motivation

When running cron, function bynder_cron makes it fail.

Steps to reproduce

I'm getting the following error when running cron:

php-error staging-38954 [11-Mar-2021 11:34:08 Europe/London] Error: Call to a member function mainPropertyName() on null in /mnt/www/html/sitedev/docroot/modules/contrib/bynder/src/Plugin/media/Source/Bynder.php on line 435 #0 /mnt/www/html/sitedev/docroot/modules/contrib/bynder/src/BynderService.php(218): Drupal\bynder\Plugin\media\Source\Bynder->getSourceFieldValue(Object(Drupal\media\Entity\Media))

Proposed resolution

Check if $field_item is null before returning the value.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork bynder-3203003

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

guardiola86 created an issue. See original summary.

guardiola86’s picture

StatusFileSize
new500 bytes

I've attached a patch that fixes the error. Maybe there's a better solution though.

guardiola86’s picture

Status: Active » Needs review
berdir’s picture

Status: Needs review » Needs work

Thanks for the patch.

Looking at the parent method, that is basically identical except that it already contains a similar check. This is probably leftover of the old media_entity days when we had to do this ourself.

I'd suggest you just remove the whole method and confirm that this fixes your problem.

FWIW, if you do get this error then it sounds like you have broken bynder media entities in your system, you might want to search for them and delete them, as they might break things elsewhere too.

JvE made their first commit to this issue’s fork.

lukus’s picture

Hi

I can confirm that removing the method solves the problem.

The core method in D9.2 will allow for a source field with zero items:

  /**
   * {@inheritdoc}
   */
  public function getSourceFieldValue(MediaInterface $media) {
    $source_field = $this->configuration['source_field'];
    if (empty($source_field)) {
      throw new \RuntimeException('Source field for media source is not defined.');
    }

    $items = $media->get($source_field);
    if ($items->isEmpty()) {
      return NULL;
    }

    $field_item = $items->first();
    return $field_item->{$field_item->mainPropertyName()};
  }

The helper method that's bundled with the module doesn't:

  /**
   * Get the primary value stored in the source field.
   *
   * @todo This helper method was added to MediaSourceBase in 8.5.0 but we
   * replicate it here because we want to support 8.4.0 sites as well. This
   * method can be safely removed once there is no need to support 8.4 anymore,
   * and we ensure the core Media dependency is bumped to 8.5.0 at least.
   *
   * @param \Drupal\media\MediaInterface $media
   *   A media item.
   *
   * @return mixed
   *   The source value.
   *
   * @throws \RuntimeException
   *   If the source field for the media source is not defined.
   */
  public function getSourceFieldValue(MediaInterface $media) {
    $source_field = $this->configuration['source_field'];
    if (empty($source_field)) {
      throw new \RuntimeException('Source field for media source is not defined.');
    }

    /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
    $field_item = $media->get($source_field)->first();
    return $field_item->{$field_item->mainPropertyName()};
  }
lukus’s picture

@berdir are you happy for me to create a patch to remove the full method?

lukus’s picture

Here's a patch in any case :)

JvE’s picture

The patch in #9 is almost identical to my merge request in #6. It just misses the increased dependency version.

JvE’s picture

Status: Needs work » Needs review
lukus’s picture

Hi @JvE

Sorry, I missed that.

Looks good :)

Best

Luke

berdir’s picture

Status: Needs review » Reviewed & tested by the community

That requirement is unnecessary though because in the very same .info.yml, we already require drupal core ^8.8 || 9, so there's no way that you could have drupal:media 8.4.

  • Berdir committed 21c3cea on 4.0.x authored by JvE
    Issue #3203003 by JvE, lukus, guardiola86: Error: Call to a member...

  • Berdir committed 8b2ac12 on 4.0.x
    Issue #3203003: Revert unecessary drupal:media version requirement
    
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Accidently merged the MR and reverted the .info.yml change again.

Status: Fixed » Closed (fixed)

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