hi guys

i'am working on a custom module that i want to have a form in it that writes the inputed data into the database

if i understand correctly i can have a tpl file that would display the layout of the form is that correct ?

and if so how do i implement this.
Actually advise on the complete setup would be helpfull if some kind soul would point me in the right direction

complete n00b to drupal

TIA

Comments

pbarnett’s picture

maori’s picture

heya

thanks for that :)

can i make the form look the way i want to ? as the example there i would imagine are side by side each other or under
which is where the tpl file come's into play correct ?

TIA

pbarnett’s picture

No, not at all.

The template file actually creates the final page content, but will define styles for that content that is defined in stylesheets, i.e. CSS files.

See http://drupal.org/project/Themes for general theming stuff and http://drupal.org/phptemplate for template file documentation.

jaypan’s picture

Actually, forms can be templated just like outputted content. For example, to theme the contact form:


// 1) Register the theme with a template file

function mytheme_theme()
{
  return array
  (
    'contact_mail_page' => array
    (
      'arguments' => array
      (
        'form' => NULL
      ),
      'template' => 'contact-page',
    ),
  );
}

// 2) Work with the preprocess function for the above theme in order to prepare variables for the template

function mytheme_preprocess_contact_mail_page(&$vars)
{
  $vars['from_name'] = drupal_render($form['name']);
  // continue this for the rest of the form elements;
  
  // And always make sure to call drupal_render on the rest of the form to render all the invisible stuff, or the form won't work
  $vars['form_extras'] = drupal_render($form);
}

And then the template works like this:
contact-page.tpl.php

  <h1>Your name is: <?php print $from_name; ?></h1>
  // and so on
  // don't forget to output the extras, or the form won't work
  <?php print $form_extras; ?>

Contact me to contract me for D7 -> D10/11 migrations.

stephenrobinson’s picture

maori’s picture

hiya

I did have a look at that but i dont think it will do what i need as the info submited is stored in to diffrent custom tables on the database

jaypan’s picture

Contact me to contract me for D7 -> D10/11 migrations.

maori’s picture

hiya

Thanks everyone this definatly points me in the right direction :D

maori’s picture

hi guys

