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

Harveysoync (not verified)

hairy amateur giant tits [url=https://amateurwebcams00.monster]amateur drunk nude girls images [/url] amateur blonde sucks bbc.
newest and oldest lily thai porn videos [url=https://asianwebcams00.website]asianwebcams00[/url] big tit asian fucked by stud.
mature lady nude in public flashing [url=https://maturewebcams00.club]maturewebcams00[/url] old mature granny nude pics.
twerking college party with beautiful teens porn [url=https://collegewebcams00.site]collegewebcams00.site[/url] new college porn vids clip.

Harveysoync (not verified)

hairy amateur giant tits [url=https://amateurwebcams00.monster]amateur drunk nude girls images [/url] amateur blonde sucks bbc.
newest and oldest lily thai porn videos [url=https://asianwebcams00.website]asianwebcams00[/url] big tit asian fucked by stud.
mature lady nude in public flashing [url=https://maturewebcams00.club]maturewebcams00[/url] old mature granny nude pics.
twerking college party with beautiful teens porn [url=https://collegewebcams00.site]collegewebcams00.site[/url] new college porn vids clip.

Harveysoync (not verified)

black groped white teen in college porn [url=https://collegewebcams00.site]naked college girls small tits tumblr [/url] college guys sleeping naked.
fuck sunrise in chinese [url=https://asianwebcams00.website]asianwebcams00.website[/url] desi fast sex videos.
amateur little sister nude [url=https://amateurwebcams00.monster]amateurwebcams00[/url] mature amateur fucked to huge orgasm.
black men sucking breastmilk nastiest fucking [url=https://blackwebcams00.pw]blackwebcams00.pw[/url] black man cums all over bed porn.

BrandonPed (not verified)

epic threesome with a beautiful college girl [url=https://collegewebcams00.site]college girl drunk in bathtub sex [/url] sexy college girls asses in tight leggings.

BrandonPed (not verified)

hot milf stepmoms anal [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] hot black milf love white dick.

Harveysoync (not verified)

university of florida gainesville amateur porn [url=https://amateurwebcams00.xyz]amateur first time interracial [/url] chubby naked black amateurs.
claxton ga amateur fucks [url=https://amateurwebcams00.monster]amateurwebcams00[/url] amateur natural tits mature.
catwoman naked lesbian sex [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] lesbian life changing fuck.
anirexic teen porn movies [url=https://teenwebcams00.uno]teenwebcams00[/url] euro babes teen sex.

BrandonPed (not verified)

nude amateur ex amanda [url=https://amateurwebcams00.monster]amateurwebcams00[/url] amateur naked saggy boobs.

Harveysoync (not verified)

alex exploited college girls porn video [url=https://collegewebcams00.site]collegewebcams00[/url] how did they film college fuck fest.
nude girlfriend amateur strip [url=https://amateurwebcams00.xyz]amateurwebcams00.xyz[/url] bridgette amateur porn videos.
sexy college coeds party nude spread fuck blow [url=https://collegewebcams00.site]hot college fucking tumblr [/url] mmf college sex gif.
college guys gay porn [url=https://collegewebcams00.store]college amateur tour 3 [/url] dick cheney college dropout.

Buy viagra in canada (not verified)

Kamksx ittxip viagra online pharmacy ed pills otc

Harveysoync (not verified)

southwest college santa fe sex therapy [url=https://collegewebcams00.store]amateur college dorm sex [/url] naked men shower college.
curvy thick teen amateur porn [url=https://amateurwebcams00.monster]amateurwebcams00[/url] crazy amateur nude females uncensored.
nude college girl amateur candid [url=https://collegewebcams00.store]collegewebcams00.store[/url] college blowjob with cum.
light bondage lesbian teens [url=https://lesbianwebcams01.fun]lesbian story porn [/url] free lesbian anal videos.

BrandonPed (not verified)

curvy naked biracial teen spreading [url=https://teenwebcams00.uno]teen shows pussy at club gif [/url] cory chase huge cock teen porn.

BrandonPed (not verified)

curvy naked biracial teen spreading [url=https://teenwebcams00.uno]teen shows pussy at club gif [/url] cory chase huge cock teen porn.

Harveysoync (not verified)

amateur wife fucked by monster cocks [url=https://amateurwebcams00.monster]amateurwebcams00.monster[/url] fucking a amateur milf in the ass.
college ass nude tumblr [url=https://collegewebcams00.site]milf college pussy ass lesbian [/url] fucking college friend melissa.
african tranns anal fucked [url=https://blackwebcams00.icu]creamy wet black pussy [/url] black dildo sucking pov.
adult big tit milfs fucking [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] brazzers ebony milf bath.

Harveysoync (not verified)

amateur wife fucked by monster cocks [url=https://amateurwebcams00.monster]amateurwebcams00.monster[/url] fucking a amateur milf in the ass.
college ass nude tumblr [url=https://collegewebcams00.site]milf college pussy ass lesbian [/url] fucking college friend melissa.
african tranns anal fucked [url=https://blackwebcams00.icu]creamy wet black pussy [/url] black dildo sucking pov.
adult big tit milfs fucking [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] brazzers ebony milf bath.

Harveysoync (not verified)

amateur wife fucked by monster cocks [url=https://amateurwebcams00.monster]amateurwebcams00.monster[/url] fucking a amateur milf in the ass.
college ass nude tumblr [url=https://collegewebcams00.site]milf college pussy ass lesbian [/url] fucking college friend melissa.
african tranns anal fucked [url=https://blackwebcams00.icu]creamy wet black pussy [/url] black dildo sucking pov.
adult big tit milfs fucking [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] brazzers ebony milf bath.

Harveysoync (not verified)

amateur wife fucked by monster cocks [url=https://amateurwebcams00.monster]amateurwebcams00.monster[/url] fucking a amateur milf in the ass.
college ass nude tumblr [url=https://collegewebcams00.site]milf college pussy ass lesbian [/url] fucking college friend melissa.
african tranns anal fucked [url=https://blackwebcams00.icu]creamy wet black pussy [/url] black dildo sucking pov.
adult big tit milfs fucking [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] brazzers ebony milf bath.

Harveysoync (not verified)

tiny tit fat ass asian [url=https://asianwebcams00.website]asianwebcams00.website[/url] hairy asian mom sex with boys.
mature blonde amateur sandra [url=https://maturewebcams00.club]maturewebcams00.club[/url] mature japanese huge black cock.
black older mother mature amature gets creampied [url=https://maturewebcams00.club]maturewebcams00[/url] huge tit mature fucks.
nude teen lesbians outdoors [url=https://lesbianwebcams00.fun]furry porn lesbian [/url] lesbians huge tits porn.

BrandonPed (not verified)

spy nude college guys video [url=https://collegewebcams00.site]drunk college girl vids on tumblr [/url] amature college men best friend fuck.

Harveysoync (not verified)

tiny tit fat ass asian [url=https://asianwebcams00.website]asianwebcams00.website[/url] hairy asian mom sex with boys.
mature blonde amateur sandra [url=https://maturewebcams00.club]maturewebcams00.club[/url] mature japanese huge black cock.
black older mother mature amature gets creampied [url=https://maturewebcams00.club]maturewebcams00[/url] huge tit mature fucks.
nude teen lesbians outdoors [url=https://lesbianwebcams00.fun]furry porn lesbian [/url] lesbians huge tits porn.

BrandonPed (not verified)

Japanese Pantyhose Sex hd Movie [url=https://asianwebcams00.website]japanese small tit nude [/url] hot chinese pussy spreading.

Harveysoync (not verified)

hot amateur gets her tits played with [url=https://amateurwebcams00.monster]amateurwebcams00[/url] free porn anal amateur.
college dick bulge weight bench [url=https://collegewebcams00.store]collegewebcams00[/url] college bpys pound ass tumblr.
arab teen girl blowjob [url=https://teenwebcams00.uno]big tits teen nude [/url] big tit brunettes teens fucking.
freaky amateur wife keeps cumming [url=https://amateurwebcams00.xyz]amateur sub wife bdsm gang bang [/url] tiny teen amateur tumblr.

BrandonPed (not verified)

Japanese Pantyhose Sex hd Movie [url=https://asianwebcams00.website]japanese small tit nude [/url] hot chinese pussy spreading.

Harveysoync (not verified)

hot amateur gets her tits played with [url=https://amateurwebcams00.monster]amateurwebcams00[/url] free porn anal amateur.
college dick bulge weight bench [url=https://collegewebcams00.store]collegewebcams00[/url] college bpys pound ass tumblr.
arab teen girl blowjob [url=https://teenwebcams00.uno]big tits teen nude [/url] big tit brunettes teens fucking.
freaky amateur wife keeps cumming [url=https://amateurwebcams00.xyz]amateur sub wife bdsm gang bang [/url] tiny teen amateur tumblr.

BrandonPed (not verified)

big ass black woman videos [url=https://blackwebcams00.icu]ebony teen porn tubes [/url] stright acting black gay porn videos.

Harveysoync (not verified)

amateur teen double teamed [url=https://amateurwebcams00.xyz]amateurwebcams00[/url] amateur teens rough anal.
strapon lesbian bizarre xxx [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] fufu lesbian threesome amateur.
ebony fat ass sex gif [url=https://blackwebcams00.icu]blackwebcams00[/url] meetup for black lesbians in georgia.
amateur real mom and daughter [url=https://amateurwebcams00.xyz]amateur interracial wives free pictures [/url] beautiful amateur lesbians sex naked.

BrandonPed (not verified)

big ass black woman videos [url=https://blackwebcams00.icu]ebony teen porn tubes [/url] stright acting black gay porn videos.

Harveysoync (not verified)

xxx young women enjoying lesbian lust [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] porn elena koshka lesbian.
amature lesbian porn gif [url=https://lesbianwebcams01.fun]lesbian daughter takes moms pussy xxx videos [/url] big natural boobed threesome lesbian anal.
asian amateur milf cumshot [url=https://amateurwebcams00.xyz]sexy amateur pussy selfies [/url] amateur teen oral pov gif.
diana blake lesbian full videos free [url=https://lesbianwebcams01.fun]lesbian porn with toys [/url] naked hispanic lesbian gifs.

BrandonPed (not verified)

amateur wife first big cock [url=https://amateurwebcams00.xyz]amateur black shemales fucking guys [/url] amateur nudist grandmas big tits.

Harveysoync (not verified)

xxx young women enjoying lesbian lust [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] porn elena koshka lesbian.
amature lesbian porn gif [url=https://lesbianwebcams01.fun]lesbian daughter takes moms pussy xxx videos [/url] big natural boobed threesome lesbian anal.
asian amateur milf cumshot [url=https://amateurwebcams00.xyz]sexy amateur pussy selfies [/url] amateur teen oral pov gif.
diana blake lesbian full videos free [url=https://lesbianwebcams01.fun]lesbian porn with toys [/url] naked hispanic lesbian gifs.

BrandonPed (not verified)

amateur wife first big cock [url=https://amateurwebcams00.xyz]amateur black shemales fucking guys [/url] amateur nudist grandmas big tits.

Harveysoync (not verified)

naked milfs sex nextdoor son [url=https://milfwebcams00.casa]milfwebcams00[/url] milf panty ass satin.
standing 69 lesbian porn [url=https://lesbianwebcams00.fun]lesbians fingering pussy gif [/url] lesbian fingering fucking videos.
lesbian teen seduces milf xxx [url=https://milfwebcams00.casa]fat ass milf pics [/url] milf loves black xxx.
new indian fuck videos [url=https://asianwebcams00.website]asian pussy and ass [/url] busty asian schoolgirls getting fucked hard.

BrandonPed (not verified)

college rules glasses porn prank [url=https://collegewebcams00.site]interview with naked female college students porno solo [/url] college move in day fuck porn.

BrandonPed (not verified)

college rules glasses porn prank [url=https://collegewebcams00.site]interview with naked female college students porno solo [/url] college move in day fuck porn.

Harveysoync (not verified)

teen girl gives boy blowjob [url=https://teenwebcams00.uno]teenwebcams00[/url] chubby teen bush nudes.
30 year old milfs [url=https://milfwebcams00.club]milf hookup sex tumblr [/url] amateur milf naked photos.
straight girls paid for lesbian sex [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] wife and sister have lesbian sex.
dorothy black lesbian mistress [url=https://blackwebcams00.pw]blackwebcams00[/url] tori black Katja Kassin nude.

BrandonPed (not verified)

amateur mature porn calendar auditions [url=https://amateurwebcams00.xyz]amateur bound naked blowjob [/url] wet amateur asian pussy.

Harveysoync (not verified)

teen girl gives boy blowjob [url=https://teenwebcams00.uno]teenwebcams00[/url] chubby teen bush nudes.
30 year old milfs [url=https://milfwebcams00.club]milf hookup sex tumblr [/url] amateur milf naked photos.
straight girls paid for lesbian sex [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] wife and sister have lesbian sex.
dorothy black lesbian mistress [url=https://blackwebcams00.pw]blackwebcams00[/url] tori black Katja Kassin nude.

phefewedy (not verified)

viagra super activo [url=https://bbuycialisss.com/]tadalafil cialis from india[/url] Amoxicillin Dosage For Sinus Infection Buy Cialis Super Priligy

phefewedy (not verified)

viagra super activo [url=https://bbuycialisss.com/]tadalafil cialis from india[/url] Amoxicillin Dosage For Sinus Infection Buy Cialis Super Priligy

Viagra or cialis (not verified)

Wibdxg ugdbjm online viagra prescription non prescription ed pills

phefewedy (not verified)

viagra super activo [url=https://bbuycialisss.com/]tadalafil cialis from india[/url] Amoxicillin Dosage For Sinus Infection Buy Cialis Super Priligy

phefewedy (not verified)

viagra super activo [url=https://bbuycialisss.com/]tadalafil cialis from india[/url] Amoxicillin Dosage For Sinus Infection Buy Cialis Super Priligy

Buy viagra prof... (not verified)

Ttrxnz afzole real casino online big fish casino online

Buy cheap viagr... (not verified)

Ooeebq ufowqj betfair casino online online casinos for usa players

Buy cheap viagr... (not verified)

Ooeebq ufowqj betfair casino online online casinos for usa players

shahhmeerkhan (not verified)

when i am watching action movies, i really love to see bruce willis in it; improve golf swing

Pfizer viagra 5... (not verified)

Sgprvh luslrx viagra viagra Real viagra pharmacy prescription

Drug viagra (not verified)

Aaapng wmdfor online payday loans no credit check pay day loans

Order viagra usa (not verified)

Uxyuzz syfjdy play casino online hollywood casino online

Viagra overnight (not verified)

Ipdmzd lzyztj cash payday loans online

Sample viagra (not verified)

Vtdovb bwikiv tadalafil 20mg cvs pharmacy

Add new comment