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

bill (not verified)

เว็บ 123 casino website that have all the betting in it to answer all the needed for the gambler

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

กติกาบาคาร่า glad that it turned out so

thanunya (not verified)

ผลหวยหุ้นไทยวันนี้ if you boring you can play gambling games on this site.

naza (not verified)

เว็บเกมออนไลน์ที่ดีที่สุด "
is a very popular online casino.
We have every online casino you want to play make a deposit-withdrawal, no minimum, 24 hours a day, complete, convenient, can be played via any smartphone.
Our website is 100% safe."

yolo (not verified)

เว็บ 123
Guaranteed by the service for a long time.

123over (not verified)

Online slots, easy to break slots, heavy payouts.สล็อตแตกง่าย

ss (not verified)

ค่ายเกมส์สล็อต Online football betting In this age it is very easy to do. And gamblers around the world turned Online football betting

justin (not verified)

Absolutely pent subject matter, appreciate it for selective information . Growth Hacking

Abdullah (not verified)

Aw, this is quite a nice post. In concept I have to put in place writing such as this additionally – taking time and actual effort to produce a really good article… but so what can I say… I procrastinate alot and by no means manage to get something done. nose hoop

wm casino (not verified)

Our complete online gambling site naza55, many casino games.wm casino

123over (not verified)

Online slots, easy to break slots, heavy payouts.สล็อตแตกง่าย

III7X (not verified)

สูตรเซียน Jackpots are easy to break, get money quickly, we pay for real

zoom (not verified)

zoomEasy to play on your phone without downloading. Moreover, there are more than 3,000 slot games that you can choose to spin the slots in minutes by minute. Don't miss out on world-class slots games Come and prove yourself today

allbet (not verified)

allbet The best online casinos in Thailand make good money.

Abbasiseo Abbasiseo (not verified)

Some really interesting information, well written and broadly speaking user friendly . 먹튀

seo (not verified)

I enjoy the dear information you offer to your articles. I will bookmark your site and have the kids have a look at up here generally. Im fairly positive theyre likely to be informed quite a lot of new stuff here than anyone else! 먹튀

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

สูตรเซียน It has been open for a long time so there is no problem. In the matter of service

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

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

เว็บพนันออนไลน์ customers who want to play at the casino but do not want to travel to the real casino. We have raised the casino on the website for you to play easily.

MM79 (not verified)

ตารางบิงโก The best online gambling sites With every bet To meet all needs of the gambler with a complete range.

712 (not verified)

ลิงค์รับทรัพย์
Where else can you find a better casino

712 (not verified)

สมัคร igoal88win
The best web Good for investment Bet

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

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

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

123 (not verified)

Anyone can play, really easy to play.สูตรรูเล็ต

สมัคร IGOAL88WIN (not verified)

สมัคร IGOAL88win
สมัคร IGOAL88win High return service by a professional team for fast deposits and withdrawals The most popular online gambling website right

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

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

หวยยี่กี (not verified)

หวยยี่กี am glad that it turned out so well and

Naza1 (not verified)

saเกม Casino, Football, Slots, Baccarat

ราคาบอลไหล (not verified)

Casino site with all sorts of bets ราคาบอลไหล

ไฮโล (not verified)

ไฮโล Sic Bo betting tips that everyone should try.

bill (not verified)

เว็บ 123 casino website that have all the betting in it to answer all the needed for the gambler

yaiba (not verified)

หวยหุ้นช่อง 9 We are the service provider The best online casino with every bet to meet all the needs of the gambler completely.

ss (not verified)

I find luck quite predictable. If you want more luck, more opportunities, more enthusiasm. Appeared more oftenสูตรเซียน

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.

123over (not verified)

online slot games Online games that make money for players.สล็อตแตกง่าย

thanunya (not verified)

igoal88 Casino, Football, Slots, Baccarat

naza (not verified)

ไฮโล is famous for most people because can play when you free time. can play or home, office or drive you can play all day all time

Anonymous (not verified)

Do you ever wonder why it is still legal to put thousands of alcohol advertising ads on television, yet it's not legal to put tobacco ads on television? While secondhand smoke does kill many people each and every year, I truly believe drunk drivers kill many more. buy amphetamine salts online uk

naza55 naza (not verified)

Betting sites do not have to go anywhere. Here, one stop.

ไฮโล

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.สล็อตแตกง่าย"

sa casino (not verified)

sa casino Online casino easy to use You can access SA Casino with an online casino website.

sa casino (not verified)

sa casino The best online casinos in Thailand make good money.

III7X (not verified)

สล็อต แจ็คพอต แตกง่าย Jackpots are easy to break, get money quickly, we pay for real

712 (not verified)

สล็อตออนไลน์
Want to have money to spend? Come here.c

เว็บ88 (not verified)

เว็บ88 here you will learn everything about bet.

88 (not verified)

88Our website is ready to provide services that meet the needs of the players the most.

igoal (not verified)

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

MM79 (not verified)

Igoal Is the number 1 most popular online gambling website.

fun88 (not verified)

Football betting websites that offer the best football odds
fun88

Add new comment