If I have the id of an existing piece of media content, what can I do to update it (modify it)?

I know how to create a new piece of media , (with Media::create) , but not sure of the code to load and then updated an existing piece of Media.


1 Answer 1


Media is like any other entity based on ContentEntityBase in Drupal.

// top of your file
use Drupal\media_entity\Entity\Media;

// in a function
$media = Media::load($id);
// do stuff with $media


In a method of a class you would not want to use Media:: but instead use entityTypeManager service to load the entity:

$media = $this->entityTypeManager->getStorage('media')->load($id); where entityTypeManager is injected to your class.

  • are there commands to set the data? such as setVid() , etc?
    – Diana
    Mar 29, 2017 at 19:58
  • Check the docs for public methods.
    – Kevin
    Mar 29, 2017 at 20:07
  • $media->set('uid',$current_userid);
    – Diana
    Mar 29, 2017 at 20:59
  • 1
    Yes. Check the class and what it is inheriting or extending - any of those public methods are available for you to use.
    – Kevin
    Mar 29, 2017 at 21:04
  • 4
    Now it's use Drupal\media\Entity\Media;
    – leymannx
    Jul 28, 2018 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.