1

i have made formexample.module, from Drupal 7 Pro Development

function formexample_menu() {
    $items['formexample'] = array(
        'title' => 'View the sample form',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('formexample_nameform'),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
}

function formexample_nameform() {
    $form['name'] = array(
        '#title' => t('Your name'),
        '#type' => 'fieldset',
        '#description' => t('what people call you'),
    );

    $form['name']['user_name'] = array(
        '#title' => t('Your name'),
        '#type' => 'textfield',
        '#description' => t('please enter name'),
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

and a theme function and preprocess function

function formexample_theme() {
    return array(
        'formexmaple_nameform' => array(
            'render element' => 'form',
            'template' => 'formexample-nameform',
        ),
    );
}

function template_preprocess_formexample_nameform(&$variables) {
    $variables['name'] = $variables['form']['name'];
    $variables['submit'] = $variables['form']['submit'];
}

this is template file for this theme() - formexample-nameform.tpl.php

<div id="formexample_nameform">
<?php
    print render($name);
    print render($submit);
?>
</div>

my question is: how and where to invoke this theme('formexample_nameform') , i thought i should insert function template_preprocess_formexample_nameform to my template.php file(with template rename to my theme name) but its not working for me

0

You just need to set it on the $form element inside your form function:

function formexample_nameform($form, &$form_state) {
  $form['#theme'] = 'formexmaple_nameform';
  // ...

When you use render element with a theme the variable on which you set #theme is the one that's passed through to the theme/preprocess functions.

Don't forget to add drupal_render_children() at the bottom of your template file or the form won't render correctly:

echo drupal_render_children($form);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.