How to attach an image or file to node in Drupal 8 programmatically

Posted by admin on Tue, 01/30/2018 - 09:09

Quick code sample on how to get and attach an image or file to node or other entity programmatically in Drupal 8.

Here's an example how you could save an image to node if you get this with file_get_contents

use \Drupal\node\Entity\Node;
 
// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);
 
// Create node object with attached file.
$node = Node::create([
  'type'        => 'article',
  'title'       => 'Druplicon test',
  'field_image' => [
    'target_id' => $file->id(),
  ],
]);
$node->save();

If you have an image already exists in you file system - so you could just attach it without any additional file manipulation.

use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
 
// Create file object from a locally copied file.
$uri  = file_unmanaged_copy('public://source.jpg', 'public://destination.jpg', FILE_EXISTS_REPLACE);
$file = File::Create([
  'uri' => $uri,
]);
$file->save();
 
// Load existing node and attach file.
$node = Node::load(1);
$node->field_image->setValue([
  'target_id' => $file->id(),
]);
$node->save();

And special thanks to Drupal 8 developers - you could easily create an image placeholder!
Just like this:

use \Drupal\node\Entity\Node;
 
$node = Node::create([
  'type'  => 'article',
  'title' => 'Generated image test',
]);
$node->field_image->generateSampleItems();
$node->save();
×

Hey! Don't miss a new cool stuff!

Comments

123abc (not verified)

ค่ายเกมสล็อต We are happy to give advice to both new and old customers. There is a quality team to serve you 24 hours a day with a fast and automatic deposit and withdrawal system.

igoal88 (not verified)

igoal88 Online casino sites With every bet A complete answer to the gambler.

เว็บคาสิโน (not verified)

เว็บคาสิโนree chips. LEARN TO PLAY. New to Baccarat?

sexy baccarat (not verified)

sexy baccarat
Easily build on Or even giving away free credit

เว็บพนันออนไลน์ (not verified)

เว็บพนันออนไลน์Live broadcast, take off via stream, beautiful girls deal cards, pretty, wear bikinis, increase the enjoyment of playing.

เว็บ 88 (not verified)

เว็บ 88
Online gambling website that allows you to make great money And also allows you to bet 24 hours.

คาสิโนออนไลน์ (not verified)

There are many promotions with the benefit of participating in bets. naza55 คาสิโนออนไลน์

712 (not verified)

igoal
Guaranteed by the service for a long time.

igoal88 (not verified)

igoal88 The best online casinos in Thailand make good money.

zoom (not verified)

zoom Many people have heard of roulette. even though I've never played it before. Online casino roulette is a great first game to play as it is quite easy to understand

123 (not verified)

lassic online casino gambling games. สูตรหวยยี่กี 2564 ฟรี

เว็บ88 (not verified)

เว็บ88 The online gambling website that everyone recommends the most at this time. We have a professional team for fast deposits and withdrawals.

SA CASINO (not verified)

SA CASINO
SA CASINO online gambling service provider that meets international standards and meets all gambler needs

สูตรเซียน (not verified)

สูตรเซียน online football betting websites.

sa คาสิโน (not verified)

sa คาสิโน Web gambling money system the most stable in Asia.

naza55 naza (not verified)

The best online football betting websites. สล็อต55

ss (not verified)

The easiest way to check football prices.บอลสเต็ป

789heng.net (not verified)

Online games that collect the most on the website, can play all the time at 789heng only.<a href="https://789heng.net">สล็อตแตกง่าย</a>

bill (not verified)

igoal88 casino website that have all the betting in it to answer all the needed for the gambler

Naza1 (not verified)

ค่ายเกม Slots, Football, Casino

yolo (not verified)

igoal
Complete at one website There are all forms

naza (not verified)

สล็อต55 Many betting games are included to serve the gambler.

somjad sadden (not verified)

สล็อตแตกง่ายThe gambling website that slots is the easiest in the country. Easy to break. Give away a lot quickly.

III7X (not verified)

สูตรเซียน Jackpots are easy to break, get money quickly, we pay for real

5678 (not verified)

คาสิโน Online gambling service providers That answers all needs Excellent gambler 24 hours a day

คาสิโน (not verified)

คาสิโน How to play baccarat for money

6665 (not verified)

หวยไทยcasino website that have all the betting in it to answer all the needed for the gambler

เว็บ 123 (not verified)

เว็บ 123 Soccer Predictions & Free Tips.

เว็บ 123 (not verified)

เว็บ 123 The casino that has the most right now.

man citylion (not verified)

บาคาร่าแตกง่าย Online gambling websites that are most accepted by gamblers right now Easy to play for real money Fast deposit and withdrawal 24 hours by a professional team.

คาสิโน (not verified)

คาสิโน The best online casinos in Thailand make good money.

MM79 (not verified)

Igoal Is the number 1 most popular online gambling website.

123 (not verified)

The best online casinos in Thailand make good money.ค่ายเกมส์สล็อต

IGOAL (not verified)

IGOAL
IGOAL online gambling service provider that meets international standards and meets all gambler needs

เกมไฮโล (not verified)

เกมไฮโล arrange to your satisfaction slot gambling website for online slots Popular gambling games that members are very popular to play in this era. Because it's easy to play and get money fast.

sexy baccarat (not verified)

sexy baccarat
The best online casino website in Thailand

kawin21 (not verified)

สมัคร igoal88win is the website that have all the betting in it to answer all the needed for the gambler.

fuu88 (not verified)

Football betting websites that offer the best football odds
fuu88

เว็บ 123 (not verified)

เว็บ 123 The best online casinos in Thailand make good money.

Naza1 (not verified)

เว็บนาซ่า Casino, Football, Slots, barcarat

หวยไทย (not verified)

<a href="https://chaiyo24.net/"><strong>หวยไทย</strong></a>How to play slots on popular gambling websites.

หวยไทย (not verified)

หวยไทยHow to play slots on popular gambling websites.

ไฮโล (not verified)

ไฮโลTeach you to bet on live Hi-Lo and introduce how to bet on Sic Bo online on the ZOOM88 website for you to know. As a betting guide for new gamblers who do not yet understand.

naza55 naza (not verified)

because it is so worthwhile and meaningful to เว็บคาสิโน

NAZA (not verified)

เว็บพนันออนไลน์ Register for free online baccarat. No need to play Baccarat Poipet, just enter the website and get real money at home. Baccarat online, Baccarat formula is very popular to make money.

เกม55 (not verified)

Our complete online gambling site many casino games.เกม55

เว็บคาสิโน (not verified)

เว็บคาสิโน Register for free online baccarat. No need to play Baccarat Poipet, just enter the website and get real money at home. Baccarat online, Baccarat formula is very popular to make money.

123maxx (not verified)

เว็บ 123 We are happy to give advice to both new and old customers. There is a quality team to serve you 24 hours a day with a fast and automatic deposit and withdrawal system.

88 (not verified)

88 Betting on Baccarat in real time 24 hours. Our website is the most popular.

ss (not verified)

Online football betting In this age it is very easy to do. And gamblers around the world turned Online football bettingหวยไทย

Add new comment