Download & Extend

How to theme CCK field with a content-field-my_field.tpl.php file

Project:Content Construction Kit (CCK)
Version:6.x-2.0-rc3
Component:Usability
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

Hi,

I use CCK since the 5.x version of Drupal and it was pretty easy to theme an individual CCK field with an external .tpl.php file.

With the 6.x version, I don't find instructions to do the same thing, no template.php file in the package, no instructions on the readme.. just a quick reference on the template_preprocess_field() function that is unfoundable on the api, on drupal.org search engine or on google...

How can I do that please ?

thanks

zmove

Comments

#1

I'm having the same question...even the default field.tpl.php is not working

#2

Status:active» fixed

From D6, no specific CCK instructions are needed; overriding the template is now done just like for any other module : find the template file that interests you, copy it into your theme's folder, override the file to suit your needs, rebuild your cache registry, et voila.
In your case, the file is in cck's 'theme' folder (which makes us assume it's rather easy to spot, this will probably become a sort of convention for 'large' module anyway).

Be sure to check devel's devel_themer.module whenever you're having troubles figuring out what to override

#3

Status:fixed» active

it seems to be related to the 'path' set in hook_theme() of the content.module?
it looks like now it uses cck/theme/field.tpl.php instead of the php in theme/mytheme/field.tpl.php

#4

Status:active» fixed

Please make sure you're using latest CCK 6.x-2.x. I committed a fix for this a few days ago.

#5

Version:6.x-2.x-dev» 6.x-2.0-rc3
Status:fixed» postponed (maintainer needs more info)

Hi,

Thank you for your answers, but the problem is not to theme CCK fields, I saw that there is a field.tpl.php and a content-field.tpl.php files under the theme folder of CCK module.

The problem is to theme a specific field, for example, a field named "field_name" in CCK.

With drupal 5, you had to put a function in your template.php file (the function was given by CCK) and then, declaring a field-_my_field.tpl.php file to theme it (in my example it gives "field-field_name.tpl.php")

With drupal 6 and CCK 2, there is no instructions about theming a single field depending on his name, there is only a field.tpl.php without documentation at all. I tried to define same naming convention "field-my_field.tpl.php", but it doesn't work.

So I make my post as active again, how to theme a single CCK field depending on his name ?

regards,

zmove

#6

Status:postponed (maintainer needs more info)» active

#7

"I saw that there is a field.tpl.php and a content-field.tpl.php files under the theme folder of CCK module."

No, there is only a content-field.tpl.php file. The other one is a leftover from a previous CCK D6 state (the file was renamed from field.tpl.php to content-field.tpl.php), which is still there probably because you copied the new /cck folder over the old one instead of deleting the old one.

This being said, I can't seem to get a field-specific template to be used either. I'm afraid this is related to the 'formatters as theme' refactoring, where the actual call to theme() is left to drupal_render($element = array('#type' => 'content-field', ...)). I could have sworn I tested per-field templates since this got in, though...
Investigating.

#8

Hi,

Ok for content-field.tpl.php, I replace my 6.x-dev version of CCK by the RC3, that's why I had the field.tpl.php as you supposed.

For the field specific theming, one people give me an advice on the IRC channel. It's to make a switch() in the content-field.tpl.php to check the field name, and put the code inside the different "case:".

But I don't really like the idea, it's like putting a switch($node->type) in the node.tpl.php file to theme differently your node types whereas there is a native core function that allow you to declare a node-node_type.tpl.php to clearly separate the differents templates.

That's why I'm waiting for a good way to do to theme a specific field depending on his name as CCK for drupal 5 provided.

#9

Your feeling is right. This will work of course, but is only a workaround, not inline with drupal theming principles.

#10

Status:active» fixed

Reintroduced the search for variant template files, and added a cck/theme/README.txt file

#11

Forgot to add : in addition to content-field-[FIELD_NAME].tpl.php, we now support the content-field-[CONTENT_TYPE].tpl.php and content-field-[FIELD_NAME]-[CONTENT_TYPE].tpl.php patterns.

#12

Anyone trying to do this should be aware that it may not work correctly in Drupal 6.2. There is a core bug at #241570: Theme preprocess functions do not get retained when using patterns that is fixed in Drupal 6-dev and will be fixed in Drupal 6.3 when it is released. The bug is that the preprocessor doesn't get triggered so the values you get in the template may not be right.

#13

Hi,

I already applied the patch proposed by merlinsofchaos mentionned in views 2, so I imagine it will be ok for me.

Thank you both KarenS and yched for this reply, I will test the new cck pattern, the addition of the content type in the pattern is just wonderfull !

#14

Status:fixed» postponed (maintainer needs more info)

Hi,

I tried all possibilities but I don't find how to theme a single field...

I have a CCK field named : "field_yield"

I copy and paste the content-field.tpl.php file in the them directory of CCK into my theme folder and I add this line on the top of it :

<?php
drupal_set_message
('<pre>'.print_r('test', true).'</pre>');
?>

I tried to rename this file into :

  • content-field-field_yield.tpl.php
  • content-field-yield.tpl.php
  • content-field-field-yield.tpl.php
  • content-field-[field_yield].tpl.php
  • lot lot lot of others.... I think I tried all possibilities

