0

I found this code to create a node programmatically. It creates the node but the "title" field is not populated.

    $body_text = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

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

  $node->title    = 'Node Created on ' . date('c');
  $node->language = 'en';

  $node->body[$node->language][0]['value']   = $body_text;
  $node->body[$node->language][0]['summary'] = text_summary($body_text);
  $node->body[$node->language][0]['format']  = 'filtered_html';

  $path = 'content/my-lipsum-' . date('YmdHis');
  $node->path = array('alias' => $path);

  node_save($node);

The machine name of my Title is "title_field", but using this neither works:

  $node->title_field    = 'Node Created on ' . date('c');

1 Answer 1

2

Are you using https://www.drupal.org/project/title ? If that's the case you must treat it like a field:

$node->title_field[$node->language][0]['value'] = 'Node Created on ' . date('c');
2
  • Yes I am, but still not working. The node is created and I can see the the title in '/admin/content' but once I edit the node the Title field is empy, same happens with the 'body' field, both are empty.
    – chefnelone
    Jan 26, 2015 at 12:13
  • If I use this line it works: $node->title_field['und'][0]['value'] = 'Node Created Programmatically on ';
    – chefnelone
    Jan 26, 2015 at 13:26

Your Answer

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

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