function Media::prepareSave

Same name in other branches
  1. 9 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::prepareSave()
  2. 8.9.x core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::prepareSave()
  3. 11.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 412

Class

Media
Defines the media entity class.

Namespace

Drupal\media\Entity

Code

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, $this->original must be
    // set. However, that is only set once parent::save() is called, so work
    // around that by setting it here.
    if (!isset($this->original) && ($id = $this->id())) {
        $this->original = $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.
            foreach ($translation->bundle->entity
                ->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
                // Only save value in the entity if the field is empty or if the
                // source field changed.
                if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)
                    ->isEmpty() || $translation->hasSourceFieldChanged())) {
                    $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.