4

I have already found Drupal 6 documentation on how to programmatically create a page node.
How is it possible to achieve that in Drupal 7?


I've changed my strategy, and I'm using another approach, but I'm getting the following error:

Fatal error: Cannot access empty property in C:\...\...\...\...\modules\field\field.attach.inc on line 198

I'm using the following code:

$form_state = array();
module_load_include('inc', 'node', 'node.pages');

$node = array('type' => 'page');
$form_state['values']['uid'] = 1;
$form_state['values']['name'] = 'admin';

$form_state['values']['title'] = 'Programatic Page';
$form_state['values']['language'] = 'en';

$form_state['values']['body']['en'][0]['value'] = 'My own value';
$form_state['values']['body']['en'][0]['summary'] = 'My own summary';
$form_state['values']['body']['en'][0]['format'] = 'filtered_html';

$form_state['values']['menu']['enabled'] = 1;
$form_state['values']['menu']['link_title'] = 'Auto-page';
$form_state['values']['menu']['description'] = 'Own Programatic Page';
$form_state['values']['menu']['parent'] = 'main-menu:0';
$form_state['values']['menu']['weight'] = 5;

$form_state['values']['path']['alias'] = 'auto-page';
$form_state['values']['comment'] = 1;

$form_state['values']['status'] = 1;
$form_state['values']['promote'] = 0;
$form_state['values']['revision'] = 0;

$form_state['values']['op'] = t('Save');

drupal_form_submit('page_node_form', $form_state, $node);

Is there anything wrong?


Using Firebug (Mozilla Firefox plugin), I figured out the name of the elements and then fill it with the proper values.
Creating a 'page' node programmatically, in Drupal 7, is done using the following code:

global $user;

$customNode = new stdClass();

$customNode->type = 'page';
node_object_prepare($customNode);
$customNode->uid = $user->uid;
$customNode->name = $user->name;

$customNode->title = 'Your page title';
$customNode->language = 'en';
$customNode->body[$customNode->language][0]['value'] = 'Your value';
$customNode->body[$customNode->language][0]['summary'] = 'Your summary';
$customNode->body[$customNode->language][0]['format'] = 'filtered_html';

$customNode->menu['enabled'] = 1;       // 1 to enable providing a link in main menu
$customNode->menu['link_title'] = 'Your page link title';
$customNode->menu['description'] = 'Your page description';
$customNode->menu['parent'] = 'main-menu:0';
$customNode->menu['weight'] = 2;

$customNode->path['alias'] = 'Your Alias';
$customNode->comment = 1;

$customNode->status = 1;        // 1 means published
$customNode->promote = 0;
$customNode->revision = 0;

$customNode->changed = $_SERVER['REQUEST_TIME'];
$customNode->created = $_SERVER['REQUEST_TIME'];

node_submit($customNode);
node_save($customNode);

This not completely functioning.
The page is being created successfully with all the values, but the link is not shown in the main menu navigation (I mean in the main menu tabs near the Home tab).
I checked it out in the created content and it's there, accessed it via "edit" button, pressed "save" (without editing anything), and it appears in the main menu.

I want it to automatically show when it is created. Do you have any idea?

2 Answers 2

4

The Devel project includes a module called Devel Generate for generating dummy content. You could use it as an example. I think the parts you'd need are in the devel_generate.inc file.

1
  • 2
    But the devel_generate.inc file doesn't explicitly show how to create the page node! It's too complicated and uses tons of functions to create a node! I just need the minor changes to apply on the drupal 6.x documentation to achieve my goal in a drupal 7.x code. What are these changes?
    – Peter
    Commented May 30, 2011 at 8:08
1

An example of how to do this is here- just tried it and it works real nice:
How to programmatically create nodes, comments and taxonomies in Drupal 7

(This was actually found by codeinthehole for this answer, so thanks to him)

Your Answer

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

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