ok i tried al the above but some how all i get is a blank page :(

in my form i have


function mylog_help($path, $arg) {
  $output = '';  //declare your output variable
  switch ($path) {
    case "admin/help#mylog":
      $output = '<p>'.  t("Test module for logginn data with a form") .'</p>';
      break;
  }
  return $output;
}

function MyLog_perm() {
  return array('access content');
}

function MyLog_menu() {
  $items = array();
  $items['MyLog'] = array(
    'title' => 'MyLog',
    'description' => 'The myLog.',
    'page callback' => 'mylog_preprocess_mylog',
    'access arguments' => array('access content'),
  );
  $items['MyLog/%MyLog'] = array(
    'description' => 'The MyLog',
    'page callback' => 'MyLog_preprocess_mylog',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
  );
    return $items;
}

function MyLog_theme()
{
  return array
  (
    'mylog' => array
    (
      'arguments' => array
      (
        'form' => NULL
      ),
      'template' => 'MyLog_form',
    ),
  );
}


function MyLog_preprocess_mylog(&$vars)
{
  $vars['from_name'] = drupal_render($form['feed']);
  


  $vars['form_extras'] = drupal_render($form);
} 

then in the tpl.php file i have

<h1>Your name is: <?php print $from_name; ?></h1>

  <?php print $form_extras; ?>

what have i messed up ? i have cleared the cache etc,etc

TIA

pbarnett’s picture

If you're seeing a completely blank page, a.k.a. the White Screen of Death, there's a PHP error somewhere - check the recent log entries and your Apache error log.

I notice you've got 'mylog' and MyLog as prefixes, which may have something to do with it.

maori’s picture

heya

yeah its the white page of death nothing in the drupal logs that i can see

pbarnett’s picture

Not the Drupal logs, the Apache error logs...

maori’s picture

okidoki

i'am getting a bit closer

function show_test() {
    $content = '';
    $content = 'Test content';
    $content2 = 'Test content2';

    $form['lead'] = array(
    '#type' => 'textfield',
    '#title' => t('test field'),
      '#default_value' =>  $user->name,
      '#size' => 20,
  );

    $content = theme('abc',$content, $content2, $form);
    return $content;
}

function mylog_theme() {
    return array(
        'abc' => array(
            'arguments' => array('content'=>NULL,'content2'=>NULL, $form_values),
            'template' => 'log_form',
        )
    );
}

and it now shows the tpl.php file with the content and content2 ok but not the textfield :( what am i missing here

TIA

jaypan’s picture

Your preprocess function should be called template_preprocess_mylog. Template functions are only prefixed by the module name when they are changing other module's templates.

I don't know if that will solve your problem, but it's somewhere to start.

Contact me to contract me for D7 -> D10/11 migrations.

maori’s picture

hi ya

ok changed the code to

function template_preprocess_mylog() {
global $user;

    $content = '';
    $content = 'Test content';
  
$form = array();

    $form['test'] = array(
    '#type' => 'textfield',
    '#title' => t('Test'),
      '#default_value' =>  $user->name,
      '#size' => 20,
  );

    $content = theme('abc',$content, $form);
    return $content;
}

function mylog_theme($form_state) {
    return array(
        'abc' => array(
            'arguments' => array('content'=>NULL, 'form' =>NULL),
            'template' => 'mylog_form',
        )
    );
    
}

and the output to the window is

Test content
Array

so its not showing the textfield in the tpl.php file

i know it's me but i bee blown as to known the reason :(

maori’s picture

okidoki :P

addedd

function phptemplate_mylog_form($form_values) {
    $variables = array( 'form' => $form);
    return _phptemplate_callback('mylog_form', $variables);
}

and the textfield now shows but dosnt show my username automatically ?

in the tpl file i have

<?php print drupal_render($form['test']); 

?>

jaypan’s picture

Preprocessing functions are always passed on argument by reference. Generally it's called 'vars'. So your preprocess function declaration will look like this:

function template_preprocess_mylog(&$vars)

$vars is a keyed array. The keys of the array become the variables that are available in your template file. For example:

function template_preprocess_mylog(&$vars)
{
  $vars['greeting'] = t('Hello, and welcome to this site);
}

The key of the array is 'greeting', so now, in the template file, you can do this:

<h1><?php print $greeting; ?></h1>

And this will print out the following to the source:

<h1>Hello, and welcome to this site</h1>

And will print this to the screen:

Hello, and welcome to this site

Contact me to contract me for D7 -> D10/11 migrations.

maori’s picture

hi jay

I tried what you posted above and it does work as it should but the textfield still dosnt show the username :( here the full code of my module

 <?php

 function testlog_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#testlog":
      $output = '<p>'.  t("test for a form Logging module") .'</p>';
      break;
  }
  return $output;
}

function testlog_menu() {
    $items['log'] = array(
        'title' => 'test Log',
        'page callback' => 'testlog_page',
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
}

function phptemplate_testlog_form($form) {
    $variables = array( 'form' => $form ); 
    return _phptemplate_callback('testlog_form', $variables);
}

function testlog_page() {
global $user;

// this should fill in the username of the logged in user 
    $form['no1'] = array(
    '#type' => 'textfield',
    '#title' => t('user name'),
      '#default_value' =>  $user->name,
      '#size' => 10,
  );

// add more form elements below

    $form = theme('abc', $form);
    return $form;
}

function testlog_theme($form) {
    return array(
        'abc' => array(
            'arguments' => array('form' =>NULL),
            'template' => 'testlog_form',
        )
    );
    
}

and in the tpl file i have


<div>
        <table width= '100%' style="background-color:#dedeff;">
                <tr>
                        <td align='center'>holder for a form element 1</td>
                        <td align='center'>holder for a form element 2 </td>
                </tr>
                <tr>
                        <td align='center'><?php print drupal_render($form['no1']); ?></td>
                        <td align='center'>holder for a form element 4</td>
                </tr>
        </table>
</div>


            <?php print drupal_render($form); ?>

so what i'am after is the #default_value' for the textfield to be filled in by defualt here it will be the logged in user it works if i dont use the tpl file way so i'am at a loss to understand why

thanks for all your help

maori’s picture

well been at it for a few more hours today and still cant seem to work this one out :(

i bet its one of those that i been making it harder than it is i'am sure but cant belive it is this hard to get a form to look the way i want to rather than the elememnts staked below each other :(

maori’s picture

finnally :P

ok so far so good posting the code here in case someone else comes across the problems i have been having

  function testlog_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#testlog":
      $output = '<p>'.  t("test for a form Logging module") .'</p>';
      break;
  }
  return $output;
}

function testlog_menu() {
    $items['log'] = array(
        'title' => 'test Log',
        'page callback' => 'testlog_page',
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
}

function testlog_page() {
  return drupal_get_form('testlog_page_form');
}

function testlog_theme()
{
   $themes = array(
        'abc' => array(
            'arguments' => array('form'),
            'function' => 'testlog_testlog_page_form',
        ),
    );
    return( $themes );
}

function testlog_testlog_page_form($form)
{
    return theme_render_template(path_to_theme() . '/testlog_form.tpl.php', array('form' => $form));
}

function testlog_page_form($form_state) {
global $user;

// this should fill in the username of the logged in user 
    $form['no1'] = array(
    '#type' => 'textfield',
    '#title' => t('user name'),
      '#default_value' =>  $user->name,
      '#size' => 10,
  );

// add more form elements below
 
 $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

$form['#theme'] = 'abc';
    //$form = theme('abc', $form);
    return $form;
}


function testlog_page_form_submit($form_id, $form_values) {
  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';
  drupal_set_message(t($message));
}

the only problem i have is the submit function dosnt seem to work :( click the button and the page refreshes but dosnt give the info it should