Active
Project:
Content Construction Kit (CCK)
Version:
6.x-2.0-rc3
Component:
Usability
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
11 Jun 2008 at 13:12 UTC
Updated:
18 Mar 2013 at 21:56 UTC
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
Comment #1
yang_yi_cn commentedI'm having the same question...even the default field.tpl.php is not working
Comment #2
yched commentedFrom 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
Comment #3
yang_yi_cn commentedit 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
Comment #4
yched commentedPlease make sure you're using latest CCK 6.x-2.x. I committed a fix for this a few days ago.
Comment #5
zmove commentedHi,
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
Comment #6
zmove commentedComment #7
yched commented"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.
Comment #8
zmove commentedHi,
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.
Comment #9
yched commentedYour feeling is right. This will work of course, but is only a workaround, not inline with drupal theming principles.
Comment #10
yched commentedReintroduced the search for variant template files, and added a cck/theme/README.txt file
Comment #11
yched commentedForgot 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.
Comment #12
karens commentedAnyone 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.
Comment #13
zmove commentedHi,
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 !
Comment #14
zmove commentedHi,
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 :
drupal_set_message('<pre>'.print_r('test', true).'</pre>');I tried to rename this file into :
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 ?
Comment #15
zmove commentedHi,
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...
Comment #16
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #17
Jeffdo commentedI'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
Comment #18
alanburke commentedNo 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
Comment #19
najibx commentedhi ...could someone update the Theming CCK Input Form Handbook. for D6. I'm getting a bit and pieces here and not so clear either :-)
Comment #20
chachasikes commentedI'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
(thanks alanburke: http://drupal.org/node/269319)
Comment #21
Michsk commentedchachasikes thanks for this! really helped me out.
Comment #22
missym commentedThanks!
Comment #23
aac commentedSubscribing!!
Comment #24
tomcatuk commentedI'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.
Comment #25
fehin commentedSubscribing.
Comment #26
fehin commentedSubscribing.
Comment #27
cladom commentedThis is very good and it works. [#20]
Now, the same, for the edit page?
How can we do that?
thank you
Comment #28
oknateIf 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.
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.
Comment #29
drurian commentedThis 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.
Comment #30
davemybes commentedI 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.
Comment #31
samlerner commentedIf you're using a sub-theme, like with Zen, you'll also need to copy
content-field.tpl.phpinto your theme directory. I couldn't getcontent-field-field_image.tpl.phpto work in my theme until I copiedcontent-field.tpl.phpin as well.Comment #32
drurian commentedJust wanted to say I've made a video tutorial about theming CCK fields in Drupal 6.
Comment #33
claar commentedComment #34
cookiesunshinex commented@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:
Comment #35
sunwukong commentedSubscribing.
Comment #36
iantresman commentedI was having problems getting this to work until I disabled and uninstalled the Contemplate Module and Display Suite. I guess one of them was intercepting the theme.
Comment #20 worked for me. I had a CCK field called
field_amazon, so I created a template file calledcontent-field-field_amazon.tpl.php, which I stored in a directory calledtemplatesin my theme directory.Once it was working, I could make changes to the template, upload them, and have the changes appear on refreshing the node. I did not need to clear the cache each time.
I was hoping that Contemplate would let me do this via a module, but it requires you to them the entire node, and it doesn't take the CCK Display Fields settings into account. And Display Suite seemed to le you do this (and much much more) to Views, not nodes.
1 Jul 2012 Note: This suddenly stopped working for me. I was able to re-enable it by (a) ensuring that my theme root had a copy of
content-field.tpl.php(copied fromall\modules\cck\theme), and I moved my own template file out of mytemplatesdirectory, and also into my theme root.Comment #37
djbobbydrake commented@iantresman Thx for that last tip. Had trouble with this until we also copied over content-field.tpl.php to our theme folder.