drupal1 Min Read

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

Gorav Singal

March 18, 2018

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.
Share

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

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…