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

สมัคร pg slot (not verified)

สมัคร pg slot
The best online casino website in Thailand

Naza1 (not verified)

เว็บพนันออนไลน์ Casino, Football, Slots, Baccarat

เกม55 (not verified)

Recommend the best gambling website in this, very easy to play, best เกม55

IGOAL (not verified)

IGOAL
IGOAL Online gambling sites With every bet

somjad sadden (not verified)

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

123maxx (not verified)

sa casino In the past, if a gambler was to play a slot game, he would have to play at a foreign casino. But nowadays, anything is easy to do because it is the era

ค่ายเกม (not verified)

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

คาสิโน (not verified)

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

เกมไฮโล (not verified)

เกมไฮโล Hope you will try your luck with Sic Bo game. You will appreciate the low odds, but the returns are very high.

5678 (not verified)

ราคาแฮนดิแคป The best football betting website, football betting website, the best online casinos supervised by experts and professional team

123 (not verified)

We have a complete range of online casino legends.หวยหุ้น

naza55 naza (not verified)

เกม 55 you use a wonderful weblog here! do you wish to develop invite posts in my blog?

sa casino (not verified)

sa casino blog post internet site on googl

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.

wm casino (not verified)

wm casino is a online gambling site.have any gambling games in example lotto,slot etc.

ss (not verified)

Accepting investors of many rates หวยยี่กี

yolo (not verified)

แทงบอล
The best web Good for casino casino number1

bill (not verified)

คาสิโน website that have all the betting in it to answer all the needed for the gambler

789heng (not verified)

"Online games, easy to play, easy to break, easy to withdraw, with automatic deposit-withdrawal system, play on mobile phones
at 789heng only. สล็อตแตกง่าย"

789heng (not verified)

"Online games, easy to play, easy to break, easy to withdraw, with automatic deposit-withdrawal system, play on mobile phones
at 789heng only.สล็อตแตกง่าย"

thanunya (not verified)

หวยออนไลน์ How to play slots on popular gambling websites.

minx (not verified)

เว็บ 123
Which online casino gambling games that use skills to play it

naza (not verified)

เว็บคาสิโน Safe and confident Easy to play, easy to withdraw.

6665 (not verified)

ค่ายเกมส์สล็อตLotto is another betting that a lot of people in the world play and it been in this world for many many year

III7X (not verified)

เว็บพนัน Jackpots are easy to break, get money quickly, we pay for real

712 (not verified)

บาคาร่า123
Where else can you find a better allbet.

เว็บ88 (not verified)

เว็บ88 Good luck belongs to you throughout your investment

fun88 (not verified)

fun88
Online gambling website that allows you to make great money And also allows you to bet 24 hours.

123abc (not verified)

เว็บ 123 1 online gambling website We have a friend referral system that can easily make money. For you, all customers, convenient to use and easy to do

Naza1 (not verified)

sa casino Casino, Football, Slots, Baccarat

ค่ายเกม (not verified)

ค่ายเกม Online casino sites With every bet A complete answer to the gambler.

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

If you are looking for a standard online lotto, we recommend the best sites right now.
ราคาบอลสเต็ป

ZathanSpady (not verified)

azithromycin generic brand
azithromycin for skin infections?
zithromax otc
- zithromax 500 mg dosage
[url=http://zithromax-otc.com/#]zithromax 250mg otc
[/url] what dose of zithromax to use for gonorrhea

5678 (not verified)

sexy baccarat You will get the experience from our online casino. With a modern and safe service system, you can play Sexy Baccarat with us now. Now!

IGOAL88 (not verified)

IGOAL88
IGOAL88
Built to support people who want to play at a casino But unable to travel to the casino

เว็บ88 (not verified)

เว็บ88 Here at one place, a complete casino, quality level You can play games and earn money easily through our website anytime from your smartphone.

สูตรบาคาร่า (not verified)

The comments here were just as instructive as your post สูตรบาคาร่า

สล็อต55 (not verified)

Online casinos, also known as online gambling, are a form of digital gambling.สล็อต55 naza

6665 (not verified)

เกมส์สล็อตออนไลน์ Number one in online gambling services Complete in one place

88 (not verified)

88 here you will learn everything about bet.

NAZA (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.

III7X (not verified)

ag gaming Jackpots are easy to break, get money quickly, we pay for real

Naza1 (not verified)

sa คาสิโน Casino, Football, Slots, Baccarat

bill (not verified)

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

ss (not verified)

Online Slot Games It is a popular game that gamblers play all over the world. And is a game that many people have playedคาสิโน

ไฮโล (not verified)

ไฮโล Many tricks that a gambler should know It's easy to become a millionaire here.

712 (not verified)

ผลหวยหุ้นไทยวันนี้
One way to help you make money right now.

bill (not verified)

เว็บแทงหวย Lotto is another betting that a lot of people in the world play and it been in this world for many many year

ราคาบอล (not verified)

ราคาบอล he most interesting thing in the world is online right now

ราคาบอล (not verified)

ราคาบอล he most interesting thing in the world is online right now

Add new comment