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

CraigFet (not verified)

free cam sites [url=https://adultfreewebcamsites.com]xxx cam sites [/url] free nude webcam sites.

zeeshanarain (not verified)

my dad is a massage therapist and he can really relieve minor pains and injuries’ The Landmark Showflat

RobertCor (not verified)

teen cam girls [url=https://camgirls1.com]hot teen cam girls [/url] naked girls cam.
best free webcam sites [url=https://adultcamsites1.com]best paying webcam sites [/url] best random cam sites.
couple cam site [url=https://adultcamsites1.com]adultcamsites1[/url] sexy webcam sites.
cammodels [url=https://camgirls1.com]cammodel [/url] live web cam girls.
web cam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] naked cam girls.
couple cam sites [url=https://adultfreewebcamsites.com]top adult cam sites [/url] best chat cam sites.

QuentinPex (not verified)

filipina sex cam [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] sex cam with strangers.
webcam girls [url=https://camgirls1.com]camgirls1.com[/url] view cam of nude girls.
ebony live sex cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex cam women.

Anonymous (not verified)

of course we need to know our family history so that we can share it to our kids.. Kent Ridge Hill Residences Floor Plan

Howardchoob (not verified)

live cam girls [url=https://camgirls1.com]camgirls1[/url] hd cam girls.
sex webcam sites [url=https://adultcamsites1.com]sexy webcam sites [/url] best adult cam sites.
camgirls [url=https://camgirls1.com]girls masturbating webcams [/url] girl cam chat.

RichardReemi (not verified)

best cam girl sites [url=https://camgirls1.com]camgirls1.com[/url] black cam girls.
nude cam girls [url=https://camgirls1.com]camgirls1[/url] big boob cam girls.
cam sites free [url=https://adultfreewebcamsites.com]live webcam sites [/url] top web cam site.
amateur webcam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] chaturbate sex cam.
webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] hidden spy cam sex.
chat cam girl [url=https://camgirls1.com]girls cams [/url] cam girl websites.

RichardReemi (not verified)

best cam girl sites [url=https://camgirls1.com]camgirls1.com[/url] black cam girls.
nude cam girls [url=https://camgirls1.com]camgirls1[/url] big boob cam girls.
cam sites free [url=https://adultfreewebcamsites.com]live webcam sites [/url] top web cam site.
amateur webcam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] chaturbate sex cam.
webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] hidden spy cam sex.
chat cam girl [url=https://camgirls1.com]girls cams [/url] cam girl websites.

Generic viagra us (not verified)

Fubquh hmoxvq generic viagra for sale online 50mg viagra

Howardchoob (not verified)

webcam chat sites without registering [url=https://adultfreewebcamsites.com]mobile cam sites [/url] free gay cam site.
ebony live sex cam [url=https://bestonlinesexwebcams.com]my sex cam [/url] real spy cam sex.
gay webcam sites [url=https://adultfreewebcamsites.com]webcam sites adult [/url] couples webcam sites.

RichardReemi (not verified)

cam to cam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] sex web cam sites.
live free sex cam [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] family sex webcam.
free sex cam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] best cam to cam site.
cam free sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex webcam.
adult cam site [url=https://adultcamsites1.com]adultcamsites1.com[/url] best porn webcam sites.
sexy cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex cam gay.

RichardReemi (not verified)

live cam sex [url=https://bestonlinesexcams1.com]free webcam sex [/url] homemade webcam sex.
free live sex cam [url=https://bestonlinesexwebcams.com]hidden cam dorm sex [/url] best sex cam.
asian sex cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] hidden cam sex clips.
adult web cam sites [url=https://adultcamsites1.com]porn cam sites [/url] best nude cam sites.
cam sex live [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] lesbian webcam sex.
gay sex cam [url=https://bestonlinesexcams1.com]sex cam free [/url] gay sex cam chat.

Howardchoob (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] my sex cam.
web cam sites [url=https://adultcamsites1.com]couples cam site [/url] top web cam sites.
asian cam site [url=https://adultfreewebcamsites.com]live cam site [/url] adult webcam sites.

Howardchoob (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] my sex cam.
web cam sites [url=https://adultcamsites1.com]couples cam site [/url] top web cam sites.
asian cam site [url=https://adultfreewebcamsites.com]live cam site [/url] adult webcam sites.

Howardchoob (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] my sex cam.
web cam sites [url=https://adultcamsites1.com]couples cam site [/url] top web cam sites.
asian cam site [url=https://adultfreewebcamsites.com]live cam site [/url] adult webcam sites.

Howardchoob (not verified)

free live cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] live cam sex.
live webcam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] home sex cam.
cheapest cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] asian webcam sites.

Howardchoob (not verified)

free live cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] live cam sex.
live webcam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] home sex cam.
cheapest cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] asian webcam sites.

RichardReemi (not verified)

couple cam site [url=https://adultcamsites1.com]adultcamsites1[/url] webcam sites for couples.
hot sex cam [url=https://bestonlinesexcams1.com]dad daughter webcam sex [/url] desi hidden cam sex.
free webcam sites [url=https://adultcamsites1.com]online webcam sites [/url] webcam sites reviews.
top adult cam sites [url=https://adultfreewebcamsites.com]sex cam site [/url] free adult web cam sites.
free live cam sites [url=https://adultfreewebcamsites.com]best cam sites for couples [/url] free adult web cam sites.
sex cam site [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] cam sites like chaturbate.

zeeshanarain (not verified)

Harry Potter is the best, i really like Daniel Radcliffe because he acts very well,, The Linq Showflat

zeeshanarain (not verified)

Audio started playing as soon as I opened up this website, so frustrating! midtown modern gucco

Stevenken (not verified)

teen webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] chat cam sex.

Stevenken (not verified)

teen webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] chat cam sex.

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.

Add new comment