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