I'm developing a new module which, among other things, carries out a some updates as part of the cron hook. For the last part of the cron hook, I want to create a new 'story' node which will appear on my front page and let the users know that the updates are complete.

It only happens once a week at most, so I don't care that each one will contain the same or similar text.

What is the best way of creating this new node?

Cheers,
Ian

Comments

I haven't tried this, but it

I haven't tried this, but it seems straightforward enough:

Run a db_query statement to insert a new record into the node table with all of the necessary data and then increment the node_nid field of the sequences table.

Of course, you should try this on a test server first to make sure that nothing misbehaves. You don't want to accidentally overwrite any data in the database.

There might also be a more elegant way to do this, though I didn't see one during a quick search.

First mistake avoided!

Thanks for that.

I didn't know about the nid being stored in the sequences table, so that would have been my first mistake.

I was sort of hoping that there was a neat api function that I could just pass the title and body to. Anybody know of one?

Thanks anyway.

ian
http://www.fantasyformula1.uni.cc

Probably a better way (and safer in 4.7)

Another way to create the story to node is to construct it in code then save it with node_save(). This avoids the problems of knowinf how the data for a node is stored which is import in 4.7 since it is no longer in a single table. An simple example would be

<?php
$node
= new StdClass();
$node->type = 'story';
$node->uid = 1// Or the uid you want the story to be from
$node->title = 'The title of the node';
$node->body = 'This is the story';

node_save($node);
?>

Just what I wanted!

Thanks nevets, thats just what I was looking for. I assume you can set the sticky, promoted etc. properties in the same way?

Correct, you can set other node attributes that way

Correct, you can set other node attributes that way. If not sure of the field name, look at the node table.

Import data

Because this i like DRUPAL more and more :))). I need import some data from other table to my own Drupal node type and there is way.

--
My first drupal site - http://www.enigma.sk/kope-vas-muza/ (now developing)

--
My site - Svoji.SK
My first Drupal site - http://www.enigma.sk/

but how to attach a file ?

yes it works even for me !!!!

but what metod for attaching a file ?

ie

i've an old db table with: title, body and file-attach...

how to even attach the file to the story ??

nobody click here