function Media::prepareSave
Same name and namespace in other branches
- 10 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::prepareSave()
- 11.x core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::prepareSave()
- 9 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::prepareSave()
- 8.9.x core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::prepareSave()
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.
File
-
core/
modules/ media/ src/ Entity/ Media.php, line 429
Class
- Media
- Defines the media entity class.
Namespace
Drupal\media\EntityCode
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. See
// https://www.drupal.org/project/drupal/issues/2976875 for more.
// In order for metadata to be mapped correctly, the original entity must be
// set. However, that is only set once parent::save() is called, so work
// around that by setting it here.
if (!$this->getOriginal() && $id = $this->id()) {
$this->setOriginal($this->entityTypeManager()
->getStorage('media')
->loadUnchanged($id));
}
$media_source = $this->getSource();
foreach ($this->translations as $langcode => $data) {
if ($this->hasTranslation($langcode)) {
$translation = $this->getTranslation($langcode);
// Try to set fields provided by the media source and mapped in
// media type config.
$original = $this->getOriginal();
foreach ($translation->bundle->entity
->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
if (!$translation->hasField($entity_field_name)) {
continue;
}
$set_metadata = FALSE;
// Only save value in the entity if the field is empty, or if the
// source field changed from one non-empty value to another non-empty
// value.
// 1st scenario: field is empty, safely store metadata there.
if ($translation->get($entity_field_name)
->isEmpty()) {
$set_metadata = TRUE;
}
else {
// 2nd scenario: field is not empty, but the source field has
// changed between two non-empty values.
$original_source_field_empty = $original instanceof MediaInterface && $media_source->getSourceFieldValue($original) === NULL;
$new_source_field_empty = $media_source->getSourceFieldValue($translation) === NULL;
if (!$original_source_field_empty && !$new_source_field_empty && $translation->hasSourceFieldChanged()) {
$set_metadata = TRUE;
}
}
if ($set_metadata) {
$translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));
}
}
// Try to set a default name for this media item if no name is provided.
if ($translation->get('name')
->isEmpty()) {
$translation->setName($translation->getName());
}
// Set thumbnail.
if ($translation->shouldUpdateThumbnail($this->isNew())) {
$translation->updateThumbnail();
}
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.