HI All,

I have been looking round the net for answers as to why my code isn't working properly, I appear to be following the same rules as all the node_save() examples that I have come across, but this is what happens.

I have a custom content type of an organic group (client_account) it has been set up with custom fields as well as the group_group value to set it as an OG, when I I pass the values across to pragmatically to create the node a content type is created, but only the title is populated and the checkbox to assign it as a group is left unchecked, I am pulling what little hair I have left out with this because it has been frustrating me all night, Please find the code to save the node type below, if anyone can point what I am doing wrong I would really really appreciate it!

<?php

function hook_form_submit($form, &$form_state)
{
global $user;
        $node = new stdClass();
        $node->type = 'client_account'; //assign the content type
        $node->title = $form_state['values']['groupName']; assign the title value
        $node->uid = $user->uid; //link it with the current user id
        $node->name = $user->name; //Provide the author
        $node->comment = 0; //disable comments
        $node->promote = 0; //do not promote
        $node->language = 'en'; //set language type as english
        $node->is_new = True; //it is a new new node not an update
        $node->path = array('alias' => 'accounts/' . $form_state['values']['groupName']); // Setting a node path
        node_object_prepare($node); // Set some default values.



        $node->group_group[$node->language][0]['value'] = 1; // tell drupal it is an organic group
        // The rest of the fields in the content type are actually stored as text fields now insert the custom field data:
        $node->field_company_contact[$node->language][0]['value'] = $form_state['values']['contactName'];
        $node->field_client_email[$node->language][0]['value'] = $form_state['values']['clientEmail'];
        //not implemented yet but requires a default value
        $node->body[$node->language][0]['value'] = 'set body with page values';
        //not implemented yet but requires a default value
        $node->field_company_logo[$node->language][0]['value'] = "/images/logo.png";
        $node->field_website[$node->language][0]['value'] = $form_state['values']['website'];
        $node->field_address_line1[$node->language][0]['value'] = $form_state['values']['addressLine1'];
        $node->field_address_line_2[$node->language][0]['value'] = $form_state['values']['addressLine2'];
        $node->field_address_line_2[$node->language][0]['value'] = $form_state['values']['addressLine3'];
        $node->field_region[$node->language][0]['value'] = $form_state['values']['county'];
        $node->field_post_code[$node->language][0]['value'] = $form_state['values']['postCode'];
        //create the node

         //Organic groups fields
        $node->og_description = 'A group managed by ' . $user->name;
        $node->og_register = 0;
        $node->og_directory = 1;
        $node->og_private = 1;
        $node->og_selective = 3;


        /*
         * drupal_printd($var, $title, $die=false)
         * is a function I added for on the fly debugging
         */
        drupal_printd($node, "original_node"); //display original node data
        $node = node_submit($node);
        drupal_printd($node, "node save"); //display the node data AFTER the node_submit() function
        $node = node_save($node);
        drupal_printd($node, "result"); //display the result of node_save() - not sure if this has a return value
        drupal_goto("accounts"); //node has been created go to the users Accounts page and show the new group
}

?>

Using the drupal_printd() function I can see that on the first call it is populated, in the second call it is also populated, but $node = node_save() does not return anything, I can hopefully assume that this is correct because the node IS created but the field values are not saved nor is it assigned as a group, so I MUST be doing something wrong with the field values,only I just cannot find out what!

All help is greatly appreciated.

Jim

Comments

Jaypan’s picture

Change:

[$node->language]

To:

[LANGUAGE_NONE]

That is likely your problem.

mkcphp’s picture

So I had tried a lot of things and using $node->language = LANGUAGE_NONE; had no effect but using LANGUAGE_NONE as the element changed everything

Its working!

Thanks again

Jim

Jaypan’s picture

Glad it worked for you.

nikhileshpaul’s picture

Can you please elaborate your answer. I've run into the exact same situation