0

I am struggling in my attempts to programmatically display a form to display in my node-page view area. I have the following code in my "simplemodule".

function simplemodule_newcomer_form($form_state){
    $form   =   array();
    $form['simplemodule_newcomer']['name']  =   array(
        '#type'         =>  'textfield',
        '#title'        =>  t('name'),
        '#description'      =>  t(''),
        '#weight'       =>  -1,
    );
    $form['simplemodule_newcomer']['email'] =   array(
        '#title'        =>  t('email'),
        '#type'         =>  'textfield',
        '#description'      =>  t(''),
        '#weight'       =>  0,
    );
    $form['simplemodule_newcomer']['phone'] =   array(
        '#title'        =>  t('telephone No.'),
        '#type'         =>  'textfield',
        '#description'      =>  t(''),
        '#weight'       =>  0,
    );
    $form['submit_button'] =    array(
        '#type' =>  'submit',
        '#value'    =>  'enter',
    );
    return $form;
}

function simplemodule_newcomer_form_submit($form_id, &$form_state){
    //dealing with submitted data
}

This code works but only from a defined link in my administration menu.

What I want to do is get the form to display and submit on a specific node in view mode. So it creates the effect that there is a form to fill when visiting the node.

2 Answers 2

2

You can implement hook_nodeapi() and attach your form using drupal_get_form():

function simplemodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  if ($node->nid == $the_nid && $op == 'view') {
    $node->content['my_additional_field'] = array(
      '#value' => drupal_get_form('simplemodule_newcomer_form'), 
      '#weight' => 10,
    );
  }

}

You can use the #weight key to specify where in relation to the other content on the page your form will appear. Also you'll need to clear Drupal's caches when you implement this hook to make sure it's picked up.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping out a newbie :-)
1

Surely you can use

hook_form_alter(&$form, &$form_state, $form_id) and 

hook_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)

http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_alter/7

http://drupal.org/node/1011692

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.