4

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.

thanks

1 Answer 1

10

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
$media->save();

edit:

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.

5
  • 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 that you have read and understand our privacy policy and code of conduct.

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