I feel like I've been through every tutorial on the face of the earth, and nothing anybody's suggesting seems to work. Please help me -- I just need to create a node, and set a field to a value.

This does not work:
$newNode->field_myfield[0]['value'] = 50;

Neither does this:
$newNode->field_myfield['und'][0]['value'] = 50;

Or this:
$newNode->field_myfield = 50;

Thanks in advance

Comments

dinakaran.ilango’s picture

what is your code look like .normally the 2nd way you suggested must work. well it worked for me.. my code will look something like this

  $newnode = new stdClass();
  $newnode->title = 'test';
  $newnode->body = 'whatever php logic you like';
  $newnode->field_myfield['und'][0]['value']= 'value';
  global $user;

  $newnode->uid = $user->uid;
  $newnode->name = $user->name;
  $newnode->type = 'page';
  $newNode->format = 3;     // 1 means filtered html, 2 means full html, 3 is php
  $newnode->status = 1;     // 1 means published
  $newnode->promote = 0;
  $newnode = node_submit( $newnode );
  node_save( $newnode ); 
jag339’s picture

Thanks very much.
This was the key line right here: $newnode = node_submit( $newnode );
What I was doing incorrectly was: doing a node_submit without assigning it to $newnode.
Thanks again

dman’s picture

Method2 looks OK for Drupal7 - I've been doing this a LOT. Method 1 was the equivalent in D6. Method3 can work for some non-field values like title.

But some field strorage methods may be a little different - setting ['value'] should work for normal text and numbers (though not nodereferences & terms) (and please don't try to understand how dates are stored)

The trick is:
* install devel.module
* make a page by hand that has the values you want set
* use the 'devel' tab to view the actual structure of a working node
* do what you need to in your own code to replicate that.
* node_save($node)

If that's not working, there may be some other process in your system that's getting in the way.

jag339’s picture

Neat trick. Thanks for opening my eyes to devel; that'll help a lot!
And thanks for letting me know what each of the methods I used does. I appreciate that.

jag339’s picture

I'm going absolutely crazy on this. I have a form with a "long text and summary" field. I have my own submit handler (as above). And I'm trying to capture the value of the field from the form. I did a display message on $form, and it looks like this should work, but it doesn't! Could somebody tell what's wrong?

<?php
function mymodule_submit_handler($form, &$form_state)
{
// create node, etc, etc...

  // If I set the field to a literal it works:
  $newnode->field_explanation['und'][0]['value'] = 'Lorem ipsum dolor sit amet';

  // But if I try to get the value from the form, the method for getting the value isn't correct somehow.
  // It says
  // Notice: Undefined index: field_explanation in mymodule_submit_handler()
  $newnode->field_explanation['und'][0]['value'] = $form_state['field_explanation']['und'][0]['value'];
}

?>
da_solver’s picture

Hi,
I'm confused. From your code, you have an issue with the $form_state structure, not the $form structure?

What does this mean?

I did a display message on $form

Have you inspected the structure and values in $form_state or not? I really can't tell from your post :)

Here's an example use of the $form_state structure in a Drupal form API submit handler:
http://api.drupal.org/api/drupal/developer!example.profile/function/example_form_submit/7. Note the last line.

You may also find the "Examples for Developers" useful : http://drupal.org/project/examples -- form_example

Hope that helps.

jag339’s picture

Thank you da_solver. When I say "I did a display message", I mean I inspected the element. When the form displays, I say:

dsm($form);

Now, if you drill down the tree and look for the variable value, it is: $form['field_explanation']['und'][0]['#default_value'].
This is strange, since a regular 'text' field is $form['field_text']['und'][0]['value']['#default_value'].

Anyway. Now I click my submit button. I call my submit handler, do a few things, and create a new node. When I try to set the node value with the value from the form it fails (with the error shown above).

The link you provided to example_form_submit($form, &$form_state) -- I don't understand. Why can't I just get the form value from the $form passed into the submit handler? (This of course is the main issue here.)

The form example from "Examples for Developers" looks OK but it still doesn't help me understand how to get this particular variable from that form that I just submitted.

So. To recap. User fills out a form. They click a custom submit. I call a custom submit handler. I create the node (instead of letting Drupal handle creation) so that I can redirect the user afterwards. In the custom submit handler, somehow I cannot get the value of the field they just entered in the form. How on earth am I supposed to do it?

Thanks!

da_solver’s picture

Hi,
Here is the main API documentation for Drupal's form api (FAPI) : http://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7. Note these paragraphs (I added emphasis):

The $form argument to form-related functions is a structured array containing the elements and properties of the form. For information on the array components and format, and more detailed explanations of the Form API workflow, see the Form API reference and the Form API documentation section. In addition, there is a set of Form API tutorials in the Form Example Tutorial which provide basics all the way up through multistep forms.

In the form builder, validation, submission, and other form functions, $form_state is the primary influence on the processing of the form and is passed by reference to most functions, so they use it to communicate with the form system and each other.

See drupal_build_form() for documentation of $form_state keys.

So let's look the documentation for drupal_build_form(). Again, I'm adding emphasis: http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_build_form/7

values: An associative array of values submitted to the form. The validation functions and submit functions use this array for nearly all their decision making. (Note that #tree determines whether the values are a flat array or an array whose structure parallels the $form array.)

To summarize, $form contains structure, $form_state contains data. Thus, the data keyed in to the form (by the end user) is stored in $form_state['values'] not the $form (I.E. buttons, titles, etc).

Hope that helps.
Good Luck :)

jag339’s picture

I wish there were a "Like" button here. That was very helpful. Thank you very much.

da_solver’s picture

Hi,
No problem :) I'm really glad that helped !!

There is something analogous to a "Like" button. You can edit original post and either prefix or suffix the subject line with "[SOLVED]". That way folks searching this forum can find a question that has been answered :)

Now the challenge is to edit the subject line so that the "answer" matches the "question". Your call, how about something like: "Form Processing: How to navigate the Documentation" or "Form Documentation Details". You get the idea :)

I just wanted to add. When you have free time, I highly recommend checking out a full featured debugger (inspecting tool). If I hadn't ran Drupal through a source debugger, I would not have been able to know which terms to search for. There are many tools out there. I use Netbeans (PHP), but I've also used Ecplipse (and there many others that are great). The big advantage of open source is the ability to inspect the program execution with a debugger (think of it as an "inspector"). :)

Good Luck and again, glad to have helped:)

dman’s picture

For a start, please examine

$form_state['values']['fieldname']

not

$form_state['fieldname']

even better, just get devel.module and drop in

dpm(get_defined_vars());

to see what's really available

da_solver’s picture

Hi,
Better yet, use a normal full featured PHP source code debugger like XDEBUG.

Google now bundles a full featured javascript source code debugger in it's Chrome browser. You can use Chrome's javascript debugger on any javascript library.

In the same manner, why not use a full featured debugger that works on any PHP program (not just Drupal). Full featured source code debuggers been a standard tool for decades, well worth checking out:)

jag339’s picture

Thanks dman. dpm(get_defined_vars()) worked beautifully; I didn't know that. Now I don't have to dsm() each element.

So I drill down the form array:
form -> field_explanation -> und -> 0 -> #default_value
So how come, in my submit handler, when I try to access the value, I get an error from this?
$form['field_explanation']['und'][0]['#default_value']

Is there a piece of the array I am missing?

Thanks!

jag339’s picture

dsm($form_state['values']['field_explanation']); 

The output showed that to use the variable you do this:

$form_state['values']['field_explanation']['und'][0]['value'];

Thanks everyone for your help!