This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join the live AMA session with Adam Silverstein on open source and WordPress core! Register Now →

How to Programmatically Create Nodes in Drupal 7

Updated on November 15, 2017

2 Min Read
Drupal-Create-Nodes

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:

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

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 7. If you need help, just leave a comment and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

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.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now