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

arm168 (not verified)

หวยหุ้นไทยวันนี้You can check the Thai stock market lottery today conveniently at your fingertips. Tap on this link and you can enjoy it.

ayan (not verified)

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. 토토사이트

ayan (not verified)

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. 토토사이트

ss (not verified)

At home or on vacation, it can also be Online football betting Anytime, anywhereราคาบอลไหล

123over (not verified)

We are number 1 in online gambling, deposit, withdraw, auto, comfortable, comfortable. 123over.สล็อต

g2g123 (not verified)

เว็บสล็อตเว็บตรง
Popular casinos for money by slot

บังรอ ยียี (not verified)

Give away every day, bang every day, play online slots at.สล็อต

bill (not verified)

สล็อต slot is just like you at the casino but In online. There everything like real casino

coupons for cia... (not verified)

viagra posologie

pong (not verified)

สล็อตเว็บตรง
This site pays for real, pays for real.
One user ends everything
The website does not go through
There are staff to take care of you 24 hours a day.
Deposit - withdraw automatically 24 hours

wanna (not verified)

สล็อตเว็บตรง The best website you want
Easy to play, real pay, lots of promotions.
#boxing #lottery #lottery slots
automatic deposit and withdrawal system
The website is straight, stable and secure.

gabapentin cost (not verified)

quiero comprar viagra farmacia

789heng (not verified)

The best online casino sites in Thailand at 789heng only.สล็อต

6665 (not verified)

ยอดเทิร์นโอเวอร์ How to calculate the turnover to help gamblers make a lot of money

789heng (not verified)

The best online casino sites in Thailand at 789heng only. บาคาร่า

wm casino (not verified)

wm casino You can apply for wm casino on our website. through the automatic system, only 10 seconds to complete

oing (not verified)

This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info:สูตรสล็อต

igoal (not verified)

igoal The best web Good for casino casino number1.

712 (not verified)

เกม123
Full casino, baccarat, slots, fish shooting games and other games

<a href="https:... (not verified)

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

เกม123 (not verified)

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

>เกม123 </a> (not verified)

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

pp0214 (not verified)

ยอดเทิร์นโอเวอร์ Online casino sites that are standard and can meet the needs of the gambler as well, must be here.

123maxx (not verified)

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

chobcakewanila (not verified)

สล็อตเว็บตรงขั้นต่ำ1บาท The best online casino sites in Asia

JDED (not verified)

ทางเข้า 123 make money this way !

amy (not verified)

ยอดเทิร์นโอเวอร์ online website Online24club online website that includes most online, no minimum deposit, withdrawal And deposit and withdraw automatically as well.

igoal (not verified)

igoal The best casinos in Thailand make good money

arm168 (not verified)

บาคาร่า123 access to casino play Very easily, just “using your fingertips”, who would have thought that Play online gambling games, especially this game of Baccarat, you can earn money. Can fit in a lot of bags Plus, get real money without cheating as well. from all you need Baccarat online, working for at least 2 jobs, you don't have to do that anymore because baccarat is a good solution for everyone. Currently, he is working full time only for one job.

thanunya (not verified)

หวยหุ้น experts and a professional team for quick deposit-withdrawal.

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

เว็บนาซ่า providers are guaranteed to give

bill (not verified)

สล็อต123 website that have all the betting in it to answer all the needed for the gambler

arm168 (not verified)

igoalA collection of popular casino games around the world. Guaranteed fun and complete service

allbet (not verified)

allbet Best Casino Sites Click Now

123ABC (not verified)

allbet For online gamblers betting is your answer.

123 (not verified)

The biggest and best lottery website give a full baht per unit There are no numbers.เว็บแทงหวย

arm168 (not verified)

เว็บเเทงหวยIf you have a lucky number, you can join the fun with chokde, a lottery website that offers impressive service at all levels.

123over (not verified)

We are number 1 in online gambling, deposit, withdraw, auto, comfortable, comfortable. 123over.123over

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

คาสิโนออนไลน์ The exclusive home of gambling website

thanunya (not verified)

สมัครigoal88win Of course, there must be a difference already as the online gambling game has a simple easy and fun process.

เกมสล็อต ค่าย pg (not verified)

If you are looking for a standard online lotto, we recommend the best sites right now.
เกมสล็อต ค่าย pg

ufapony88 (not verified)

สล็อต

Online slot betting website no.1

justin (not verified)

I have been checking out a few of your stories and i must say nice stuff. I will surely bookmark your website Bilverksted bergen

Naza1 (not verified)

sa คาสิโน The biggest Baccarat Thailand

justin (not verified)

Some truly nice and utilitarian info on this internet site , besides I think the layout holds wonderful features. poker bitcoin

ufapony88 (not verified)

สล็อต

Online slot betting website no.1

Anonymous (not verified)

I would like to say that this blog really convinced me to do it! Thanks, very good post. 파워볼사이트

neurontin 300 mg (not verified)

Cialis Online Fast Delivery

viagra vs cialis (not verified)

Allergies Amoxicillin

gapspyza (not verified)

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.สล็อตแตกง่าย

Add new comment