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

minx (not verified)

เว็บคาสิโนออนไลน์
Each online slot game is different. Which the rules of play

Anonymous (not verified)

Online gambling website that slots are easy to break, baccarat is easy to see cards The best water ball, easy to bet, fast transfer money, must be 789heng 789heng

Anonymous (not verified)

Online gambling website that slots are easy to break, baccarat is easy to see cards The best water ball, easy to bet, fast transfer money, must be 789heng 789heng

Abbasiseo Abbasiseo (not verified)

I needed to put you the little bit of note to finally say thanks over again about the pleasant pointers you’ve contributed at this time. It’s really incredibly generous of people like you to provide without restraint all most people could have offered as an ebook in making some bucks on their own, even more so considering that you could possibly have tried it in case you desired. The inspiring ideas additionally served as the good way to fully grasp that other people online have the identical zeal just like mine to learn a little more in regard to this condition. I am sure there are many more enjoyable sessions ahead for individuals that check out your blog. Anti-Rat IL-10

justin (not verified)

Needed to compose you a tiny note to finally thank you very much yet again for your personal splendid methods you have discussed above. It is strangely open-handed with people like you to provide publicly all that a number of people would have marketed as an electronic book to generate some bucks for their own end, primarily now that you could possibly have tried it if you ever wanted. These inspiring ideas likewise acted like a fantastic way to know that the rest have the same dreams really like my personal own to see a whole lot more concerning this problem. I’m sure there are thousands of more enjoyable times in the future for many who check out your blog. ฮาร์ดดิสกล้องวงจรปิด

Propecia (not verified)

Buy 1 Mg Prednisone Pills Online

chansin tanhin (not verified)

Enjoy all online slots games at 123over. สล็อต

justin (not verified)

Ich kenne einige Leute, die aus Kanadakommen. Eines Tages werde ich auch dorthin reisen Lg Daniela ฮาร์ดดิสก์

Anonymous (not verified)

online games for real money Play anywhere and anytime, only at 123over. slot

Cafeseast (not verified)

order cialis

Cafeseast (not verified)

cialis generic buy

sa casino (not verified)

sa casino Do you want extar money?

sexy baccarat (not verified)

sexy baccarat Comprehensive online betting site

MM79 (not verified)

igoal88 Leading casino Excited with gambling in online casinos It is becoming popular nowadays. There are pretty, beautiful girls who are always dealing with cards without getting bored. Full of fun, exciting, challenging, comes with a clear HD picture system.

igoal88 (not verified)

igoal88 There is a best friend referral system. where you can earn up to 1% commission from your friend's play without having to play even a single baht Sign up for free No minimum deposit-withdrawal Open 24 hours

Naza1 (not verified)

sa casino Football, Slots, Baccarat

เว็บ88 (not verified)

เว็บ88Online casino website With all kinds of bets That can make a lot of money for the gambler

fuu88 (not verified)

fuu88
Is the number 1 most popular online gambling website

ฝาก300ฟรี100 (not verified)

ฝาก300ฟรี100 The casino that has the most right now.

bina pink (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.

zoom (not verified)

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

naza (not verified)
Anonymous (not verified)

สล็อต123
The best online casino website in Thailand and make good money.

minu (not verified)

สล็อตออนไลน์
Or your technique In increasing your chances of winning

slots (not verified)

789heng No. 1 direct website Easiest slots to break in Thailand สล็อต

bina pink (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.

thanunya (not verified)

บาคาร่า123 Lots of games, including many online slots betting games

minu (not verified)

บาคาร่า123
Want to play online casinos, must AG Asia Gaming

Locuoub (not verified)

Viagra

yolo (not verified)

igoal88 เครดิตฟรี
Many good promotions that look for both old and new customers.

ufapony88 (not verified)

ufa This web is just like you at the casino but In online.

freerurgy (not verified)

Propecia

freerurgy (not verified)

Propecia

saksun kholoy (not verified)

บาคาร่าThe best baccarat in Thailand is easy to break the most and the best must be ufapony88

Lasix (not verified)

Viagra Bestellen Empfehlung

knockadia (not verified)

cialis online prescription

Locuoub (not verified)

viagra and prostatitis

Propecia (not verified)

Order Xenical Online Australia

icorync (not verified)

Lasix

Anonymous (not verified)

123คาสิโน A casino that will definitely make you rich

furosemide and ... (not verified)

cialis propiedades

Cialis (not verified)

Propecia Online Us

Lasix (not verified)

Buy Zithromax Online Us

naza368 (not verified)

<a href="https://naza368.net/”>NAZA368</a> Deposit-withdrawals auto call center working 24 hours.

gensywhes (not verified)

Stromectol

NAZA368 (not verified)

NAZA368 Deposit-withdrawals auto call center working 24 hours.

789heng (not verified)

Online slots, fast service Ready to give advice 24 hours a day at 789heng only.[url=https://789heng.net]สล็อต[/url]

789heng (not verified)

Online slots, fast service Ready to give advice 24 hours a day at 789heng only.สล็อต

naza368 (not verified)

NAZA368 Deposit-withdrawals auto call center working 24 hours.

Stromectol (not verified)

Cialis 100mg Dosage

Add new comment