Problem/Motivation
It is easy to create a Media entity with a non-existent type (see below). I came across this when defining a migration, when I used the incorrect bundle name.
In this case, $this->bundle->entity
is NULL
. Since Media::getSource()
is simply
public function getSource() {
return $this->bundle->entity->getSource();
}
$entity->getSource()
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 $this->bundle->entity
is not empty before trying to access its getSource()
method.
Remaining tasks
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
N/A