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

MatthewTwedy (not verified)

amateur vanessa latina -bella -milf -blake -sky -bebe [url=https://amateurwebcams00.monster]amateurwebcams00[/url] real amateur bachelor party.

MatthewTwedy (not verified)

tall skinny milf anal creampie [url=https://milfwebcams00.casa]hd milf threesome porn tubes [/url] hot short hair milf.

Estherchera (not verified)

Ruwwol llfrbw Original brand cialis canadian pharmacy online

MatthewTwedy (not verified)

matures peg for cum porn [url=https://maturewebcams00.club]elegant mature anal porn [/url] real homemade amateur mature mom.

MatthewTwedy (not verified)

thick busty country milf gets ass fucked [url=https://milfwebcams00.club]hairy chubby milf tumblr [/url] anna lee asian milf.

MatthewTwedy (not verified)

skinny grandpa with bbw ebony black teen porn [url=https://teenwebcams00.uno]teenwebcams00.uno[/url] free teen shemale porn vids.

MatthewTwedy (not verified)

full nude women amateur home pics [url=https://amateurwebcams00.monster]amateurwebcams00[/url] real amateur first handjob.

MatthewTwedy (not verified)

college shows off ass [url=https://collegewebcams00.store]collegewebcams00.store[/url] college girl coerced into fucking.

MatthewTwedy (not verified)

college shows off ass [url=https://collegewebcams00.store]collegewebcams00.store[/url] college girl coerced into fucking.

MatthewTwedy (not verified)

full college busty porn [url=https://collegewebcams00.site]collegewebcams00.site[/url] teens fuck at college.

MatthewTwedy (not verified)

milfs smoking and fucking [url=https://milfwebcams00.casa]milfwebcams00[/url] naked milf before after pregnancy tumblr.

Estherchera (not verified)

Knzchb krctrf Get cialis walmart pharmacy

MatthewTwedy (not verified)

drunk college girl cum shot [url=https://collegewebcams00.store]naked college men cum [/url] cute college dress tits.

MatthewTwedy (not verified)

drunk college girl cum shot [url=https://collegewebcams00.store]naked college men cum [/url] cute college dress tits.

MatthewTwedy (not verified)

krystal boyd wild new year college orgy [url=https://collegewebcams00.store]russian college girl blowjob [/url] naked college gurls alayna.

MatthewTwedy (not verified)

the anal milf and the pool boy cashmere [url=https://milfwebcams00.club]milfwebcams00[/url] black milf ass fuck.

MatthewTwedy (not verified)

the anal milf and the pool boy cashmere [url=https://milfwebcams00.club]milfwebcams00[/url] black milf ass fuck.

MatthewTwedy (not verified)

real amateur drunk sluts [url=https://amateurwebcams00.xyz]amateurwebcams00.xyz[/url] tumblr watching her fuck amateur real.

Thomasgef (not verified)

teen breasts nude ass sideboob uncensored [url=https://teenwebcams00.uno]teen glasses small tits porn tubes [/url] big toy anal small blue haired teen.

Thomasgef (not verified)

teen breasts nude ass sideboob uncensored [url=https://teenwebcams00.uno]teen glasses small tits porn tubes [/url] big toy anal small blue haired teen.

Thomasgef (not verified)

busty milf rhyse richards galleries [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] milf dana hayes nude anal white guy.

Thomasgef (not verified)

free hairy milf blowjob [url=https://milfwebcams00.casa]milfwebcams00[/url] shaved milf wife creampies.

Thomasgef (not verified)

gloria vintage porn actress black gloria todd [url=https://blackwebcams00.icu]black cock and white pussy xxx [/url] black big ock porn hd.

Thomasgef (not verified)

gloria vintage porn actress black gloria todd [url=https://blackwebcams00.icu]black cock and white pussy xxx [/url] black big ock porn hd.

Thomasgef (not verified)

pregnant black bbw naked [url=https://blackwebcams00.icu]huge ebony boobs solo [/url] big ebony tits flash.

Thomasgef (not verified)

lesbian eat pussy ass [url=https://lesbianwebcams01.fun]ol young lesbians porno [/url] videos of lesbian having an orgasam.

Thomasgef (not verified)

female video game lesbian feet naked sfm [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] thick ass lesbian milfs clips.

Thomasgef (not verified)

female video game lesbian feet naked sfm [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] thick ass lesbian milfs clips.

Thomasgef (not verified)

female video game lesbian feet naked sfm [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] thick ass lesbian milfs clips.

Pfizer viagra (not verified)

Tpybgh gmujka levitra 10 mg

Pfizer viagra (not verified)

Tpybgh gmujka levitra 10 mg

Pfizer viagra (not verified)

Tpybgh gmujka levitra 10 mg

Pfizer viagra (not verified)

Tpybgh gmujka levitra 10 mg

Kennethviort (not verified)

hot college girls nude videos [url=https://collegewebcams00.store]photos naked college women [/url] college girl convinced to fuck.
hot teen asian teen porn stars [url=https://asianwebcams00.website]asianwebcams00[/url] big titted asian fucks black.
black dick suck gif [url=https://blackwebcams00.icu]tia ling black guy anal [/url] hogtied barefoot white girls sucking black monster cocks.
muscle asses black men [url=https://blackwebcams00.pw]blackwebcams00[/url] big black juicy-fat-ass shemale bareback.
milfs 50 year olds [url=https://milfwebcams00.club]naked curvy milf spreading [/url] hot milf diaries tubes.

Thomasgef (not verified)

black asian girls naked [url=https://blackwebcams00.icu]blackwebcams00.icu[/url] black girl porn stream.

Thomasgef (not verified)

big boob lesbian milking milf [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] hot lesbian tongue kissing gif.

Thomasgef (not verified)

lesbian forced to fuck on x videos [url=https://lesbianwebcams00.fun]lesbianwebcams00.fun[/url] teen lesbians anal strapon.

Thomasgef (not verified)

teen lesbian eats ass in public [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] step daughter strapon lesbian.

Thomasgef (not verified)

top chinese war movies top porn movies [url=https://asianwebcams00.website]asianwebcams00.website[/url] gay indian men fuck hard.

Kennethviort (not verified)

japanese friend mother fuck [url=https://asianwebcams00.website]asianwebcams00.website[/url] uncensored images of nude japanese mothers.
amateur ffm cumshot compilation [url=https://amateurwebcams00.xyz]amateurwebcams00[/url] teen amateur redhead porn forcee.
uncensored japanese lesbians whipping porn [url=https://lesbianwebcams00.fun]lesbianwebcams00.fun[/url] lesbian lily labeau college teen kissing.
amature wife sucks black cock [url=https://blackwebcams00.pw]blackwebcams00.pw[/url] ebony with massive boobs.
amateur big cock guy fuck close up [url=https://amateurwebcams00.monster]amateurwebcams00.monster[/url] amateur swingers porn xnxx.

Thomasgef (not verified)

college teacher and student xxx [url=https://collegewebcams00.site]bowdoin college women go nude [/url] how to get fucked in college.

Kennethviort (not verified)

drunk college girls fucks several guys at once [url=https://collegewebcams00.store]collegewebcams00.store[/url] nude college art class.
college hunks fucking whore on noisy bed [url=https://collegewebcams00.site]collegewebcams00.site[/url] grope naked college titties.
young hot milf gif [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] 30 year old milf quickie movies.
busty lusty milfs cast [url=https://milfwebcams00.club]asian milf fucked hard [/url] big tits milf gif.
amateur crossdresser loving black cock [url=https://amateurwebcams00.xyz]amateur wife porn videos [/url] tonya amateur blowjob queen.

Thomasgef (not verified)

i wanna be a lesbian video [url=https://lesbianwebcams00.fun]amataur real life lesbians making lesbians orgasm clit licks [/url] drunk milf lesbian lick pussy.

Thomasgef (not verified)

i wanna be a lesbian video [url=https://lesbianwebcams00.fun]amataur real life lesbians making lesbians orgasm clit licks [/url] drunk milf lesbian lick pussy.

Thomasgef (not verified)

lesbian fucks big cock has orgasm [url=https://lesbianwebcams00.fun]free lesbian strapon riding [/url] real celebrity lesbian porn videos.

Thomasgef (not verified)

video threesome going off to county amateur [url=https://amateurwebcams00.monster]amateur black cock suck [/url] amateur chubby ugly porn photos.

Thomasgef (not verified)

video threesome going off to county amateur [url=https://amateurwebcams00.monster]amateur black cock suck [/url] amateur chubby ugly porn photos.

Thomasgef (not verified)

big boobs no bra amateur [url=https://amateurwebcams00.monster]real amateur bi mature porn [/url] nude amateur websites men.

Viagra in usa (not verified)

Eopmsq yymbyw vardenafil coupon

Thomasgef (not verified)

black saggy amateur costa rica [url=https://blackwebcams00.pw]tiffany black girl analized [/url] tumblr naked black man crossing legs.