2

I have created content type named "site_profile", and form named "add_website_form". The content type includes cck fields and one of the fields is "field_operators" which is select box field. I have hooked options of the field to display variable that contains array of specific users with some mechanism. The hook_form_alter is:

function lcm_form_site_profile_node_form_alter(&$form, &$form_state, $form_id) {
    $relevant_users = lcm_get_relevant_users();
    $form['field_operators']['und']['#options'] = $relevant_users;
}  

Now I am trying to get functionality that stores data into the node through node_save() function from the "add_website_form" (costume form).

the code that supposed to insert the data is:

function lcm_add_website_form_submit($form, &$form_state){
    $website_name = $form_state['values']['website_name'];
    $url = $form_state['values']['url'];
    $operators_for_site = $form_state['values']['operators_for_site'];
    lcm_add_website_form_insert_to_db($website_name, $url, $operators_for_site);
}
function lcm_add_website_form_insert_to_db($website_name, $url, $operators_for_site){
    // Create a node object, and add node properties.
    $new_website = new stdClass();

    $new_website->type = 'site_profile';
    $new_website->title = $website_name;
    $new_website->uid = $GLOBALS['user']->uid;
    $new_website->field_url['und'][0]['value'] = $url;
    $new_website->field_operators['und']['value'] = $operators_for_site;
    $new_website->created = strtotime("now");
    $new_website->changed = strtotime("now");
    $new_website->status = 1;
    $new_website->comment = 0;
    $new_website->promote = 0;
    $new_website->moderate = 0;
    $new_website->sticky = 0;
    $new_website->language = 'und';

    node_object_prepare($new_website);
    // Save the node.
    node_save($new_website);
}

the problem is that the data that supposed to save on field_operators doesn't save... Any ideas?

1 Answer 1

0

Ok, new answer, replace:

$new_website->field_operators['und']['value'] = $operators_for_site;

with this

$new_website->field_operators['und'][0]['value'] = $operators_for_site;
1
  • Ok, I have changed that and got an sql error because i should insert the data into: '$new_website->field_operators['und'][0]['field_operators_value']', after change to the above line it worked but the problem now is that the form_state returns an empty array for the $operators_for_site: ' ["operators_for_site"]=> array(3) { [0]=> string(1) "0" [1]=> int(0) [2]=> int(0)'
    – Dori
    Commented Sep 23, 2013 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.