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

  1. 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));
      }
    }
    ?>
  2. 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('&lt;pre&gt;'); print_r($form); print('&lt;/pre&gt;'); ?&gt;
    &lt;table border=1&gt;
        &lt;th&gt;My Header&lt;/th&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;?php print drupal_render($form['title']); ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
    &lt;?php print drupal_render($form); ?>
  3. 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'=&gt;t('My new something'),
        '#type'=&gt;'textfield',
        '#description' =&gt; 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;
  4. 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'=&gt;t('Please explain the problem. NOTE: Only the first 50 characters will be recorded'),
            '#name'=&gt;'TextField',
            '#type'=&gt;'textfield',
            '#attributes'=&gt;array(
                'onBlur'=&gt;'InputLengthCheck()',
                'onKeyUp'=&gt;'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

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Sie können syntax highlighting von Quelltext aktivieren, indem Sie folgende Tags einsetzen: <code>, <blockcode> The supported tag styles are: <foo>, [foo]. PHP-Quelltext kann auch in "<?php ... ?>" oder "<% ... %>" eingepackt werden.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zeilen und Absätze werden automatisch erzeugt.
  • Sie dürfen andere Beitrage mit [quote] Tags Zitieren.

Weitere Informationen über Formatierungsoptionen

CAPTCHA
Diese Frage dient dazu festzustellen, ob Sie ein Mensch sind und um automatisierte SPAM-Beiträge zu verhindern.
Image CAPTCHA
Enter the characters shown in the image.
 

Too Cool for Internet Explorer