0

I need to write a module that will fetch remote content via an API and store it as nodes. These nodes have custom fields defined within the module. I have already accomplished this by modifying the Node Example Module.

I can add nodes via the admin end with all the custom fields, but when I'm trying to programatically create the nodes from hook_install(), the body and custom fields are not saving.

The code for creating the fields is largely the same as the example I linked. Here's the function i'm using to create the nodes:

function sm_add_mashup_item_node($remoteId, $site, $text, $image, $time){

    $node = new stdClass();
    $node->type = 'rscg_mashup';
    node_object_prepare($node);

    $node->title = "My Awesome Title";
    $node->language = LANGUAGE_NONE;
    $node->created   = time();
    $node->timestamp = time();
    $node->changed   = time();

    $node->status = 1;
    //$node->uid = 1;
    $node->promote = 1;
    $node->sticky = 0;
    $node->format = 3;
    $node->language = 'en';
    $node->revision = 0;


    $node->body[$node->language][0]['format'] = 3;
    $node->body[$node->language][0]['summary'] = 'summary';
    $node->body[$node->language][0]['value'] = 'dfg sdgdfs gsdg sdfg sdfg sdfg sdfg sdfg sdfg sdgsdf';

    // Try to set your custom field
    $node->rscg_mashup_image_url[$node->language][0]['value'] = "Too many to count...";

    echo "\n Edited: \n";
    print_r(node_load(526));

    echo "\n Pre-Save: \n";
    print_r($node);


    $node = node_submit( $node );
    node_save($node);

    echo "\n Loaded: \n";
    print_r(node_load($node->nid));

    return;

I've tried countless variations of the above.

Here is the debug output from the function:

https://gist.github.com/agroff/1c2c22920c20e3775b0d

This is a screenshot of the field list.

screenshot

  • OK, what exactly happens? You tried, good, but what is wrong? What happens? And can you show a comparison of node object loaded from existing node, and object you are creating? – Mołot Dec 5 '14 at 2:01
  • @Mołot, Thanks for reading my question. I have updated the function to include debug info and linked to a gist of that debug info. The issue is that manually edited nodes properly store and display the body and custom fields, while programmaticly created ones do not. I have no idea what is wrong. There are no errors or anything. – Andy Groff Dec 5 '14 at 7:09
  • Have you tried saving the node without custom fields filled in? Maybe the problem lies somewhere else. – laniol Dec 5 '14 at 8:41
  • Disable your module, uninstall it and then install it again. – laniol Dec 5 '14 at 8:51
1

try using like below

$node->body[LANGUAGE_NONE][0]['format'] = 3;
    $node->body[LANGUAGE_NONE][0]['summary'] = 'summary';
    $node->body[LANGUAGE_NONE][0]['value'] = 'dfg sdgdfs gsdg sdfg sdfg sdfg sdfg sdfg sdfg sdgsdf';

// Try to set your custom field
    $node->rscg_mashup_image_url[LANGUAGE_NONE][0]['value'] = "Too many to count...";
  • 1
    Hmm, I can't see how this is different from what I'm doing but I'll give it a shot anyways. At this point I'll try anything. – Andy Groff Dec 5 '14 at 8:35
  • The difference is that you are setting $node->language = LANGUAGE_NONE; and then $node->language = 'en' which overwrites the previous :). So ideally when you set custom field use LANGUAGE_NONE directly and your $node->language still can be 'en' – Nish Dec 5 '14 at 15:46
  • Ahh, I didn't even realize I was overwriting it. That wasn't my intention at all. Let me fix. If this works i'll be so stoked. – Andy Groff Dec 5 '14 at 17:14
  • I'm not positive if this was exactly what fixed it, but it seems to at least be a contributing factor. Answer accepted and thanks for your help. – Andy Groff Dec 9 '14 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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