Media::create() should fail gracefully if called with an invalid bundle

Created on 1 October 2020, over 4 years ago
Updated 17 February 2023, about 2 years ago

Problem/Motivation

It is easy to create a Media entity with a non-existent type (see "Steps to reproduce" below). But saving the entity causes a PHP fatal error.

I came across this when defining a migration, when I used the incorrect bundle name.

The error is caused because Media::prepareSave() calls Media::getSource(), which is simply

  public function getSource() {
    return $this->bundle->entity->getSource();
  }

If the bundle is not valid, then $this->bundle->entity is NULL, which leads to an error something like this:

Call to a member function getSource() on null in Drupal\media\Entity\Media->getSource() (line 137 of /var/www/html/web/core/modules/media/src/Entity/Media.php)

Steps to reproduce

$entity = \Drupal::entityTypeManager()
  ->getStorage('media')
  ->create(['name' => 'foo', 'bundle' => 'does_not_exist']);
$entity->save();

Proposed resolution

Check that the bundle value is one of the configured Media types in Media::doCreate(). If not, then throw an exception.

We already throw an exception, in ContentEntityBase::doCreate(), if no bundle value is provided.

Remaining tasks

  • Fix the failing test.
  • Add a new test.
  • Add a change record.
  • Add follow-up issue(s). See #16, #19.2, perhaps other comments.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
MediaΒ  β†’

Last updated 5 minutes ago

Created by

πŸ‡ΊπŸ‡ΈUnited States benjifisher Boston area

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I tested on Drupal 10.1.x, standard install using the steps in the Issue Summary. I was able to reproduce the error.

    back to need work for tests.

  • First commit to issue fork.
  • Merge request !11949Throw exception for all entity types β†’ (Open) created by dcam
  • Pipeline finished with Failed
    16 days ago
    Total: 161s
    #482769
  • Pipeline finished with Failed
    16 days ago
    Total: 598s
    #482777
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    I'm aware that the previous consensus was to only fix this for the Media entity type because of the perception that it would be too much work for all entities. But I really wanted to see for myself what would happen.

  • Pipeline finished with Failed
    1 day ago
    Total: 646s
    #494031
  • πŸ‡ΊπŸ‡ΈUnited States dcam

    dcam β†’ changed the visibility of the branch 3174255-docreate-invalid-bundle to hidden.

  • Merge request !12104Created MR from #22 β†’ (Open) created by dcam
  • Pipeline finished with Failed
    about 19 hours ago
    Total: 520s
    #494459
Production build 0.71.5 2024