I've created a cck field formatter. The field outputs uploads from filefield into embedded iframes that link to google docs for display. It currently outputs:

<div class="field field-type-filefield field-field-documents">
  <div class="field-items">
    <div class="field-item odd">
      <iframe src="http://docs.google.com/viewer?url=http://example.com/2011/sites/example/files/session/documents/brighton_online.doc&amp;embedded=true" style="width:470px; height:296px;" frameborder="0"></iframe>
      <p><a href="http://example.com/2011/sites/example/files/session/documents/brighton_online.doc">Download the document</a></p>        
    <div class="field-item even">
      <iframe src="http://docs.google.com/viewer?url=http://example.com/2011/sites/example/files/session/documents/sample.pdf&amp;embedded=true" style="width:470px; height:296px;" frameborder="0"></iframe>
      <p><a href="http://example.com/2011/sites/example/files/session/documents/sample.pdf">Download the document</a></p>        

I would like to edit the html outside the contents of the .field-item. Is it possible to do this in the module or do I have to create a custom theme tpl for it?


You should override the field template content-field.tpl.php to do that. The default template is usually found in /sites/all/modules/cck/theme/content-field.tpl.php.

Be aware that this will override the template for every field that is rendered, not just your custom formatted fields. In theory, you can build logic into the template that switches its output depending on the field type. However that is a bit of a hack; IMHO, all html output that is specific to your formatter, should be generated by the formatter itself and should stay inside div.field-item. Imagine that all formatters would start overriding content-field.tpl.php - that would create a mess because you never know which module is going to 'win' the override battle.

| improve this answer | |
  • Some good point raised here. – googletorp Mar 8 '11 at 9:02
  • Is there anyway to override this per field? I need to be able to control the output around the field as part of module. – digital Mar 16 '11 at 22:25
  • Like I said before, you can build logic into the template that switches its output depending on the field type. (pseudo code: if (field_belongs_to_my_module($field)) { $output = 'x' } else { $output = 'y' }). However, I recommend against it. It's a hack. I don't know your exact situation, but IMHO, you should be able to do what you want inside div.field-item. What you're saying is like "I'm designing a car, so I need to control the width of the road." It doesn't work like that. If all car manufacturers start re-shaping the road, traffic will become a mess. – marcvangend Mar 17 '11 at 9:04

I'm often using the excellent module Custom Formatters for this kind of purpose. Simple and clean.

| improve this answer | |
  • Do you know of any screencast/video which shows how to use custom formatters? – chefnelone Nov 1 '11 at 17:22

Edit: I see 2 other answers have been posted while I answered. In this case, the most you can take from here is using Semantic CCK.

The easiest way to do this is to use the Semantic CCK module. It allows you to override the markup per field without additional templates.

However if you want to keep it all in your module, then you need to implement to have hook_theme() implementation that returns the definition for content_field and return the needed markup.

Your hook_theme implementation would looks something like this:

mymodule_theme() {
  return array(
    'content_field' => array(
      'arguments' => array('element' => NULL),      

mymodule_content_field($element) {

You can also use hook_theme_registry_alter()

| improve this answer | |
  • I'm already doing something similar to this to alter the contents of the field. I need to address the html output outside of the field especially considering this uses multiple uploads. – digital Mar 16 '11 at 22:27
  • did you take a look at Semantic CCK? it does sounds like that is what you are looking for – zerolab Mar 17 '11 at 0:40

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.