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

Naza1 (not verified)

28 Slots, Football, Lottery, Casino

เว็บพนัน (not verified)

เว็บพนัน Slots, online fish shooting games, mole games, card games, all in one place Apply today with us Guarantee that you won't be disappointed with shooting games or online slots.

MM79 (not verified)

บิงโก Online gambling service providers That answers all needs Excellent gambler 24 hours a day.

บิงโก (not verified)

บิงโก is the website that have all the betting in it to answer all the needed for the gambler.

เว็บคาสิโนออนไลน์ (not verified)

เว็บคาสิโนออนไลน์ The best online casinos in Thailand make good money.

naza55 naza (not verified)

Comprehensive online gambling websites open for mobile services Deposit-withdraw, no minimum, quickly by a professional team. ไฮโล

bina pink (not verified)

สูตรเซียน Casino Baccarat, online gambling website gambling service, online casino, many selections, Baccarat, Dragon Tiger, slots, fish shooting, deposit 1 sec, withdraw 15 sec, fastest

ไฮโล (not verified)

ไฮโล naza55 Betting is the most return rate in betting. You can bet in any team that you want to bet. Also, it real live time betting so you cannot miss this bet at all, I gaurantee you

พนันออนไลน์ (not verified)

Betting sites do not have to go anywhere. Here, one stop
พนันออนไลน์

igoal88 (not verified)

igoal88 Hello there, just became aware of your

5678 (not verified)

NAZA Online casino website With all kinds of bets That can make a lot of money for the gambler

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

เว็บพนันออนไลน์ If you are looking for extra income Our website will make you rich easily.

เว็บนาซ่า (not verified)

เว็บนาซ่า nline gambling website that has received the number

bunny bunny (not verified)

The best of betting games, baccarat, slots, roulette and many others that you can choose from, easy to use, available 24 hours a day.naza

บิงโก (not verified)

บิงโก
บิงโก Bingo can be profitable, fast and fun

ss (not verified)

♥ ♥ igoal88♥ ♥ Finally there is entertainment in your spare time, there are over 100 games to choose from.

minb (not verified)

เว็บคาสิโนออนไลน์
It is the ultimate in gambling games. There is a slot game that is popular with people playing another camp.

NAZA28 (not verified)

ค่ายเกม Betting on online is the best answer for the gambler.

yolo (not verified)

สล็อตออนไลน์
Deposit-withdrawal, fast, stable, excellent service

thanunya (not verified)

เว็บ123 The best online casino website in Thailand

Mueed Ahmed (not verified)

Spot i’ll carry on with this write-up, I actually think this site needs considerably more consideration. I’ll more likely be once more to see a great deal more, thanks for that information. sa casino

Whasksaks (not verified)

https://buyplaquenilcv.com/ - Plaquenil

Plaquenil (not verified)

Amoxicillin Recommended Dosage

seop (not verified)

I like this post, enjoyed this one appreciate it for putting up. website slot tos885

can you buy zit... (not verified)

Where To Purchase Clomphene

abejessah (not verified)

how long does it take plaquenil to work for lupus

Anonymous (not verified)

I just want to complete a quick comment as a way to express gratitude to you personally for all those wonderful pointers you’re posting at this site. Time consuming internet investigation has by the end for the day been rewarded with top quality strategies to tell my guests. I might say that many people guests can be extremely endowed to happen in an amazing network with methods . marvellous individuals with useful hints. I feel quite privileged to obtain used your webpages and check toward really more fabulous minutes reading here. Thank you for most things. voyance numero gratuit

Zithromax (not verified)

cialis guidelines

Henrytrief (not verified)

gay incest dating
gay dating issues
gay dating dragon age origins

igoal88 (not verified)

igoal88 We have gathered all online gambling in one place.

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

เว็บคาสิโนSpin the best free slots, compete in leagues, join clubs and hit Jackpot!

ไฮโล (not verified)

ไฮโล Top gambling sites Best football betting website Best online casino Supervised by experts And a professional team for quick deposit-withdrawal.

sa casino (not verified)

sa casino We are a provider of online gambling websites that will meet the full range of gamblers.

เว็บ88 (not verified)

เว็บ88 playing baccarat easy to understand Because there are counting characteristics and playing cards bounce. If anyone has ever played Pokdeng Will be able to play baccarat is not difficult at all.

Anonymous (not verified)

NAZA Casino Baccarat, online gambling website gambling service, online casino, many selections, Baccarat, Dragon Tiger, slots, fish shooting, deposit 1 sec, withdraw 15 sec, fastest

สล็อตออนไลน์ (not verified)
123abc (not verified)

sexy baccarat Hello there, just became aware of your blog through Google, and found that it’s really informative. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers

MM79 (not verified)

24 Is the number 1 most popular online gambling website.

เว็บ 123 (not verified)

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

abejessah (not verified)

Plaquenil

III7X (not verified)

เว็บพนัน Easy to apply for membership, deposit 200, get an additional 100, free deposit-withdraw, no minimum, easy to play, modern auto system. Guarantee safety, betting on football, lottery, slots, complete

เว็บ 88 (not verified)

เว็บ 88
Betting on online is the best answer for the gambler.

Trubbebra (not verified)

http://buypriligyhop.com/ - Priligy

5678 (not verified)

เว็บคาสิโน We are the service provider The best online casino with every bet to meet all the needs of the gambler completely.

Naza1 (not verified)

28 Slots, Football, Lottery, Casino

เว็บพนัน (not verified)

Fish Hunter Game is the most common site that people usually play because of the nice graphic and the return rate is very high. เว็บพนัน

28 (not verified)

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

Priligy (not verified)

Rio Rico Pharmacy Canadian Rx Net

ราคาบอลสเต็ป (not verified)

ราคาบอลสเต็ป there, just became aware of your blog through

naza55 naza (not verified)

Gather the hottest promotions That appeals to all investors ไฮโล

Add new comment