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

zoom (not verified)

zoom zoom88 Online gambling service providers That answers all needs Excellent gambler 24 hours a day

igoal (not verified)

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

เว็บหวย (not verified)

เว็บหวย Thai stock market lottery is a lottery betting. Play to predict numbers based on the results of the prize draw from the stock exchange.

MM79 (not verified)

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

fun88 (not verified)

fun88
Getting started at a legal, real-money online casino

คาสิโน (not verified)

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

88 (not verified)

88For online football betting on the ZOOM88 website, besides being comfortable Football prices are equally important. because of online football betting There will be a very good football price.

123maxx (not verified)

ยอดเทิร์นโอเวอร์ 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

NAZA28 (not verified)

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

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

Lots of games that everyone should not miss out on. เว็บคาสิโน naza55

Naza1 (not verified)

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

sa casino (not verified)

sa casino Betting on online casino is the best answer for the gambler.

IGOAL (not verified)

IGOAL
IGOAL I am glad that it turned out so well and I hope it will continue in the future because it is so worthwhile and meaningful to the community

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

บอลสเต็ป Which website to bet on football? Popular questions with Which website to bet on football? It is a popular question in the football gambling industry.

เว็บ88 (not verified)

Enjoy a new type of betting and earn a lot of money here. เว็บ88

เกมสล็อต (not verified)

เกมสล็อตMany popular slots games and gambling.

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

เว็บพนันออนไลน์ Recommend the best gambling website in them, this is really easy to play.

88 (not verified)

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

ตารางบิงโก (not verified)

ตารางบิงโก We are the service provider Online casinos With every bet A complete answer to the gambler.

5678 (not verified)

pg123 A collection of many popular online betting games. That will make the gambler enjoy

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.

MM79 (not verified)

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

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

Lasix (not verified)

Levitra Prezzo Farmacia Italia

zoom (not verified)

zoomUsing the friend referral system Just you are a Zoom88 member, then use the link to get money. in your system Forward to friends to subscribe Just like this, you will get paid commission from playing.

NAZA (not verified)

allbet 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

yolo (not verified)

คาสิโน
Complete at one website There are all forms
play until you are confident

III7X (not verified)

NAZA 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

naza (not verified)

naza The best online casinos in Thailand make good money.

sexy baccarat (not verified)

Football betting websites that offer the best football odds
sexy baccarat

abejessah (not verified)

Plaquenil

Graiveada (not verified)

http://buylasixshop.com/ - lasix pharmacy takes paypal payment

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.

Naza1 (not verified)

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

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

คาสิโนออนไลน์ Play through any smartphone, complete, convenient, our website is 100% safe.

เว็บแทงหวย (not verified)

am glad that it turned out so well เว็บแทงหวย

ss (not verified)

เว็บแทงหวย 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

NAZA (not verified)

NAZA The best online football betting websites

ไฮโล (not verified)

ไฮโล Experience with special bets in the form of an online casino that offers everything like a real casino to play for real money via smartphone every machine.

bunny bunny (not verified)
Zithromax (not verified)

principio activo viagra

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

สูตรเซียนCan bet lottery every day, but rich.

ตารางบิงโก (not verified)

ตารางบิงโก
ตารางบิงโก Easy to play, fast, real giveaway, bingo.

thanunya (not verified)

เว็บ123 Online casinos Alternative sources of income that you should not miss

NAZA28 (not verified)

เทิร์นโอเวอร์ Online gambling website that allows you to make great money And also allows you to bet 24 hours.

5678 (not verified)

เว็บคาสิโนออนไลน์ An online casino site that offers all forms of gambling for gamblers to experience a complete range of fun.

naza (not verified)

เว็บคาสิโน Of course, there must be a difference already, as the online gambling game has a simple, easy and fun process.

naza55 naza (not verified)

ราคาบอลไหลThe best game providers are guaranteed to give you the best odds. to make you earn real money

yolo (not verified)

naza
here are both card games OnlineSlot Games

igoal (not verified)

igoal88 An online casino site that

Add new comment