I have a field in my View that outputs a YouTube URL (eg. http://www.youtube.com/watch?v=rF1X12PE6PY)

I want to output just the ID part (ie. rF1X12PE6PY)

So I made a module with a function that takes the full URL and returns just the ID. Then in my views-views-fields.tpl.php template I call the function:

<?php print youtube_id_get($fields['url']->content); ?>

Now this works fine, but something tells me it's not good Drupal practice?

Will this screw up Views performance/caching? Should I create a custom Views filter instead (still learning about such things)?

1 Answer 1


This should be just fine, however, The REAL Drupal way to do this, would be for your module to provide a field formatter to views that calls this function. There are many modules which do nothing but provide functions. CTools, for example.

Having a field formatter removes your site's functionality from the "view". Which is a best practice with any kind of software. You'll be able to change the theme in the future without worrying about your youtube IDs. Creating a formatter isn't hard to do, here is a great tutorial on how to do it.

Once you have a formatter, you can simply select it from the drop down in the View's UI and you're done!

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.