Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Last active January 2, 2016 02:39
Show Gist options
  • Save cesarmiquel/8238891 to your computer and use it in GitHub Desktop.
Save cesarmiquel/8238891 to your computer and use it in GitHub Desktop.
Create a Drupal node with path
<?php
// Create a node by code
$newNode = new stdClass();
$newNode->type = 'page';
node_object_prepare($newNode);
$newNode->title = 'This is the title of the new node';
$newNode->status = 1;
$newNode->comment = 1;
$newNode->language = LANGUAGE_NONE;
$newNode->uid = 1;
node_save($newNode);
// Add content for the node
$newNode->body[LANGUAGE_NONE][0]["value"] = '<p>Here you can put HTML code for your node.</p>';
$newNode->body[LANGUAGE_NONE][0]["format"] = 'full_html';
// Add image
$filename = 'gogh.white-house.jpg';
$image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_image[LANGUAGE_NONE][0] = (array) $file;
// Save node
node_save($newNode);
// Add path
$path = array(
'source' => "node/" . $newNode->nid,
'alias' => 'alias-for-my-node',
);
path_save($path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment