4

I need to create nodes from shell script at my server (some server statistics).

As I know drush cannot add new node. (It was a discovery for me)

So I think make shell script which will create CSV file with node fields and make periodical import by Feeds.

Is there more smart solution?

3 Answers 3

7

If you are going to start scripting on the command line, you can always script a php script and run it via drush. That way, you have access to your Drupal system and data. For example, if you place the following code in a file, e.g. script_create_node.php:

<?php

// Expect title as $args[1] and body as $args[2]
if( !isset($args[1]) || trim($args[1]) == '' || !isset($args[2]) || trim($args[2]) == '') {
  print "Expecting two arguments on the command line, title and body, for the new article\n";
  exit;
}

$node = new stdClass();  // Create a new node object
$node->type = 'article';  // Content type
$node->language = LANGUAGE_NONE;  // Or e.g. 'en' if locale is enabled
node_object_prepare($node);  //Set some default values

$node->title = $args[1];
$node->body[$node->language][0]['value'] = $args[2];

$node->status = 1;   // (1 or 0): published or unpublished
$node->promote = 0;  // (1 or 0): promoted to front page or not
$node->sticky = 0;  // (1 or 0): sticky at top of lists or not
$node->comment = 1;  // 2 = comments open, 1 = comments closed, 0 = comments hidden
// Add author of the node
$node->uid = 1;

// Save the node
node_save($node);

you can always call this on the terminal like this:

drush scr script_create_node.php "Article Title" "Article Body"

You can extend such a script to include the logic you would use in your shell script, but instead of writing to a CSV file, you can write directly to the database. It's a great advantage having both PHP-CLI and a bootstrapped Drupal available at the same time.

Hope this helps some!

P.S: Lots of pages out there answering the question "programmatically creating nodes", one random pick would be http://www.group42.ca/creating_and_updating_nodes_programmatically_in_drupal_7

1
  • So Drush can add nodes (and much more) with drush scr! Thanks. It will take some time to test before I accept answer.
    – Dimetry
    Mar 20, 2016 at 11:07
1

There are a lot of right answers here; using feeds would work fine. You might also be interested to try Drush Entity Commands.

1
  • Drush Entitiy Commands are not regarded as save and D7 only
    – yunzen
    Aug 21, 2020 at 9:24
0

Don't forget drush genc if you have the devel module installed. Old school but effective, creates fully populated nodes with randomly generated images etc. (You can also use it to delete nodes as well: drush genc --kill --types=NODE_TYPE 0 0)

> drush genc --help
Create content.

Arguments:
 number_nodes                              Number of nodes to generate.
 maximum_comments                          Maximum number of comments to generate.

Options:
 --feedback                                An integer representing interval for insertion rate logging. Defaults to 1000
 --kill                                    Delete all content before generating new content.
 --languages                               A comma-separated list of language codes
 --skip-fields                             A comma delimited list of fields to omit when generating random values
 --types                                   A comma delimited list of content types to create. Defaults to page,article.

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.