I haven't been able to find information on how to programmaticly create a node in Drupal 7. Migrate module lookd like it can do it, but it is a bit overkill to learn just for this.
I have a Drupal 6 blog, and want to upgrade it to Drupal 7. Unfortunately, updating is not working, and I refuse to try updating again until the update path is more clearly defined with a beta release of D7.
So, instead I:
1) Set up fresh install of D7
2) Turn on blog module
3) Export node and node_revision SQL tables from D6 blog
4) Clean up node SQL data with some regex
5) Insert the SQL data into array (e.g., $node[0]['title'] = 'Why I like Drupal'; $node[1]['title'] = 'Drupal love';
6) Run the below to import into D7 (imagine about 60 nodes):
<?php
main();
function main() {
$nodes = array();
$nodes[1]['title'] = 'First post';
$nodes[1]['body'] = '<p>A node</p><p>A node which is a blog post with lots of stuff</p>';
$nodes[1]['teaser'] = '<p>A node</p>';
$nodes[1]['timestamp'] = 1281765101;
$nodes[1]['format'] = 2;
// about 65 more nodes that I don't want to paste here
make_nodes($nodes);
}
function make_nodes($nodes) {
foreach($nodes as $new_node) {
$node = new stdClass();
$node->type = 'blog';
$node->status = 1;
$node->uid = 1;
$node->title = $new_node['title'];
$node->promote = 1;
$node->created = $new_node['timestamp'];
$node->timestamp = $new_node['timestamp'];
$node->sticky = 0;
$node->format = 3;
$node->language = 'en';
$node->teaser = $new_node['teaser'];
$node->body = $new_node['body'];
$node->revision = 0;
node_save($node);
}
}
The nodes get created, but without the field data from the body. I'm guessing there is some magic I need to do with the new field API, like using field_attach or perhaps setting some field or entity value in the node object, but so far I haven't found any information on this on d.o. Any suggestions or able to point me to the right page on how to programmatically create nodes in D7?
Side note: I can get it to work if I manually insert the data with a script into field_data_body and field_revision_body tables, but this is neither ideal nor the correct way to be doing things.
Comments
Changes in Drupal 7
Hi,
You are really close. You just need a couple changes.
Note! I tested this for the Drupal 7 Alpha 6 release. Therefore, the following simply gets you through the work around you've described in your post. This a not a comprehensive migration example (for folks reading this after a Drupal 7 production release).
First, add a "changed" attribute to your node structure:
Next, "body" is removed from the default node structure/instance in Drupal 7 (as you implied). Thus, change:
to
With those changes I was able to generate blog posts programmatically
(i.e. made same call, per your post) .
Hope that helps.
how to attach image with the node and tags
HI. i am working on a project to import xml contents into drupal 7. i have parsed all the data in php. so far i have been succeded in importing node body and its title. There is no documentation for drupal 7 on how to attach image to the node and tags. i really need help i have spend two days finding it tried a lot. i will be very thank you for someone. please just guide me somewhere
function make_nodes($nodes)
{
$new_node = $nodes[0];
$node = new stdClass();
$node->title = $new_node['title'];
$node->body['und'][0]['value'] = $new_node['body'];
$node->type = 'article';
$node->created = $new_node['timestamp'];
$node->changed= $new_node['timestamp'];
$node->status = 1;
$node->promote = 1;
$node->sticky = 0;
$node->body['und'][0]['format'] = 1;
$node->uid = (isset($local_user->uid) && !empty($local_user->uid)?$local_user->uid:1);
$node->language = 'en';
$node->timestamp = $new_node['timestamp'];
$node->revision = 0;
node_submit($node);
node_save($node);
}
$node param in node_submit() is not passed by reference
In D7, when you use node_submit(), you should get the returned value before passing it to node_save(). So in the above code.. it should be like so:
$node = node_submit($node);
node_save($node);
Thanks much, this did indeed
Thanks much, this did indeed solve the problem. Though I changed 'und' to 'en' since I had English enabled as the language type. I'm working on a writeup of this for anyone else who wants to go through this process before a proper migration path is available.
adding image fields to node in D7
This works with D7 beta 2 - it needs refining though!
I've exported my nodes using views bonus as XML with simpleXML (hence the casting as string) and already got the images in sites/default/files - look out for D7's file stream i.e. public:// & private:// - and re-imported it using the code above plus:
Anyone got a more elegant way?
http://modelagnostic.co.uk/
Thanks
Saved me after hours of searching!! Works like a charm.
Here's my take on
Here's my take on it
The downside of this approach is that really big files can cause out of memory errors as they are read entirely into memory during the execution of the above code.
Drupal 7 node save with Image field
Here's an example that uses minimal code by prefilling node and image file field values with the help of
node_object_prepare()
andfile_copy()
. This is currently used to import nodes with an image.<?php//
Saving to file field requires 2 extra variables
In order to save a file to a file field you need to extra variables compared to the above example of saving to the image field
The code will look something like this:
Add term references
Hi guys I have a similar problem...
I have managed to add nodes programmaticaly but I can not reference the taxonomy terms that I want selected....
I have tried this
$node->taxonomy[4] = 12;
$node->taxonomy[8] = 30;
$node->taxonomy[5] = 20;
and this
array(4=>array(12),8=>array(30),5=>array(20));
but they don't seem to work...
any ideas?
Thanx
Hey, this code works for
Hey,
this code works for me:
Source: http://fooninja.net/2011/04/13/guide-to-programmatic-node-creation-in-dr...
What i can't manage so far is adding a date-field. (I want to add events).
Any ideas?
A writeup about this
Have you finished/posted this writeup, nowarninglabel?
Well, not exactly, but you
Well, not exactly, but you can read what I did write here: http://www.coderintherye.com/node-export-import-d6-d7
Cheers.
Similar difficulty with the body field
And frankly I'm confused. I'm trying to create four nodes, one for the "page" content type, and one for the "alert" content type.
The Pages are created without any problems:
...but the Alert is created with an empty body field:
Both content types are using the default body field, and UID 1 is permitted to use the raw_html text format. Any thoughts?
Perhaps try also setting the
Perhaps try also setting the 'safe_value' and you could try directly with the language, e.g.,
$node->body['und'][0]['value'] = 'hello';
$node->body['und'][0]['safe_value'] = check_plain('hello');
One thing to double check
Make sure that the body field really is body and not something like field_body.
The body field is no longer guaranteed to be there in d7.
Ditto
I cant get the body to save. I've tried all the code from this thread as well as many other threads on node creation.
Entity Metadata Wrappers
I found that this worked for me:
However in order to use Entity Metadata Wrappers you do need to have the Entity API module installed.
drupal_form_submit()
I'll recommend you to use drupal_form_submit() function for programmatically node creation.
Only this function fires all hooks.
Here you can find an example:
http://d.danylevskyi.com/node/6
In theory I agree with you
In theory I agree with you however in practice using files doesn't work and ends up always with a warning: "Managed file: the file used in the field may not be referenced", and no nodes are created.
All other examples I found on the web uses node_save(), I would be very happy if you could explain us how to make it work with drupal_form_submit
Thanks in advance
Fredrik Lahode
Développeur et Formateur web indépendant