drupal - theme or/and alter forms
Source: drupal.org, written by light-blue (thanks!)
Below is an super powerful way in 5.7 to give you total control of theming any node form (e.g. a cck-based content type called news_article). It uses template files, so your designers will be less confused as well. This example uses the zen theme by the way...
-
put this in template.php (uncomment the print statement to see the template filename required)
<php
function zen_node_form($form) //zen, or phptemplate, or yourthemename {
//print('/YOUR_(SUB_THEME_NAME_IF_ZEN)_THEME_NAME_HERE/'.$form['type']['#value'].'_form.tpl.php');die();
if(file_exists(path_to_theme().'/YOUR_(SUB_THEME_NAME_IF_ZEN)_THEME_NAME_HERE/'.$form['type']['#value'].'_form.tpl.php')) {
return _phptemplate_callback($form['type']['#value'].'_form', array('user' => $user, 'form' => $form));
}
}
?> - create a template file in your (sub if zen) template directory as nodetype_form.tpl.php (my live site uses workorder_form.tpl.php). uncomment the line below to see all the variables you have access to!
<?php //print('<pre>'); print_r($form); print('</pre>'); ?>
<table border=1>
<th>My Header</th>
<tr>
<td><?php print drupal_render($form['title']); ?></td>
</tr>
</table>
<?php print drupal_render($form); ?> - if you want to modify the $form variable (to add new form elements, like text fields for example) before that template file loads, put this in a (hopefully your own custom) module or in template.php if you're lazy / testing...
<?php
function ANY_MODULE_NAME_form_alter($form_id, &$form) {
$form['something'] = array(
'#title'=>t('My new something'),
'#type'=>'textfield',
'#description' => t('Please enter your new something.')
);
}
?>Troubleshooting: I just had a problem of input widths being default to 60, which was too wide for my application. Here's how I fixed it. I went into my workorder_form.tpl.php, uncommented the <?print(''); print_r($form); print(''); ?>
and realized I needed to adjust the size attribute like so:$width=20;
$form['field_cm_wo_contact_name']['0']['value']['#size']=$width; - More Troubleshooting: If you need to add fields to your form that are NOT CCK (I'm using charLeft.js -- which *cannot* understand field_something[0]['value'] since it stops at the first bracket -- to show number of remaining characters for a textfield), use form_alter to do something like:
<?php
function commercial_workorders_form_alter($form_id, &$form) {
$form['TextField']=array(
'#title'=>t('Please explain the problem. NOTE: Only the first 50 characters will be recorded'),
'#name'=>'TextField',
'#type'=>'textfield',
'#attributes'=>array(
'onBlur'=>'InputLengthCheck()',
'onKeyUp'=>'InputLengthCheck()')
);
}
//note that in this format, all the submit handlers for CCK and your custom ones fire--thanks to Crell for pointing that out to me
$form['#submit']['commercial_workorders_new']=array('');
?>then create a commercial_workorders_new to send that value to the correct CCK field like this:
<?php
//note that you have access to the entire $form
function commercial_workorders_new(&$node, &$form_values, &$form) {
$form['field_cm_wo_problem'][0]['value']=$form_values['TextField'];
}
?>

Kommentar hinzufügen