This blog describes about how to create a
new node programmatically in Drupal 7. If you want to add a new node, you can done
at node/add by default. In Drupal, you can also add a node programmatically.
Let see the below code.
After ran this code, you can see newly created node at admin/content. When you view that node, it looks like below image:
<?php
// create object
$node = new stdClass();
// set title for a node
$node->title = t('Created node programmatically');
// set node type
$node->type = 'article';
// set node language
$node->language = LANGUAGE_NONE;
// set value to node body
$node->body[LANGUAGE_NONE][0]['value'] = t('This node has been created programmatically in Drupal 7');
// set value to node body summary
//$node->body[LANGUAGE_NONE][0]['summary'] = text_summary(t('This node has been created programmatically in Drupal 7'));
// set node body format like plain_text, filtered_html, full_html
$node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
node_object_prepare($node);
// author for a node
$node->uid = 1;
// status of node 0 - unpublished, 1 - published
$node->status = 1;
// promoted to front page or not
$node->promote = 0;
// sitcky at top of tha page
$node->sticky = 0;
// comments 0 - hidden, 1 - closed, 2 - opened
$node->comment = 1;
// add term
$node->field_tags[$node->language][]['tid'] = 1;
// get the file path
$file_path = drupal_get_path('module', 'phponwebsites') . '/Desert.jpg';
// create file object
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => file_get_mimetype($file_path),
'status' => 1,
);
// Save the file to the public directory. You can specify a subdirectory, for example, 'public://images'
$file = file_copy($file, 'public://');
// assign the file object into image field
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
// Prepare node for a submit
$node = node_submit($node);
//save the node
node_save($node);
// create object
$node = new stdClass();
// set title for a node
$node->title = t('Created node programmatically');
// set node type
$node->type = 'article';
// set node language
$node->language = LANGUAGE_NONE;
// set value to node body
$node->body[LANGUAGE_NONE][0]['value'] = t('This node has been created programmatically in Drupal 7');
// set value to node body summary
//$node->body[LANGUAGE_NONE][0]['summary'] = text_summary(t('This node has been created programmatically in Drupal 7'));
// set node body format like plain_text, filtered_html, full_html
$node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
node_object_prepare($node);
// author for a node
$node->uid = 1;
// status of node 0 - unpublished, 1 - published
$node->status = 1;
// promoted to front page or not
$node->promote = 0;
// sitcky at top of tha page
$node->sticky = 0;
// comments 0 - hidden, 1 - closed, 2 - opened
$node->comment = 1;
// add term
$node->field_tags[$node->language][]['tid'] = 1;
// get the file path
$file_path = drupal_get_path('module', 'phponwebsites') . '/Desert.jpg';
// create file object
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => file_get_mimetype($file_path),
'status' => 1,
);
// Save the file to the public directory. You can specify a subdirectory, for example, 'public://images'
$file = file_copy($file, 'public://');
// assign the file object into image field
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
// Prepare node for a submit
$node = node_submit($node);
//save the node
node_save($node);
After ran this code, you can see newly created node at admin/content. When you view that node, it looks like below image:
Now I’ve hope you know how to create a new node programmatically in
Drupal 7.
Related articles:
Add new menu item into already created menu in Drupal 7
Add class into menu item in Drupal 7
Create menu tab programmatically in Drupal 7
Add custom fields to search api index in Drupal 7
Clear views cache when insert, update and delete a node in Drupal 7
Create a page without header and footer in Drupal 7
Login using both email and username in Drupal 7
Redirect users into any page after logged into a site in Drupal 7
Add new menu item into already created menu in Drupal 7
Add class into menu item in Drupal 7
Create menu tab programmatically in Drupal 7
Add custom fields to search api index in Drupal 7
Clear views cache when insert, update and delete a node in Drupal 7
Create a page without header and footer in Drupal 7
Login using both email and username in Drupal 7
Redirect users into any page after logged into a site in Drupal 7
No comments:
Post a Comment