0

i am using FAPI to create nodes through a custom form. the code below creates the nodes and assign them with a title and a body, so if i wanted static field values everything was perfect. my problem is, i want the value of $body_text to be determined by the user's input in the "order" textfield. i tried doing something like -

$body_text = $form['name'];

but that didn't work.

this is the code i'm using -

<?php
function nodeform_menu() {
  $items = array();
  $items['nodeform/form'] = array(
    'title' => t('My form'),
    'page callback' => 'nodeform_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function nodeform_form() {
  return drupal_get_form('nodeform_my_form');

}

function nodeform_my_form($form, &$form_state) {

  $form['box'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="box">',
    '#suffix' => '</div>',
    '#markup' => '<h1>Initial markup for box</h1>',
  );

    $form['order'] = array(
    '#type' => 'textfield',
    '#title' => t('My order'),
    '#default_value' => 'Products',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#ajax' => array(
      'callback' => 'ajax_example_submit_driven_callback',
      'wrapper' => 'box',
      'name' => 'submit1',
    ),
    '#value' => t('Submit'),
  );



$body_text = $form['order'];
$node = new StdClass();
$node->type = 'page';
$node->status = 1;
$node->title = "name";
  $node->language = LANGUAGE_NONE;
  $node->body[$node->language][0]['value']   = $body_text; 
  $node->language = LANGUAGE_NONE;

node_save($node);
  return $form;
}

can anyone help me understand how to do this? i tried googling and reading the FAPI guide's but couldn't figure it out... plus, this is the first time i am using FAPI so in-spite the code is working, if anyone have any insights on this code (such as coding it more properly) i would also very appreciate that.

migrated from stackoverflow.com Dec 20 '12 at 17:22

This question came from our site for professional and enthusiast programmers.

0

write:

print_r($_POST);
exit();

somewhere in that script and copy/paste the values here.

  • put it closer to the end of the lifecycle on the page. it didn't "break" the drupal page, it's just helping you debug it. the blank page with Array() means there's nothing in your post Array print_r($post) will print out all of the post variables that come back from a form. maybe you only want to conditionally call that function if you've posted something. – rnirnber Dec 12 '12 at 18:34
  • unfortunately it's giving me the same blank array() no matter where i put it in the file... when i go on the form page and check it out with Devel module, it also shows Array() without any values.. am i doing something wrong? i'm new to drupal and it's fapi as i said. very appreciate your help! – BLV Dec 12 '12 at 19:18
  • ALSO IF YOU COULD GIVE ME SOME POINTER'S ON HOW YOU THINK THIS SHOULD BE DONE, SO I COULD GOOGLE MYSELF AND LEARN HOW TO TAKE THE ARRAY VALUE AFTER THE USER SUBMIT, AND PUT IT AS THE NODE FIELD VALUE... THANKS A LOT! – BLV Dec 12 '12 at 20:25
  • node_save($node); – rnirnber Dec 12 '12 at 20:46
  • 1
    thanks... print_r($node) did the job and gave me the available field list, but that wasen't my problem... check out my answer above... anyhow - thanks a lot! your answers sent me to google some and understand how FAPI works... – BLV Dec 13 '12 at 20:22
0

You should put node creating into form submit function. In your case it should be nodeform_my_form_submit. You can read more about it here under "Submitting Forms". Btw, you can use drupal_get_form as page callback function and with that skip using separate function.

0

the problem was i tried accessing $form['name']; array value the wrong way... instead of $body_text = $form['name']; , it should be done this way -

declare the array's value as the $body_text variable -

$body_text = $form_state['values']['order'];

then, set it as the body field content -

  $node->body[$node->language][0]['value']   = $body_text;

plus, i had to add _state to $form in order to fetch the value... Form API is really useful once you get the hang of it... thanks a lot everyone!

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.