0

By a mistake, a lot of videos were created as documents instead of videos.

I want to avoid copy them into new entities because of the usage of the existing entities. I tried something like the below but gets the error Field field_media_video_file is unknown.:

$entity = Media::load(29265);
$entity->set('bundle', 'video');
$fileId = $entity->get('field_media_document')->target_id;
$entity->set('field_media_video_file', $fileId);
$entity->save();

How can I change the bundle type dynamically as the two bundles have different fields for storing the file (field_media_document and field_media_video_file)?

3
  • Have you tried splitting up the update into two separate queries? First change the bundle and only read the file ID, save, reload the entity (should now load with the new bundle's field definitions), set video field to file ID, save again.
    – Hudri
    Feb 8 at 12:46
  • I would create a new object rather than updating the existing object.
    – Jaypan
    Feb 8 at 15:00
  • You could create a new object Media::create(), and then map the specific fields from the old entity to the new one. Set the ID of the new entity and save it.
    – Birk
    Feb 9 at 9:47

0

Your Answer

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

Browse other questions tagged or ask your own question.