X
    Categories: Drupal 7 Tutorials, Tips and GuidesDrupal Tutorials, Tips and Guides

How to Programmatically Create Nodes in Drupal 7

The prime purpose of content management systems like Drupal is to make it easier for the end users to create websites. For the developers, customizing the CMS to the specific requirements of the projects is a great way of getting things up and ready quickly and efficiently.

Nodes are a great Drupal feature that organizes and controls the way in which all content within a Drupal website is displayed. All published content pieces are “nodes” that could be individually or collectively created and manipulated. To add a custom content type, a developer only has to create or modify the associated node either through the GUI or programmatically.

In this post, I’ll show you how to programmatically create nodes in Drupal 7 through simple code snippets. Let’s get into it.

Initialize Node Object

The following snippet of code will initialize a node object:

$node = new stdClass(); // We create a new node object

$node->type = "page"; // Or any other content type you want

$node->title = "Your title goes here";

$node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled.

$node->path = array('alias' => 'your node path'); // Setting a node path

node_object_prepare($node); // Set some default values.

$node->uid = 1; // Or any id you wish

Note: If you have a multi language site, you will have to enable the locale module and put the code of the language you wish your node to have in place of ‘LANGUAGE_NONE’ for $node->language.

Add Custom Fields

Adding custom fields is easy and could be done through the following snippet:

$node->field_id[LANGUAGE_NONE][0]['value'] = $modelnumber;
$node->field_myfield['en'][0]['value'] = $ENbody; // Add your custom field formats here.
$node->field_myfield['nl'][0]['value'] = $NLbody;

Add Files or Images

Images or other file types could be easily added to nodes without much trouble:

// Some file on our system

$file_path = drupal_realpath('somefile.png'); // Create a File object

$file = (object) array(

'uid' => 1,

'uri' => $file_path,

'filemime' => file_get_mimetype($file_path),

'status' => 1,

);

$file = file_copy($file, 'public://'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images'

$node->field_image[LANGUAGE_NONE][0] = (array)$file; //associate the file object with the image field:

Add a term to node

You can also add terms to a node for easier categorization. Terms allow developers to improve content categorization and enhance the content search process.  Adding terms to nodes requires the following single line of code:

$node->field_tags[$node->language][]['tid'] = 1; //1 is the term id being assigned to the field tag (name of term reference field)

Save the node

The only thing remaining now is to save the node created earlier. Here is how to do that:

$node = node_submit($node); // Prepare node for a submit

node_save($node); // After this call we'll get a nid

Conclusion

Nodes are a great way of managing content on Drupal website. The best thing is that the developers could easily manage various aspects of nodes pro through simple code snippets. I hope that you could now confidently handle nodes at your Drupal website. If you found this blog post useful you might like to check out another post by us on, How To Install And Enable Elasticsearch On Drupal 7If you need help, just leave a comment and I will get back to you.

 

Hamza Zia: Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.