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...

Comments

Barry Pretsell’s picture

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?

Nick Lewis’s picture

Version: 6.x-1.x-dev » 4.7.x-1.x-dev

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

print_r(form_render($form['field_frank']));
print_r(form_render($form['field_sue']));
print_r(form_render($form));

Does this answer your question?

talltim’s picture

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?

rory--’s picture

Check out Contemplate.module: worked like a charm for me.

talltim’s picture

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.

introfini’s picture

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.

Coldice4678’s picture

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)

heine’s picture

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.

Patrick Nelson’s picture

@Nick,

How do you use your solution without then breaking all the other node submission forms such as story, page, etc?

heine’s picture

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).

Patrick Nelson’s picture

Heine,

Can you possibly give me a more specific example, as in showing me the code and where it goes?

Regards

Patrick

heine’s picture

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-' .

function 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):

print '<strong>The CCK form of the type aangeboden</strong>';
print form_render($form);
Patrick Nelson’s picture

That works just great, Heine. Thanks a lot.

Regards

Patrick

Patrick Nelson’s picture

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?

heine’s picture

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:

function 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.

Patrick Nelson’s picture

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!

karens’s picture

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.

Patrick Nelson’s picture

Thank you Karen, that works just fine.

Regards

Patrick

jonbob’s picture

Status: Active » Fixed

The form IDs are changed now; the hack should no longer be necessary.

Anonymous’s picture

Status: Fixed » Closed (fixed)
ncameron’s picture

Title: Theming CCK forms with the Forms API? » Resizing text fields

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']);
?>
ncameron’s picture

Title: Resizing text fields » Theming CCK forms with the Forms API?

Just renaming the issue with its orignal name after I accidently changed it. Ahem. (looks around and quickly makes for the door).

jghyde’s picture

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?

jghyde’s picture

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

jghyde’s picture

More details on themeing forms generated by CCK, and another example, is available here: http://drupal.org/node/85908

ryivhnn’s picture

And just to be a pain in the system, on my end finishing things up with print form_render($form) dumps in everything that wasn't already dumped BUT things like authoring information etc come up as just text rather than links. Is that normal or have I missed something blatantly obvious? (wouldn't be the first time... :)

cday119’s picture

In Drupal 5.0 form_render has changed to drupal_render

chlobe’s picture

subscribing