0

I'm attempting to load the file attached to a media entity used in a field attached to a paragraph in order to create a url to the file.

    $vid = $paragraph->get($field_name)[0]?->entity?->toArray()['vid'][0]['value'];
    $media['desktop_mp4'] = File::load($vid)?->createFileUrl();

$paragraph->get($field_name)[0]?->entity does give me a proper Media object, which does have properties like 'vid' and 'mid' but I'm not sure what to use when loading the file attached to it.

In an example case I know that the ID is supposed to be 18 but sometimes that comes from the vid and sometimes that comes from the mid. Even when using the same media in the same field type there doesn't seem to be any consistency as to where the proper fid can be found in the array returned by $paragraph->get($field_name)[0]?->entity?->toArray();

What should I be using to the fid from a media entity?

1 Answer 1

1

The issue was I wasn't thinking about using the actual field from the media object that contained the file. The IDs on the media object lining up with the files I needed might have been coincidences.

In order to get what I needed I used this

$video_file_url = $paragraph->get('field_video')[0]?->entity->field_media_video_file->entity->createFileUrl();

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.