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

DonaldUntom (not verified)

lesbian cam girls [url=https://camgirls1.com]teen girls live cam [/url] cam girl vids.
best sex cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] social webcam sites.
web cam sex [url=https://bestonlinesexwebcams.com]1 on 1 sex cam [/url] first date sex hidden cam.
porn cam sites [url=https://adultfreewebcamsites.com]free adult webcam sites [/url] mobile cam sites.
cam to cam site [url=https://adultcamsites1.com]adultcamsites1.com[/url] teen cam site.
sex cam free [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex live cam.

RichardTub (not verified)

cam free sex [url=https://bestonlinesexcams1.com]young webcam sex [/url] free sex cam site.
cam girl [url=https://camgirls1.com]black girls cam [/url] pregnant cam girl.
webcam model [url=https://hotcamgirls1.com]big tit cam girls fucking [/url] big tit cam girls fucking.

RichardTub (not verified)

cam free sex [url=https://bestonlinesexcams1.com]young webcam sex [/url] free sex cam site.
cam girl [url=https://camgirls1.com]black girls cam [/url] pregnant cam girl.
webcam model [url=https://hotcamgirls1.com]big tit cam girls fucking [/url] big tit cam girls fucking.

Stevenken (not verified)

cam whores [url=https://hotcamgirls1.com]girls cam [/url] girls on web cam.

DonaldUntom (not verified)

teen cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] teen hidden cam sex.
free cam to cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] best cam site for couples.
couple cam sites [url=https://adultfreewebcamsites.com]cam site [/url] best cam sites to make money.
milf sex cam [url=https://bestonlinesexcams1.com]college sex hidden cam [/url] free ebony sex cam.
asian cam site [url=https://adultcamsites1.com]adultcamsites1[/url] eagle cam sites.
sexcams [url=https://bestonlinesexcams1.com]real webcam sex [/url] webcam live sex.

RichardTub (not verified)

top adult webcam sites [url=https://adultfreewebcamsites.com]free adult cam sites [/url] cam girl sites.
cam sex live [url=https://bestonlinesexcams1.com]bongo cam sex [/url] amateur webcam sex.
web cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] chat cam site.

RichardTub (not verified)

top adult webcam sites [url=https://adultfreewebcamsites.com]free adult cam sites [/url] cam girl sites.
cam sex live [url=https://bestonlinesexcams1.com]bongo cam sex [/url] amateur webcam sex.
web cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] chat cam site.

RichardTub (not verified)

top adult webcam sites [url=https://adultfreewebcamsites.com]free adult cam sites [/url] cam girl sites.
cam sex live [url=https://bestonlinesexcams1.com]bongo cam sex [/url] amateur webcam sex.
web cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] chat cam site.

Get viagra fast (not verified)

Scnrrf gsjpao rx pharmacy online viagra Buy viagra overnight delivery

AndrewHaf (not verified)

real life cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] real sex caught on spy cam.

AndrewHaf (not verified)

real life cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] real sex caught on spy cam.

Williamplada (not verified)

asian cam girls [url=https://hotcamgirls1.com]camgirls [/url] romanian girls nude.
webcamming [url=https://hotcamgirls1.com]top webcam girl [/url] cam girl show.
webcam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] best adult webcam sites.
web cam girls [url=https://camgirls1.com]camgirls1.com[/url] girl strip webcam.
top cam sites [url=https://adultcamsites1.com]live webcam sites [/url] teen webcam sites.
top webcam sites [url=https://adultcamsites1.com]top webcam sites [/url] cam sites that work on android.

Davidbiamp (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] gay cam sex.
random sex cam [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] sex cam room.
adult web cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] chatterbait cam site.

AndrewHaf (not verified)

random sex cam [url=https://bestonlinesexcams1.com]sexy cam girl [/url] live free webcam sex.

Viagra alternative (not verified)

Ozdcfn eomlyz Buy generic viagra online Real viagra

Viagra alternative (not verified)

Ozdcfn eomlyz Buy generic viagra online Real viagra

Tyronewer (not verified)

webcam sex videos [url=https://bestonlinesexwebcams.com]video cam sex [/url] cam sex videos.

Ronaldbuigh (not verified)

young girls on cam [url=https://hotcamgirls1.com]cam girls porn [/url] latina cam girls.
live cam sex video [url=https://bestonlinesexcams1.com]free live cam sex [/url] free sexy cam girls.
cam to cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] real sex cam.
spy cam sex [url=https://bestonlinesexcams1.com]free web cam sex [/url] sex cam site.
web cam girls [url=https://camgirls1.com]hot cam girls [/url] latina cam girls.
webcam sex live [url=https://bestonlinesexwebcams.com]sex cam app [/url] arab hidden cam sex.

JohnieOmisa (not verified)

teen cam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] webcam sex free.
web cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] adult cam to cam sites.
free cam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] live girls cam.

smithseo (not verified)

Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy best CBD oil

JohnieOmisa (not verified)

teen cam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] webcam sex free.
web cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] adult cam to cam sites.
free cam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] live girls cam.

Michaelnon (not verified)

top adult cam sites [url=https://adultcamsites1.com]list of cam sites [/url] sexy webcam sites.

Normanbon (not verified)

free cam girl sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] fetish cam site.
best cam sites [url=https://adultcamsites1.com]top adult cam sites [/url] online cam sites.
free porn cam sites [url=https://adultfreewebcamsites.com]safe cam sites [/url] korean cam site.
mfc cam site [url=https://adultcamsites1.com]gay webcam sites [/url] free live webcam sites.
most popular cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] free nude webcam sites.
best sex cam sites [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] indian webcam sex.

Buy viagra canada (not verified)

Djbxcn ocnsvl Overnight viagra Buy generic viagra

Michaelnon (not verified)

asian cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] real life cam sex.

Normanbon (not verified)

young girls on cam [url=https://hotcamgirls1.com]free cam chat with girls [/url] redhead cam girls.
top adult webcam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] free cam site.
live cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] best chat cam sites.
camgirl [url=https://camgirls1.com]cam model [/url] cam girl site.
cam girls [url=https://hotcamgirls1.com]young cam girls [/url] hairy cam girls.
top cam girl sites [url=https://adultcamsites1.com]cheap cam sites [/url] reddit best cam sites.

Buy generic viagra (not verified)

Nsrxhx ampmqf Female viagra Best price for generic viagra

WayneREORP (not verified)

free gay webcam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] korean cam site.

Jamesheelp (not verified)

camgirls [url=https://hotcamgirls1.com]naked girls web cam [/url] japanese cam girls.
live cam sex [url=https://bestonlinesexwebcams.com]webcam sex videos [/url] 100 free sex cam.
free cam girl sites [url=https://adultcamsites1.com]adultcamsites1[/url] cam sites that work on android.
live cam girls [url=https://camgirls1.com]naked cam girls [/url] cam 4 girls.
lesbian cam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] cam 4 girls.
sex cam site [url=https://adultfreewebcamsites.com]best paying cam sites [/url] most popular webcam sites.

Robinjack (not verified)

I real pleased to find this site on bing, just what I was looking for : D as well saved to my bookmarks . Games Online

Viagra jelly (not verified)

Ponrab bmkqnl Viagra 100mg england Buy viagra overnight delivery

Viagra jelly (not verified)

Ponrab bmkqnl Viagra 100mg england Buy viagra overnight delivery

zeeshanarain (not verified)

Perfect work you have done, this website is really cool with excellent information. amber park price

zeeshanarain (not verified)

Hi there, just became alert to your blog through Google, and found that it’s really informative. I am going to watch out for brussels. I¡¦ll be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers! treasure at tampines

zeeshanarain (not verified)

Can I just now say exactly what a relief to uncover one who truly knows what theyre preaching about online. You definitely understand how to bring a challenge to light making it crucial. Lots more people ought to check this out and understand this side of the story. I cant think youre not more well-known simply because you definitely have the gift. buy fake passport online

zeeshanarain (not verified)

Oh my goodness! a fantastic article dude. Appreciate it However My business is experiencing problem with ur rss . Don’t know why Unable to sign up to it. Possibly there is anybody getting identical rss difficulty? Anybody who knows kindly respond. Thnkx buy fake euroe notes in scotland

zeeshanarain (not verified)

I am happy that I detected this blog , precisely the right information that I was looking for! . buy fake passport online

Fda approved viagra (not verified)

Ogvepu eqtpma Buy viagra with discount Canadian viagra and healthcare

Drug viagra (not verified)

Jegqjb fvdmco Buy pfizer viagra online Buy brand viagra

CharlesDwert (not verified)

best cam site [url=https://adultcamsites1.com]sex cam site [/url] teenage webcam sites.

CharlesDwert (not verified)

best cam site [url=https://adultcamsites1.com]sex cam site [/url] teenage webcam sites.

CharlesDwert (not verified)

best cam site [url=https://adultcamsites1.com]sex cam site [/url] teenage webcam sites.

Anonymous (not verified)

your blog is so uncomplicated to read, i like this article, so maintain posting much more trapeze swings

Viagra best buy (not verified)

Pnunmi alnltt generic viagra buy online 50mg viagra

Estherchera (not verified)

Vuramk bxosoz sildenafil tablets Canadian viagra and healthcare

smithseo (not verified)

Youre so cool! I dont suppose Ive learn something like this before. So nice to find any individual with some authentic thoughts on this subject. realy thanks for beginning this up. this web site is one thing that is wanted on the internet, somebody with a bit originality. useful job for bringing something new to the internet! CBD oil for anxiety

Anonymous (not verified)

Decent blog page! That i absolutely love the simplest way it’s easy on my own view as well as information and facts are well written. Now i’m wanting to know earn money may perhaps be warned whenever a new place has been produced. I have subscribed to your own rss feed which inturn have to the secret! Possess a good daytime! buy clonazepam tablets online

zeeshanarain (not verified)

After study some of the content in your web site now, and i also really much like your technique of blogging. I bookmarked it to my bookmark web site list and will also be checking back soon. Pls take a look at my internet site as well and told me what you believe. tks paint equipment

Estherchera (not verified)

Lpfqxf fdtngt Viagra order Brand viagra over the net

Arthurwed (not verified)

webcam group sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] random sex cam chat.

Add new comment