Tech Blog :: views


Aug 6 '10 1:15pm

Drupal: Impossible to set custom size for embedded video field in a view?

I have an Embedded Media youtube video field shared across multiple content types on a site, and nodequeues for the administrators to pick from these videos and feature them on various landing pages. The designs call for the videos to be different sizes on different pages - sometimes the standard 425px, sometimes 300px, sometimes widescreen 600px. When I saw the designs, I thought "I'm sure that's easy to do," and gave my approval. I'm implementing each "featured video" using a view that pulls from the respective nodequeue and displays the video as a field.

Well... I've spent hours trying to crack this nut, and my conclusion is that it's nearly impossible to do in a clean way. By clean I mean, I want to specify per view what size the video should appear. I can't set it per content type (using the backend UI) because the videos come from many content types, and if I set it by URL, there could be other videos on those pages that don't need special formatting. The settings for the field in Views only have Full and Preview, not custom sizes.

The logical approach seemed to be a hook_views_pre_render like so:
$view->field['field_video_youtube_embed']->content_field['widget']['video_width'] = 300;

That didn't work (still came out 425px), so I decided to go down an academic rabbit hole to figure out why...

The problem seems to arise with the generic content_handler_field::render() class that the field is rendered through. The object has a perfectly good $this->content_field array, which includes my modified width/height. But the function finds it necessary for some reason to get a fresh field array,
$field = content_fields($this->content_field['field_name'], $values->{$this->aliases['type']});
so the parameters are all reset. Why??

I'll just use a fields preprocessor to re-render the field. Should have done that to begin with; this study was intellectually fascinating (in a sad way) but otherwise a huge waste of time.