Theming CCK forms with the Forms API?
talltim - May 2, 2006 - 18:46
| Project: | Content Construction Kit (CCK) |
| Version: | 4.7.x-1.x-dev |
| Component: | content.module |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
Is it possible to theme CCK forms using the Forms API? I'm getting the hang of theming CCK nodes, but my forms are a huge list of fields and I'd like to group them more sensibly (ideally, using tabs or the same collapsible areas that the core admin pages use). Any links to relevant docs - I've done quite a bit of searching, but nothing found...

#1
Me too, I have a large list of fields and I'd also like to group them. So does anyone know if this is possible or in the pipeline?
#2
Super easy folks:
First, add the following code to your template.php file (make one, if you haven't).
/* Enables node_form.tpl.php */function phptemplate_node_form($form) {
return _phptemplate_callback('node_form', array('form' => $form));
}
Now create a file called node_form.tpl.php. Let's assume you only wanted two fields, one named Frank, the other Sue. Do this
<?phpprint_r(form_render($form['field_frank']));
?>
<?phpprint_r(form_render($form['field_sue']));
?>
<?phpprint_r(form_render($form));
?>
Does this answer your question?
#3
Thanks for the tip - I have a number of different CCK content types and therefore a number of different forms, a couple of which share fields. How do I theme these forms individually, as they'll need different fieldsets etc?
#4
Check out Contemplate.module: worked like a charm for me.
#5
Does this allow me to theme forms? Looking at the module docs and README, it looks like it's only intended to theme nodes/teasers - something I'm already doing with custom node-content-xxx.tpl.php files.
#6
They are both great ideas but for different stages.
- The technique by Nick Lewis is the one to use when you want to theme the form that creates the node (the ones that the users will be getting when inserting data).
- The contemplate.module is the one to use when theming the presentation of the node created . It’s also a fast way to find out the name of the fields available to you.
Hope this helps.
#7
What happens if you have maybe 3 cck modules, but you want to use one form to insert all the data in.
After On the same form, be allowed to insert data into one cck module EX:Video inventory, one module dedicated to Director (which you input once in the form) and a dedicated module for films (which you can input x amount of films on the same form)
#8
As http://drupal.org/node/69472 explains (and hopefully fixes) CCK forms can only be themed via the general node_form callback, not via their form_id because a dash (-) is not valid in function names.
#9
@Nick,
How do you use your solution without then breaking all the other node submission forms such as story, page, etc?
#10
Test the id of the form in the theme hook($form['form_id']['#value']) and only when it is a specific CCK form pass it through your theme code, otherwise just return / print nothing or call theme_node_form($form).
#11
Heine,
Can you possibly give me a more specific example, as in showing me the code and where it goes?
Regards
Patrick
#12
Suppose I have a CCK form of the node type aangeboden, then it's (broken) form_id will be content-aangeboden_node_form. The following code in template.php checks the form_id of every passed form and uses form-aangeboden.tpl.php only when the cck form 'aangeboden' needs to be styled. Other nodetypes will be displayed by the default theme. If you want to aspecifically route cck forms through themeing code, you can use the full arsenal of php functions to determine whether the form_id contains 'content-' .
<?phpfunction phptemplate_node_form($form) {
if ($form['form_id']['#value'] == 'content-aangeboden_node_form') {
return _phptemplate_callback('form-aangeboden', array('form' => $form));
}
}
?>
then in form-aangeboden.tpl.php (you probably want to do something more exciting):
<?phpprint '<strong>The CCK form of the type aangeboden</strong>';
print form_render($form);
?>
#13
That works just great, Heine. Thanks a lot.
Regards
Patrick
#14
One more question... :)
I notice that Nick's use of form_render (i.e,
print_r(form_render($form['field_frank']));) renders all the form elements. In other words, it displays the label and the associated help text as well as the actual form element (textfield, etc).Is there another function that can be used to split these elements up? I would prefer to be able to display the label and the form element seperately for example rather than in the standard Drupal way. But only for the form for this one particular content type. I'm happy to leave the other forms 'as-is'.
Any ideas?
#15
Sure, as long as you know what the fields are named (use var_dump($form['the key']) to find out.
Very stupid, hackish example that pulls out the description underneath the Author name box on the story submission form and displays it somewhere else:
<?phpfunction phptemplate_story_node_form($form) {
$name_desc = $form['author']['name']['#description'];
$form['author']['name']['#description'] = '';
$output .= form_render($form);
$output .= "<strong>$name_desc</strong";
return $output;
}
?>
Not sure whether this always generates nice HTML. But you can of course override the different theme_elements as well.
#16
Further question - I've followed all the steps in here and come to the realisation that I have absolutely no idea how to render the form buttons (submit, etc.).
Any help, anyone? Please!
#17
Use form_render($form[field_name]) for each individual field you want to use, then finish the whole thing up with form_render($form) which will finish things up and display all the parts of the form that were not displayed already, like the submit buttons.
#18
Thank you Karen, that works just fine.
Regards
Patrick
#19
The form IDs are changed now; the hack should no longer be necessary.
#20
#21
I have been referring to this thread for the past couple of hours as I get to grips with theming forms. This might come in useful for fellow php amatures. The code below explains how to change the size of a text field. This would desirable if you only want the user input one number or a zip code or another short piece of info. Let me know if there are any errors below.
<?php/*This replaces the default value of the size part of the title field contained within the Array of $form.
Note that 'title' is an array inside the $form array, i.e. a multi-dimensioinal array or nested array
*/
$form['title']["#size"] = 10;
// This simply outputs the form.
print form_render($form['title']);
// To See the contents of the $form array uncomment the line below
// var_dump($form['title']);
?>
#22
Just renaming the issue with its orignal name after I accidently changed it. Ahem. (looks around and quickly makes for the door).
#23
Using CCK 4.7
CCK node content name is "business"
I presume the node name is "content_business" because when looking at the content.module code, it formats with content_ in lieu of content-
Here is the template.php contents:
<?php/* Pass the form template for Directory Listing through themes. added jghyde 9/24/06 */
function simplex_node_form($form) {
if ($form['form_id']['#value'] == 'content_business_node_form') {
return _phptemplate_callback('form-business', array('form' => $form));
} else {
return;
}
}
?>
and the template file is form-business.tpl.php. In it is simply:
print '<strong>The CCK form of the type Business</strong>';This renders a blank page whenever you submit a login and other blank pages after submitting forms. If I delete the template.php file, the problem goes away.
What is wrong with my code?
#24
The problem with the blank page is solved.
Blank page and template,php problem is caused by having any space, character, gremlins, or etc BEFORE the opening "<?". Get rid of the white space and it works.
BUT, it still does not appear to intercept the content_business input form so I can theme it. I get the default, standard form un-themed.
joeh
#25
More details on themeing forms generated by CCK, and another example, is available here: http://drupal.org/node/85908
#26
And just to be a pain in the system, on my end finishing things up with
<?phpprint form_render($form)
?>
#27
In Drupal 5.0 form_render has changed to drupal_render
#28
subscribing