Pergunta

In a node I get the values of CCK fields as $ array or $_rendered as the rendered view of the field. How can I change the $<field_name>_rendered from its default way of rendering?

Foi útil?

Solução

Drupal is amazing in the many ways it let's you override the final rendered HTML. I want to point out that you can also override how the field is rendered at the theme layer.

Just copy the file ...contrib/cck/theme/content-field.tpl.php to your personal theme folder. and rename it to

content-field-[field_name].tpl.php where [field_name] is the id/name of the cck field you want to override.

Just edit that file to your liking. And make sure you reconstruct the theme chache tables so that Drupal recognizes the new template file.

Outras dicas

The Custom Formatters module will help you quickly and elegantly. Here is a quote about it (from the module's project page):

... allows users to easily create custom CCK/Field Formatters without the need to write a custom module. Custom Formatters can then be exported as CTools Exportables/Features or Drupal API Formatters.

CCK fields use something called field formatters to control it's output. This is also how fields do it in Drupal 7. Field formatters is really just a theme function that's called.

In the content type setting: Display fields it's possible to select a formatter for normal view mode and teaser view mode. If the default formatters are not usable, you need to create your own formatter for the field and a theme function that will control how it's rendered.

I've written a blog post: Creating a field formatter for CCK a long time ago, at times I still use it myself for reference so it might help you out.

Alternatively you can use your theme to overwrite the default theme function for the formatter. This will however change all fields on your site with the selected formatter.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a drupal.stackexchange
scroll top