1

I believe I have a way to do this, but as I'm learning I want to make sure I'm not doing it horribly wrong or reinventing a wheel that already exists (the module search on the Drupal site leaves a lot to be desired).

I've got a Link field with multiple values (just URLs). When this field is being rendered, I would like to sort of preprocess each of the URLs. If it's a youtube or vimeo link, I'd like to change it so that a video embed is rendered instead. If it's a link to an audio file (with certain extensions) then I want to render an html5 audio tag. Else, I just want to render the link normally.

It looks like I could override mytheme_field__links__mycontenttype() (although I'm not exactly sure how to implement that method just yet, any examples are welcome).

It also looks like I could create a template in my theme named field--links--mycontenttype.tpl.php

Is this the right way to go?

Is one better than the other?

Do you have any links to a good example of how to implement/override these?

Thanks!

1

The two best ways to do this are the one you suggest by creating a tpl.php like you mention and doing your work in there directly. Another (and preferred by many, but a bit more complex) is to use template_preprocess_field.

A very good explanation of how to go down the second route is http://www.digett.com/blog/01/18/2012/change-output-single-field-drupal-7-node

That way you don't end up with a whole bunch of field.tpls and you can isolate your changes in that function.

  • Wonderful, thank you for the example link! It was exactly what I needed. – Kenny Wyland Aug 9 '12 at 23:32
  • No problem. All credit due to the person who wrote that explanation. I've used it myself. Very handy. – nedwardss Aug 10 '12 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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