public function updateSourceField(
    MediaInterface $media,
    $resource,
    $mimetype
  ) {
    $source_field = $this->getSourceFieldName($media->bundle());
    $file = $this->getSourceFile($media);

    // Update it.
    $this->updateFile($file, $resource, $mimetype);
    $file->save();

    // Set fields provided by type plugin and mapped in bundle configuration
    // for the media.
    foreach ($media->bundle->entity->getFieldMap() as $source => $destination) {
      if ($media->hasField($destination) && $value = $media->getSource()->getMetadata($media, $source)) {
        $media->set($destination, $value);
      }
      // Ensure width and height are updated on File reference when it's an
      // image. Otherwise you run into scaling problems when updating images
      // with different sizes.
      if ($source == 'width' || $source == 'height') {
        $media->get($source_field)->first()->set($source, $value);
      }
    }

    $media->save();
  }
public function updateSourceField(
    MediaInterface $media,
    $resource,
    $mimetype
  ) {
    $source_field = $this->getSourceFieldName($media->bundle());
    $file = $this->getSourceFile($media);

    // Update it.
    $this->updateFile($file, $resource, $mimetype);
    $file->save();

    // Set fields provided by type plugin and mapped in bundle configuration
    // for the media.
    foreach ($media->bundle->entity->getFieldMap() as $source => $destination) {
      if ($media->hasField($destination) && $value = $media->getSource()->getMetadata($media, $source)) {
        $media->set($destination, $value);
      }
      // Ensure width and height are updated on File reference when it's an
      // image. Otherwise you run into scaling problems when updating images
      // with different sizes.
      if ($source == 'width' || $source == 'height') {
        $media->get($source_field)->first()->set($source, $value);
      }
    }

    $media->save();
  }
public function updateSourceField ( MediaInterface $ media , $ resource , $ mimetype ) { $ source_field = $ this -> getSourceFieldName ( $ media -> bundle ( ) ) ; $ file = $ this -> getSourceFile ( $ media ) ; // Update it. $ this -> updateFile ( $ file , $ resource , $ mimetype ) ; $ file -> save ( ) ; // Set fields provided by type plugin and mapped in bundle configuration // for the media. foreach ( $ media -> bundle -> entity -> getFieldMap ( ) as $ source => $ destination ) { if ( $ media -> hasField ( $ destination ) && $ value = $ media -> getSource ( ) -> getMetadata ( $ media , $ source ) ) { $ media -> set ( $ destination , $ value ) ; } // Ensure width and height are updated on File reference when it's an // image. Otherwise you run into scaling problems when updating images // with different sizes. if ( $ source == 'width' || $ source == 'height' ) { $ media -> get ( $ source_field ) -> first ( ) -> set ( $ source , $ value ) ; } } $ media -> save ( ) ; }
Updates a media's source field with the supplied resource.

@param \Drupal\media\MediaInterface $media
The media to update.
@param resource $resource
New file contents as a resource.
@param string $mimetype
New mimetype of contents.

@throws HttpException
Updates a media s source field with the supplied resource .