0

I have created a custom form in my module so that when the user clicks submit, some changes are performed in the databases and other actions follow. The form is an empty form with only the submit button nothing else. When the user clicks the Submit button, a function is called to perform some tasks. However, I am getting some errors. Here's the code:

My module folder is formtest and is placed in sites/all/modules/

formtest.info

name = Test Form
description = Accept Challenge Form Submit
core = 7.x

formtest.module

function formtest_menu() {
$items['formtest'] = array(
'title' => 'Accept Challenge',
'page callback' => 'drupal_get_form',
'page arguments' => array('formtest_form'),
'access callback' => TRUE,
);
return $items;
}

function formtest_form($form,&$form_state) {
  $form['button'] = array(
  '#type' => 'submit',
  '#value' => 'Accept Challenge',
  '#submit' => array('formtest_action'),
   );
  return $form;
}

function formtest_action($form,&$form_state) {
  $query = db_insert('challenges')
    ->fields(array(
    'cid' => '$node->nid',
    'uid' => '$user->uid'
    ))
   ->execute();
}

/** * To display the form on a particular content type */

function formtest_node_view($node, $view_mode = 'full', $langcode = NULL) {
      if($view_mode == 'full') {
        if($node->type == 'challenge') {
          $node->content['my_form'] = array(
            '#markup' => drupal_get_form('formtest'),
            '#weight' => 1,
         );
     }
   }
  }

Well that's all. There's no form showing up..no error messages..nothing...I tried a var_dump() but nothing...That custom content page is displaying as it was but there seems to be no effect of this code. Would appreciate your help in any manner. Thanks

2 Answers 2

0

When I make custom forms I first register them as blocks.

function mymodule_block_info() {
  $blocks['review_membership'] = array(
    'info' => t('Membership'),
  );
  return $blocks;
}

Implement the view of the block

function mymodule_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'review_membership':
        $node = menu_get_object();
        $block['subject'] = t('Membership');
        $form = drupal_get_form('mymodule_review_membership_form');
        $block['content'] = drupal_render($form);
        break;
  return $block;
}

Then I have the body of the form

function mymodule_review_membership_form(){

    $form = array();
    $node = menu_get_object();
    $preverified = $node->field_mng_accepted['und'][0]['value'];

    $form['accept'] = array(
        '#type' => 'submit',
        '#value' => t('Accept'),
    );
    $form['reject'] = array(
        '#type' => 'submit',
        '#value' => t('Reject'),
    );
    if(!$preverified){
        $form['reason'] = array(
            '#type' => 'textarea',
            '#title' => t('Reject reason'),
            '#default_value' => NULL,
            );
    }

    return $form;
}

and the reaction on the form submit (the name is automatic based on the name of form)

function mymodule_review_membership_form_submit($form,&$form_state){
.....

Once all is defined I just add the block wherever I want

2
  • this looks promising...let me try this out..just one question here..how do i make a block to appear only on a particular content type nodes
    – why
    Commented Feb 7, 2013 at 12:19
  • I would use context. Condition on an address or node type. And with reactions/blocks you add the block to a chosen part of your website
    – Pawel G
    Commented Feb 7, 2013 at 13:54
0

drupal_get_form doesn't return a markup as such. it returns an array of the form structure. to get the html out put you should run the drupal_render function.

something like drupal_render(drupal_get_form('formtest');

1
  • I did this: '#markup' => drupal_render( drupal_get_form('formtest')), but then i get few errors such as : Notice: Undefined index: formtest in drupal_retrieve_form() (line 763 of /opt/lampp/htdocs/includes/form.inc). Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'formtest' not found or invalid function name in drupal_retrieve_form() (line 798 of /opt/lampp/htdocs/includes/form.inc). Strict warning: Only variables should be passed by reference in formtest_node_view() ...
    – why
    Commented Feb 7, 2013 at 11:25

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.