1

I've written a simple script that iterates over a CSV file and creates nodes in Drupal. All of the nodes are present in the admin interface but do not appear on the site unless I manually click 'Edit' then immediately save. This is a real problem because I do not have time to open each and every node. Funny thing is, this doesnt happen for every node.

A simplified version of my code

 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {              
    $title = $data[1];
    $address1 = $data[2];
    $address2 = $data[3];

    // Add a node
    $node = new stdClass();
    $node->type = 'store_location';
    node_object_prepare($node);
    $node->status = 1; # published or not...
    $node->promote = 0;
    $node->uid = 1; // ID of author
    $node->created = date('U', strtotime('now'));

    $node->language = LANGUAGE_NONE;
    $node->path['pathauto'] = TRUE;
    $node->title = $title;
    $node->gsl_addressfield['und'][0]['thoroughfare'] = ucwords($address1);
    $node->gsl_addressfield['und'][0]['premise'] = ucwords($address2);

    node_save($node);
}
11
  • 2
    Not sure it's the root of your problem, but $node->created should be a UNIX timestamp, not a formatted date
    – Clive
    Oct 22, 2013 at 10:08
  • Test if cache clear makes your nodes appear. I f neither this nor Clive's suggestion works, it's an interesting problem. Btw they "do not appear" - I guess it's on a lists and in menus? What about direct /node/nid links?
    – Mołot
    Oct 22, 2013 at 10:16
  • have you tried the feeds module it can import csv pretty well. Oct 22, 2013 at 10:18
  • 1
    date('U', ...) returns string, strtotime() returns int. So yes and no, both returns timestamp, but their return values are ==, not ===. @Clive can you tell if it can make a difference?
    – Mołot
    Oct 22, 2013 at 11:45
  • 1
    @Mołot Nah, this is PHP/PDO - it ain't that picky ;)
    – Clive
    Oct 22, 2013 at 11:52

1 Answer 1

1

Use hook_node_presave($node) to check whether you are getting proper values.

Also, try drupal_form_submit() instead of node_save() for edit

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.