Nothing worked, I clear cache between all my tests.

I'm on the very recent drupal 6.3, maybe this is the problem, and I'm on CCK rc3, do I have to download the latest dev version to make it work ? or do I miss something ?

#15

Status:postponed (maintainer needs more info)» fixed

Hi,

the problem comes from me, I didn't copy the content-field.tpl.php file in my theme root folder, I only had the content-field-field_yield.tpl.php file.

Sorry...

#16

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

#17

I'm experiencing the bug with content-field.tpl.php, in Drupal 6.3.

Was this fixed in that last release or is a patch required?

Cheers

#18

No Patch required
Use CCK RC4
Drupal 6.3

Make sure that content-field.tpl.php [base template]
is in your theme folder
as well as
content-field-field_myfield.tpl.php

Regards
Alan

#19

hi ...could someone update the Theming CCK Input Form Handbook. for D6. I'm getting a bit and pieces here and not so clear either :-)

#20

I'm trying to organize my own notes for this-- here's what I have - I'm not sure how to edit drupal handbooks.

Theme Individual CCK 2 Fields

  • copy content-field.tpl.php from sites/all/modules/cck/theme to your theme folder
    (thanks alanburke: http://drupal.org/node/269319)
  • make your template: content-field-field_name_of_my_field.tpl.php -- placed in your theme folder (or other file names, see theme info output from devel module or complete list of new templates (note: i usually just put gobbledy gook html in an empty file - just to get the template to show up something besides default output. Later, I go back and really edit the theme output for the fields with the correct syntax. You don't need to clear the cache after you get your tpl.php registered (again, unless you move/rename it)
  • add your new template files to your theme folder - you can nest these in folders, just keep in mind you need to clear your cache to get the templates to show up if you move them around
  • clear theme registry cache manually, or visit admin/build/modules to trigger a rebuild, or execute drupal_rebuild_theme_registry(); to register your new template files

#21

chachasikes thanks for this! really helped me out.

#22

Thanks!

#23

Subscribing!!

#24

I've spent a couple of weeks looking for what was in this thread and this one. Damn, it's not so much knowing what's wrong, it's knowing how to ask the question in the right way.

#25

Subscribing.

#26

Subscribing.

#27

Status:closed (fixed)» active

This is very good and it works. [#20]
Now, the same, for the edit page?

How can we do that?

thank you

#28

If you want to theme a field in Drupal 6 and you are developing your own module, you can do the following:

Add your module to the theme registry for 'content_field'.

By using array_unshift, which adds an item to the beginning of an array, you can place your custom module first in line for the content_field templates.

<?php
function mycustommodule_theme_registry_alter(&$theme_registry) {


   
$thisPath = drupal_get_path('module', 'mycustommodule');
   
array_unshift($theme_registry['content_field']['theme paths'],$thisPath);
}
?>

You could then put your own content-field.tpl.php in your module folder, or, as was more useful to me, you can create a field template for just the content type you're working with in your custom module, content-field-mycustomcontenttype.tpl.php. You then have to clear the theme registry, and voia! You can now edit the HTML hidden inside the $content variable in your node template. And also the template sits in your custom module instead of your theme folder. I try to avoid adding more templates to my theme folder, because it keeps all the files for your custom module together, and it keeps your theme folder and template.php file in your theme folder from getting weighed down with hundreds of files. This sort of organization is really helpful, if you are working on a large drupal site with lots of custom modules.

#29

This didn't work for me. The module path shows in the 'theme - paths' at index 0, but the default cck template is still being used.

#30

I realize this is old, but here goes anyway. Make sure your tpl file is called content-field-field_name_of_field.tpl.php. Note the two "field"s in there. Also note the dashes and underscores. The simplest way to get this right is to go to the Manage fields tab and copy your field name from there. This will be field_name_of_field. Then put a dash in front of that and add it to the content-field template file.

#31

If you're using a sub-theme, like with Zen, you'll also need to copy content-field.tpl.php into your theme directory. I couldn't get content-field-field_image.tpl.php to work in my theme until I copied content-field.tpl.php in as well.

#32

Just wanted to say I've made a video tutorial about theming CCK fields in Drupal 6.

#33

Title:How to theme CCK field by declaring a field-my_field.tpl.php file» How to theme CCK field with a content-field-my_field.tpl.php file

#34

@drupal librarian - Thanks for the video.

#15 and #18 worked for me.

I now have the following in my theme directory, and a field called field_artistwebsite in my content type.
content-field.tpl.php
content-field-field_artistwebsite.tpl.php

I was able to re-write the output of a CCK Link field so that it removes the "http://" on the display title on the node.

I did this by adding the following lines of code in replace of the commented out print statement below:

          <?php $item['display_title'] = str_replace("http://","",$item['display_title']);
          echo
"<a href=\"".$item['url']."\">".$item['display_title']."</a>";
         
         
/* Commented out original PHP print statement
           * <?php print $item['view'] ?>

           *
           */
          ?>

#35

Subscribing.