Hello

I'm using the following code to create a node programmatically:


require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('inc', 'node', 'node.pages');

$node = new stdClass();

node_object_prepare($node);

$node->type = 'my_license';

$node->uid = 1;

$node->title = 'gratis';

$node->field_state[0]['value'] = 'a';

node_save($node);

The field $node->field_state is a select list text field. The value 'a' is the key for the label Approved. The node is saved succesfully into the database, but when I go to the View page, instead of the label value I see the key value. When I go to the Edit page, the select list field has no value selected.

Why can this be? When using the same code for a normal text field, everything works perfectly.

Thanks!

Comments

nevets’s picture

Are you sure 'a' is a valid key value?

matiaslezin’s picture

Yes. I've defined it when I configured the fields and checked it in the database when the node is saved.

p | Pending
r | Rejected
a | Approved

da_solver’s picture

Hi,
Can you explain how a "form" fits in to your code???

You create a new stdClass object and then start adding properties (I.E, fields), and property values

$node->type = 'my_license'; is short hand for:

  • Declare and add property "type" to object instance.
  • Populate "type' property with value of 'my_license'

So you declare an object property then populate it with a value.

Then you assert there is a form structure ??? Where ????

Where is the form? How does the form all of a sudden become part of the object instance??

If there is no "form", you don't populate a "form select element"??

If there is no "form select element", you just declare and populate the property. Why is that one property different???

matiaslezin’s picture

I use this code in a Rule.

When a user account is created, a node my_license is created on his behalf. Then I realized that the values of that node will always be modified through php and a form will never be used, so I decided to change the field to a normal text field, thus solving the issue.

Either way, I couldn't get this to work with a select list field.

da_solver’s picture

Hi,
You are welcome :) Glad to have helped.

To summarize, the code you posted does not use a "form", thus did not work.

Please consider appending the original post subject line with [SOLVED] so that other folks can find your answer.

Good Luck :)

matiaslezin’s picture

Thx!