1

I want to create a contact form using FAPI my custom module.

I created 2 files: one is .info and the other is .module. But I do not understand how I should link that module with my template name page--contact-us.tpl.php as I already activated that module from module list.

I used the code below in my module_name-module:

<?php
  function my_module_form($form,&$form_state) {
  $form['name'] = array(
    '#type' => 'textfield',
  );
  $form['company'] = array(
    '#type' => 'textfield',
  );
  $form['phone'] = array(
    '#type' => 'textfield',
  );
  $form['email'] = array(
    '#type' => 'textfield',
  );
  $form['message'] = array(
    '#type' => 'textfield',
  );
  return $form;
  }

And in my template.php file I wrote this code:

<?php
 function mytheme_theme($existing, $type, $theme, $path) {
 $items['my_module_form'] = array(
 'render element' => 'form',
 'template' => 'page--contact-us',
 'path' => drupal_get_path('theme', 'mytheme') . '/template',
  );
  return $items;
  }
  ?>

Finally I wrote a single line of code in my page--contact-us.tpl.php file to check whether a name field is appearing or not:

<?php print render($form['name']); ?>.

But nothing is happening on my contact-us page ... Why?

UPDATED: I also implemented hook_menu, here it is:

 function form_module_menu() {
 $items = array(); 
 $items['contact-us'] = array(
 'page callback' => 'drupal_get_form',
 'page arguments' => array('form_module_form'),
 'access callback' => TRUE
 );
 return 
 $items;
 }
2
  • Implement hook_theme
    – Rupesh
    Commented Mar 21, 2015 at 6:11
  • Thanku @Rupesh for your reply, I updated my question, I tried the above code but form field is not appearing. Can you please guide me where I am wrong?
    – nomeer
    Commented Mar 21, 2015 at 6:57

3 Answers 3

1

When dealing with Form API in Drupal 7 there are two major steps:

Step 1: Create and implement the form in a module

In this step 1, you will setup the form, and then validate and submit the form, as further detailed below.

a) Setup the form

    function form_example() {
       $form['first_name']=array(
       '#type' => 'textfield',
       '#title' => t('First name'),
       '#description' => t('Enter first name'),
        );
        $form['last_name']=array(
        '#type' => 'textfield',
        '#title' => t('Last name'),
        '#description' => t('Enter last name'),
        );
    $form['submit_button'] = array(
        '#type' => 'submit',
        '#value' => t('Click Here!'),
        );

    return $form;
    } 

    function form_example_validate($form, &$form_state) {

    }
    function form_example_submit($form, &$form_state) {

    }

b) Validate and submit the form

Now the form is ready to be displayed.

One way to display the form, is to theme the form in the module and then print the form.

function mytheme_theme($existing, $type, $theme, $path) {
   'theme_contact_form' => array (
        'variables' => array(
            'contact_form' => FALSE,
            ),
        'template' =>'theme-contact-form',
    ),
 }
function mytheme_block_info() {
   $blocks['block_contact_form'] = array(
      'info' => t('My Theme Contact form'),
      );
   return $blocks;
}
function mytheme_block_view($delta = '') {
  $block = array();
  switch ($delta) {
  case 'block_contact_form':
        $block['content'] = array(
            '#theme' => 'theme_contact_form',
            '#contact_form' => drupal_get_form('form_example'),
            );
        break;
  }
  return $block;
}

Then create theme-contact-form.tpl.php in the module file and print the form:

<?php print render($contact_form); ?>

With all previous steps complpeted, the form is created and implemented in the module.

Step 2: style the form

To style the form from theme layer, refer to How to a use a template file to theme a form?

2
  • Thanku @BinYarbua, I have my template file named page--contact-us.tpl.php and it placed in mytheme/templates/page--contact-us.tpl.php, I had created my .module file as I mentioned in my question. but I didnt get form on my contact-us page?
    – nomeer
    Commented Mar 21, 2015 at 8:41
  • @nomeer be sure of file template name (templates or template) and name when use it.
    – BinYarbua
    Commented Mar 21, 2015 at 9:16
0

You are missing a hook_menu to define your page.

This hook enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu (usually the Navigation menu). A path and its associated information is commonly called a "menu router item". This hook is rarely called (for example, when modules are enabled), and its results are cached in the database.

By having the hook_menu you can created the path on your website.

1
  • Thanku @darol100, I updated my ques and implement hook_menu. Is this right?because I do not get my form on contact-us page?
    – nomeer
    Commented Mar 21, 2015 at 8:07
0

Try these working Code

form_module.module contents

<?php

function form_module_menu() {
  $items = array();
  $items['contact-us'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_form'),
    'access callback' => TRUE
  );
  return $items;
}

function my_module_form($form, &$form_state) {
  $form['name'] = array(
    '#type' => 'textfield',
  );
  $form['company'] = array(
    '#type' => 'textfield',
  );
  $form['phone'] = array(
    '#type' => 'textfield',
  );
  $form['email'] = array(
    '#type' => 'textfield',
  );
  $form['message'] = array(
    '#type' => 'textfield',
  );
  return $form;
}

function my_module_form_submit($form, &$form_state) {

}

function my_module_form_validate($form, &$form_state) {

}

function form_module_theme($existing, $type, $theme, $path) {
  $items['my_module_form'] = array(
    'render element' => 'form',
    'template' => 'page--contact-us',
    'path' => drupal_get_path('module', 'form_module') . '/template',
  );
  return $items;
}

AND form_module/template/page--contact-us.tpl.php contents

MY FORM CUSTOM
<?php echo drupal_render($form['title']); ?>
<div class="company">
    <?php echo drupal_render($form['company']); ?>
</div>
<?php echo drupal_render_children($form); ?>
2
  • Thanku @Rupesh, my template is in mytheme/templates/page--contact-us.tpl.php thats why in function my_module_theme($existing,$type,$theme,$path) I replaced the path like this ('theme','my_theme').'/templates', is this right? and in my template file, form is appearing using echo drupal_get_form('my_module_form') but I want to print individual fields like echo drupal_render($form['field_name'])
    – nomeer
    Commented Mar 24, 2015 at 4:56
  • You are working on module level so you should have to keep tpl inside module as per drupal standard. Above code will fully work as per your requirement(drupal_render). Wrong Way (echo drupal_get_form('my_module_form')) on tpl.
    – Rupesh
    Commented Mar 24, 2015 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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