Hi,
This must be very simple but I can't find how is should be done.
I'm new to drupal programming and i try to do my first module.
I want to use a custom content type to generate a form and when it is submitted, save the data in the database.
So my code, till now, is this :

// $Id$

/**
* Implement hook_menu().
*/
function paiement_menu() {
  $items = array();
  $items['rencontres/inscription'] = array(
    'title' => 'Inscription',
    'description' => 'Inscriptions aux rencontres.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('user_inscription_form', 1),
    'access arguments' => array('administer users'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

function user_inscription_form() {
  module_load_include('inc', 'node', 'node.pages');
  $node_form = new stdClass;
  $node_form->type = 'inscriptionrencontres';
  $node_form->language = LANGUAGE_NONE;
  $form = drupal_get_form('inscriptionrencontres_node_form', $node_form);
  $form['#submit'][] = 'paiement_submit';
  return $form;
}

function paiement_submit($form, &$form_state) {
  // And here, I would like something like
  node_save($node);
  // but I can't figure out how to get the $node out of the $form
  // a similar way I got the $form from the $node
}

With this code, my form is just as I expect but i can't figure out how I can save the data in the database (in the form_submit ?)

Thanks for your help,
PJ

Comments

jaypan’s picture

You're on the right track, you're just off a little in your implementation.

You need to do the following:
1) In your hook_menu(), create a page callback that returns a page and don't use drupal_get_form() like you are.

function paiement_menu() {
  $items = array();
  $items['rencontres/inscription'] = array(
    'title' => 'Inscription',
    'description' => 'Inscriptions aux rencontres.',
    'page callback' => 'my_page_callback',
    'access arguments' => array('administer users'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

2) In your page callback, you can use the function node_add(), which will do everything you were doing manually:

function my_page_callback()
{
  module_load_include('inc', 'node', 'node.pages');
  return node_add('inscriptionrencontres');
}

You then add your submit function in hook_form_alter() (you cannot add it after generating the form like you were trying)

function paiement_form_alter(&$form, &$form_state, $form_id)
{
  if($form_id == 'inscriptionrencontres_node_form')
  {
    $form['buttons']['submit']['#submit'][] = 'paiement_submit';
  }
}

And in your submit function, if I recall correctly the node will be located at $form['#node'].

function paiement_submit($form, &$form_state)
{
  $node = $form['#node'];
}

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

PushingJoe’s picture

Everything seems to be ok until the form is displayed.
But when I submit it, i have the same result as i had before (but in less lines :-) )
So, my code is :

// $Id$

/**
* Implement hook_menu().
*/
function paiement_menu() {
  $items = array();
  $items['rencontres/inscription'] = array(
    'title' => 'Inscription',
    'description' => 'Inscriptions aux rencontres.',
    'page callback' => 'user_inscription_form',
    'access arguments' => array('administer users'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

function paiement_form_alter(&$form, &$form_state, $form_id)
{
  watchdog("paiement", "paiement_form_alter");
  if($form_id == 'inscriptionrencontres_node_form')
  {
    $form['buttons']['submit']['#submit'][] = 'paiement_submit';
  }
}

function user_inscription_form() {
  watchdog("paiement", "user_inscription_form");
  module_load_include('inc', 'node', 'node.pages');
  return node_add('inscriptionrencontres');
}

function paiement_submit($form, &$form_state)
{
  watchdog("paiement", "paiement_submit");
  //$node = $form['#node'];
  print_r("<pre>", false);
  print_r("---------------------------------------------------------------------------------\n", false);
  print_r($form, false);
  print_r("---------------------------------------------------------------------------------\n", false);
  print_r("</pre>", false);
}

I put watchdogs and have no entry for paiement_form_alter and paiement_submit.
Instead, I have

Notice: Undefined index: node in node_form_submit_build_node() (line 509 of /var/www/html/modules/node/node.pages.inc).

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7929 of /var/www/html/includes/common.inc).
jaypan’s picture

Is hook_form_alter() in the .module file? And did you clear the cache after adding it?

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

PushingJoe’s picture

No, I didn't clear the cache. And now it works fine. \o/
Thank you very much.
Drupal seems to be very powerful but the firsts steps are not obvious (I should go migrate to 8 quickly :-)

PushingJoe’s picture

In fact, i don't even need the hook_alter_form and the submit_callback... I only need 10 lines of code. Nice API.

PushingJoe’s picture

Thank you, i'll try this as soon as i can...