By jag339 on
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
need more details
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
Thanks very much. This was
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
Method2 looks OK for Drupal7
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.
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Neat trick. Thanks for
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.
One last piece of the puzzle
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?
Please Elaborate
Hi,
I'm confused. From your code, you have an issue with the $form_state structure, not the $form structure?
What does this mean?
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.
Thank you da_solver. When I
Thank you da_solver. When I say "I did a display message", I mean I inspected the element. When the form displays, I say:
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!
Data versus Structure
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):
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
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 :)
Thanks!
I wish there were a "Like" button here. That was very helpful. Thank you very much.
No Problem
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:)
['values']
For a start, please examine
not
even better, just get devel.module and drop in
to see what's really available
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Better yet use a standard debugger
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:)
Still not there...
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!
It finally worked
The output showed that to use the variable you do this:
Thanks everyone for your help!