I am having a problem with the saving of information to a CCK "select list" text field. I am using Drupal 5.6 and CCK 5.x-1.6-1.

I am doing the following:

I have written a module that gets external information through a RSS feed, splits that information to several parts (Country, City, Description, Title, ...) and then puts these parts in different CCK fields of newly created nodes.

Most of these parts are written to Text – Textfield CCK fields. In this case I use the following code in PHP:
$node = array('type' => 'jobs', 'iid' => $iid);
...
$values['fieldname'] [0]['value'] = "data";
...
drupal_execute('jobs_node_form', $values, $node);

In this case all works perfectly and I can see the data in the CCK -Text – Textfield- fields of the created nodes.

The part "Country" is written to a Text – Select List CCK field that has an allowed values list (all countries in the world). In this case I was not successful to store any information to this CCK field. In the created nodes, the CCK field Country is always empty. I use the same PHP code as above.

I already checked the following:

- The incoming country names are in the allowed values list and are written identically
- The problem still exits if I delete the information in the allowed values list
- The problem is only there if I use a Text – Select List CCK field. If I use a Text – Textfield CCK field instead, the country name is shown correctly in the nodes.

So the problem must be the way in which data is written to Text – Select List CCK fields. Perhaps someone can help me an give me a sample PHP code of how this is done?

Comments

Hi there,
I don't have that much experience with CCK or the Forms API but I did have a somewhat similar problem when trying to use hook_form_alter() to add a hidden field as a nodereference field value. My understanding of the reason why such things are not possible is as follows:
- when you do drupal_execute('xxx_node_form'), CCK's hook_form_alter() gets invoked
- this in turn invokes all of CCK's various hooks for processing form values, according to the field types that have been defined for that particular content type
-the 'select' field type is defined in optionwidgets.module and the widget hook defines how form values for this field are to be handled. It is expecting a value to be passed from a select field

Bottom line, I'm pretty sure you need to bypass drupal_execute('jobs_node_form', $values, $node). Maybe you can use node_save(), but not sure how that works with CCK.

Anyway, hope you get it figured out. Good luck,

Katherine

I had the same problem. Here is what worked for me:

<?php
$values
['field_tracking_id'][0]['value'] = $tracking_id;
$values['field_timestamp'][0]['value'] = $timestamp->iso8601;
$values['field_status']['key'] = $status; // use 'key' and omit array index for select list input
$node->type = 'tracking_status';
drupal_execute('tracking_status_node_form', $values, $node);
?>

It took me a while to get the correct syntax to set a nodereference field (configured to use a dropdown list) with drupal_execute. In case it's helpful to others, here's a (rough) example:

<?php
function foo_bar_form_submit ($form_id, $form_values) {
 
$node = array('type' => 'your_node_type');
 
$values = array();
 
$values['title'] = 'set title here';
 
$values['field_parent']['nids'] = $form_values['form_field_with_nid_of_desired_parent_node'];
);
drupal_execute('your_node_type_node_form', $values, $node);
?>

The important point is that you need to use nids. That is, use
$values['field_parent']['nids'] = ... ;
rather than
// $values['field_parent'][0]['nid'] = ... ; or
// $values['field_parent'][0]['value'] = ... ;

For me, just using drupal_execute, I found that I had to create the nodereference value like so:

  $node = array('type' => 'user_website_profile');
  $values = array(
    'name' => $author->name,
  );
  $values['field_refers_to_project']['nids'][$project_nid] = $project_nid;
  drupal_execute('user_website_profile_node_form', $values, $node);

In this case, the crucial bit being: $values['field_refers_to_project']['nids'][$project_nid] = $project_nid;

Figured this out by using this post (http://drupal.org/node/99837) which shows how the form submit handler receives nodereference data:

    [field_related_blog_posts] => Array
        (
            [nids] => Array
                (
                    [93] => 93
                    [7] => 7
                    [20] => 20
                )
        )

Charles

www.parkroad.co.za

Any luck using node save?

I'm trying to do it like this, and I've tried dozens of variations (field[nids][nid], field[]['value'], field[0][nid], etc)

<?php
//create a new patient-study-record node
                       
$studyRecordNode = new stdClass();
                       
$studyRecordNode->title = $nTitle . ' - ' . $patientNode->title;
                       
$studyRecordNode->field_site_reference[]['nid'] = $siteNid;
                       
$studyRecordNode->field_study_reference[]['nid'] = $studyNid;
                       
$studyRecordNode->field_status_reference[]['nid'] = 360;
                       
$studyRecordNode->field_date_enrolled[]['value'] = date('m/d/Y');
                       
$studyRecordNode->uid = 1;
                       
$studyRecordNode->type = 'patient_study_record';
                       
$studyRecordNode->status = 1;
                       
$studyRecordNode->promote = 0;
                       
$node = node_submit($studyRecordNode);
                       
node_save($node);
?>

I've used this technique for inserting other CCK node types, but the Noderefernce nodes, select lists, radio buttons, etc NEVER get imported - only CCK text fields import fine.

I'm not sure exactly what your arrays need to look like. But a great way to hunt this down is with the devel module. Try installing that, and then put something like this in your module code (replace hook_, of course, with your module name).

dpm() will show the data structure in the message area, so you can examine in detail. Try manually submitting or viewing a node, and that should trigger this code. (Sometimes this output can be huge... so you may need to increase max_allowed_packet in mysql).

<?php
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
 
dpm($node);
}
?>

actually, now that I think of it, you probably don't even need dpm() for this. That's basically what the "dev render" tab does (provided by devel module).

I too had this problem and can confirm that a select list must be referenced by the key, not the value. If you have a text select list of allowed values and only write the allowed value, not key|value pair, then the allowed value is the key and also the label. This means to access a select list of text entry programmatically, it must be done as follows:
$node->field_status['key'] = 'Transferred';

do not use
$node->field_status[0]['value'] = 'Transferred';

as this will not store (using node_submit or even just node_save)

I have a similar problem to this.

I just want to know if this should work:

$node->field_image[0]['value'] = 'http://imageurl.jpg';

I've been trying this approach with my custom module but it doesn't seem to work