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

fun88 (not verified)

fun88
The most trusted online gambling website at the moment

igoal (not verified)

igoal The best online casinos in Thailand make good money.

Naza1 (not verified)

sa casino Casino, Football, Slots, Baccarat

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.

sexy baccarat (not verified)

sexy baccarat Do you want extar money?

IGOAL88 (not verified)

IGOAL88
IGOAL88
Deposit - withdraw automatically 1 minute, bill cuts fast, automatic update web, not closed, update, disturbing customers.

123abc (not verified)

sexy baccarat 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

Anonymous (not verified)

เว็บ88 Everyone can easily earn money. Just play games on your smartphone through our online gambling website.

789heng (not verified)

Online games, casino slots, all famous slots games must be slots only. at 789heng only. slot

5678 (not verified)

ค่ายเกมสล็อต Lots of games, including many online slots betting games

สล็อต55 (not verified)

สล็อต55 naza Casino site with all sorts of bets

หวยลาว (not verified)

หวยลาว many milagrams should i take

เว็บ 88 (not verified)

เว็บ 88
If you are looking for a standard online lotto, we recommend the best sites right now.

ss (not verified)

Accepting investors of many ratesเว็บนาซ่า

mueed (not verified)

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks! oneplus service center in Hsr Layout

mueed (not verified)

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks! Chocolate Cold Storage in Ahmedabad

mueed (not verified)

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks! Best Dental academy in Gujarat

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

พนันออนไลน์
Easily build on Or even giving away free credit

Anonymous (not verified)

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks! Foam Water Monitor

naza (not verified)

ปั่นแปะออนไลน์ convenient to use and easy to do

minc (not verified)

สล็อตออนไลน์
Or your technique In increasing your chances of winning

bill (not verified)

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

thanunya (not verified)

บาคาร่า123 Will collect online gambling games, there is a trial service, also supports Androi and iOS

saksun kholoy (not verified)

บาคาร่าOnline gambling website that results withdraw easily Easy to see cards, must be ufapony88

ss (not verified)

Online casinos It is an online casino with a system to play through electronic tools.คาสิโนออนไลน์

ราคาบอล (not verified)

ราคาบอล naza55 can made your relax if you strain in the real life you can enjoy with gambling games in this website.

knockadia (not verified)

Cialis

seo person (not verified)

I Just stopped by to say your article is great. The clarity in your post is simply spectacular and i can assume you’re an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please continue the enjoyable work. Nocturnal Omissions Artist

ufapony88 (not verified)

สล็อต Popular gambling sites of all forms.

Cafeseast (not verified)

Cialis

Cialis (not verified)

Cialis Canada Viagra

เว็บ88 (not verified)

เว็บ88The Hi-Lo betting technique is similar to the first one, but the Hi-Lo betting technique in this article is a medium-risk. Betting on 2-3 numbers or even even numbers and totals is a bit more risky. But rest assured that will make money from using the technique of betting on dice

ss (not verified)

Good luck belongs to you throughout your investment.เว็บหวย

ทางเข้า 123 (not verified)

ทางเข้า 123 Auto deposit-withdraw, no minimum 1 baht, you can make transactions, no need to make a turn

III7X (not verified)

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

ราคาบอลสเต็ป (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

kawin21 (not verified)

เว็บนาซ่า There is a best friend referral system. where you can earn up to 1% commission from your friend's play without having to play even a single baht Sign up for free No minimum deposit-withdrawal Open 24 hours

ราคาบอลสเต็ป (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

MM79 (not verified)

nazago
Leading casino Excited with gambling in online casinos It is becoming popular nowadays. There are pretty, beautiful girls who are always dealing with cards without getting bored. Full of fun, exciting, challenging, comes with a clear HD picture system.

Naza1 (not verified)

28 Football, Slots, Baccarat

sexy baccarat (not verified)

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

123maxx (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

789heng (not verified)

Online slots, fast service Ready to give advice 24 hours a day at 789heng only.สล็อต

buy fincar 5mg cheap (not verified)

Viagra Cialis Unterschied

thanunya (not verified)

เว็บแทงหวย Of course, there must be a difference already as the online gambling game has a simple easy and fun process.

Branzonsap (not verified)

hartfordfree ivermectin dog
errr ivermectin dose for scabies in humans
ivermectin 12mg online
- buy stromectol pills online
[url=http://ivermectin3mg.net/#]ivermectin lotion
[/url] bumili ivermectin tablet

Cafeseast (not verified)

cialis for sale

sdfg (not verified)

Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. aw8

naza (not verified)

สูตรบาคาร่า is the best online gambling sites right now

bill (not verified)

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

Add new comment