drupal|March 18, 2018|1 min read

Drupal 7: How to save a node programmatically and add an image field from a public URL

TL;DR

Use system_retrieve_file to download an image from a URL and attach it as a file object to a programmatically created Drupal 7 node.

Drupal 7: How to save a node programmatically and add an image field from a public URL

Introduction

I have a content type, which has an image field. I want to create many of these nodes programmatically.

Note: I have public URLs of these images, which I want to save.

Code to create Node

``` function _prepareImageObj($imageUrl) { if (!$imageUrl) { return FALSE; } $imageRes = drupal_http_request($imageUrl); if ($imageRes->code != 200) { return FALSE; } //prepare target path on disk $targetPath = 'public://prefix_' . date('Y').'/'.date('m'); file_prepare_directory($targetPath, FILE_CREATE_DIRECTORY); $imageFile = file_save_data($imageRes->data, $targetPath, FILE_EXISTS_REPLACE);

return $imageFile; } function save_my_node($valueArray) { $node = new stdClass(); $node->title = $valueArray->title $node->type = ‘my_content_type’; node_object_prepare($node);

$node->language = LANGUAGE_NONE; $node->uid = 1; //see if you want to use another user, or logged in user $node->status = 1; $node->promote = 0; $node->comment = 1; //comments closed $node->sticky = 0;

$node->body[$node->language][0][‘value’] = $valueArray->description; $node->body[$node->language][0][‘format’] = filter_default_format(); $imageObj = _prepareImageObj($valueArray->imageUrl); if (!$imageObj) { return false; } $node->field_image[$node->language][0] = (array)$imageObj;

// Prepare node for saving if ($node = node_submit($node)) { node_save($node);

print "\nNode saved, nid: ".$node->nid . ", videoId: ".$youtube_doc['_id'];

} else { print ‘Error’; } }


You can customize this code for more robust error handling.

Enjoy.

Related Posts

Drupal 7 - Code for Exporting all your content nodes in json files

Drupal 7 - Code for Exporting all your content nodes in json files

Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…

Drupal: How to block a user by email programatically

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

Drupal 8: How to Export and Import View

Drupal 8: How to Export and Import View

You have created some views, and want to port it to your production environment…

Drupal Code: Fetch Link, Title, Category names, tag names from every article

Drupal Code: Fetch Link, Title, Category names, tag names from every article

See the code below: The output will be 4 columns separated by comma. You can…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…