diff -u b/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php --- b/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -10,7 +10,6 @@ use Drupal\media\MediaInterface; use Drupal\media\MediaSourceEntityConstraintsInterface; use Drupal\media\MediaSourceFieldConstraintsInterface; -use Drupal\media\PrepareSaveInterface; use Drupal\user\UserInterface; /** @@ -84,7 +83,7 @@ * } * ) */ -class Media extends EditorialContentEntityBase implements MediaInterface, PrepareSaveInterface { +class Media extends EditorialContentEntityBase implements MediaInterface { use StringTranslationTrait; @@ -375,15 +374,22 @@ } /** - * {@inheritdoc} + * Sets the media entity's field values from the source's metadata. + * + * Fetching the metadata could be slow (e.g., if requesting it from a remote + * API), so this is called by \Drupal\media\MediaStorage::save() prior to it + * beginning the database transaction, whereas static::preSave() executes + * after the transaction has already started. + * + * @internal + * Expose this as an API in + * https://www.drupal.org/project/drupal/issues/2992426. */ public function prepareSave() { // @todo If the source plugin talks to a remote API (e.g. oEmbed), this code // might be performing a fair number of HTTP requests. This is dangerously // brittle and should probably be handled by a queue, to avoid doing HTTP - // operations during entity save. As it is, doing this before calling - // parent::save() is a quick-fix to avoid doing HTTP requests in the middle - // of a database transaction (which begins once we call parent::save()). See + // operations during entity save. See // https://www.drupal.org/project/drupal/issues/2976875 for more. // In order for metadata to be mapped correctly, $this->original must be diff -u b/core/modules/media/src/MediaStorage.php b/core/modules/media/src/MediaStorage.php --- b/core/modules/media/src/MediaStorage.php +++ b/core/modules/media/src/MediaStorage.php @@ -19,13 +19,14 @@ */ public function save(EntityInterface $media) { // For backwards compatibility, modules that override the Media entity - // class, are not required to implement \Drupal\media\PrepareSaveInterface. - if ($media instanceof PrepareSaveInterface) { + // class, are not required to implement the prepareSave() method. + // @todo For Drupal 8.7, consider throwing a deprecation notice if the + // method doesn't exist. See + // https://www.drupal.org/project/drupal/issues/2992426 for further + // discussion. + if (method_exists($media, 'prepareSave')) { $media->prepareSave(); } - else { - // @todo For Drupal 8.7, consider throwing a deprecation notice. - } return parent::save($media); } reverted: --- b/core/modules/media/src/PrepareSaveInterface.php +++ /dev/null @@ -1,27 +0,0 @@ -