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

MM79 (not verified)

sa casino The best online gambling sites With every bet To meet all needs of the gambler with a complete range.

sa casino (not verified)

sa casinois the internet's top guide to slot games and online casinos.

fuu88 (not verified)

fuu88
Online football And online football betting websites That these gamblers use the service,
that is, is an online football betting website. The best in this era!

หวยหุ้น (not verified)

หวยหุ้น Thai stock results, open in the morning, close at noon, open in the afternoon, close in the evening, market channels.

ไฮโล (not verified)

ไฮโลYou can connect to the internet. Access from your home This channel allows you to play Sic Bo safely. without having to worry or feel risked anymore

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

พนันออนไลน์
We have strengths in diversity and fairness to our users. Online.

กติกาบาคาร่า (not verified)

กติกาบาคาร่า The best online casinos in Thailand make good money.

5678 (not verified)

วิธีดูราคาบอล The best football betting website, football betting website, the best online casinos supervised by experts and professional team

bbobo (not verified)

web casino online best of thailand.เว็บ 123

123maxx (not verified)

sa casino 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

คาสิโน (not verified)

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

เว็บ 123 (not verified)

เว็บ 123Popular slot online.

คาสิโน (not verified)

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

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

IGOAL (not verified)

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

เกม55 (not verified)

in our rating there are casino games you can play online with confidence on any site you like.เว็บคาสิโน naza55

naza55 naza (not verified)

To choose a gambling website to play various gambling games สูตรบาคาร่า

Naza1 (not verified)

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

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.

ss (not verified)

หวยหุ้น casino. We have raised the casino on the website for you to play easily.

เทิร์นโอเวอร์ (not verified)

เทิร์นโอเวอร์ Online gambling has been compiled in this website the best

เว็บ88 (not verified)

เว็บ88 Online football betting at our website Easy to play, get real money, 24 hours service.

NAZA28 (not verified)

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

ไฮโล (not verified)

ไฮโลComprehensive online gambling website popular service the most in Thailand Accept all types of online sports betting and casinos.

yolo (not verified)

คาสิโน
We are also a standard website. In terms of providing world-class service

Harzyhoope (not verified)

humanistu stromectol over the counter buy
tontonan ivermectin dosage for goats
stromectol no prescription
- ivermectin 3mg
[url=http://ivermectin-stromectol.net/#]ivermectin 3mg tablets
[/url] bresanis ivermectin amazon

หาเงินออนไลน์ (not verified)

providers are guaranteed to give you the best หาเงินออนไลน์

thanunya (not verified)

เว็บ123 We are also a standard website. In terms of providing world-class service

AAapckb (not verified)

Перші ластівки 3 сезон Перші ластівки 3 сезон gcvk iav xadz uey btqp oat

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.

เว็บหวยออนไลน์ (not verified)

เว็บหวยออนไลน์ The number 1 online lottery website that is the hottest. We have services for both government lottery, Laos lottery, Hanoi lottery.

igoal (not verified)

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

คาสิโน (not verified)

คาสิโน is your new place for top sports betting tips

แฮนดิแคป (not verified)

แฮนดิแคป
I must say that That our gambling website Giving you high football prices and very good water bills Plus different from other websites That is modern, cutting-edge, not lagging behind Meet international standards

5678 (not verified)

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

MM79 (not verified)

igoal88 Online gambling service providers That answers all needs Excellent gambler 24 hours a day.

sexy baccarat (not verified)

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

IGOAL (not verified)

IGOAL
IGOAL Online gambling websites that meet the needs of gamblers

เกมไฮโล (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.

เกมไฮโล (not verified)

เกมไฮโล is a online gambling site.have any gambling games in example lotto,slot etc.

boo (not verified)

The best online gambling website that offers a variety of games for customers to choose to play as a way to make easy money.เว็บคาสิโน

Naza1 (not verified)

NAZA Slots, Football, Lottery, Casino

sexy baccarat (not verified)

sexy baccarat
Easily build on Or even giving away free credit

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

88 (not verified)

88 Here in one place we have all the bets you need. Apply and receive money easily through refer a friend system. The more you share, the more you get.

bill (not verified)

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

naza55 (not verified)

Slots, Football, Lottery, Casino เกม 55

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

g2gwin8888 (not verified)

G2GWIN88 It operates out of a physical location in Poipet, Cambodia. There are numerous communication channels that can be utilized.

ss (not verified)

เว็บหวยออนไลน์ Finally there is entertainment in your spare time, there are over 100 games to choose from.

Add new comment