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

Raphaelmup (not verified)

electro college boys porn [url=https://collegewebcams00.store]collegewebcams00[/url] college studen pens in ass.

Raphaelmup (not verified)

electro college boys porn [url=https://collegewebcams00.store]collegewebcams00[/url] college studen pens in ass.

Raphaelmup (not verified)

home movies tube mature amateur moms voyer masterbating [url=https://amateurwebcams00.xyz]amateur porn college party [/url] amateur voyeur under boob.

Raphaelmup (not verified)

home movies tube mature amateur moms voyer masterbating [url=https://amateurwebcams00.xyz]amateur porn college party [/url] amateur voyeur under boob.

Raphaelmup (not verified)

chubby amateur teen anal [url=https://amateurwebcams00.monster]x videos real amateur homemade wife dp creampie [/url] amateur nerds with big tits.

Raphaelmup (not verified)

best looking shaved teen pussies [url=https://teenwebcams00.uno]teen girl raped porn [/url] hd teen pussy ass.

Raphaelmup (not verified)

best looking shaved teen pussies [url=https://teenwebcams00.uno]teen girl raped porn [/url] hd teen pussy ass.

Real viagra online (not verified)

Jqicof ulnbql order propecia over the counter erectile dysfunction pills

Raphaelmup (not verified)

american teen amateur fuck [url=https://teenwebcams00.uno]dasha c. teen porn [/url] thais teen bunnies club pussy.

Raphaelmup (not verified)

american teen amateur fuck [url=https://teenwebcams00.uno]dasha c. teen porn [/url] thais teen bunnies club pussy.

Raphaelmup (not verified)

american teen amateur fuck [url=https://teenwebcams00.uno]dasha c. teen porn [/url] thais teen bunnies club pussy.

Raphaelmup (not verified)

amateur drunk college slut secretly filmed naked [url=https://collegewebcams00.store]collegewebcams00[/url] exploited college girls aarin orgasm.

Raphaelmup (not verified)

amateur drunk college slut secretly filmed naked [url=https://collegewebcams00.store]collegewebcams00[/url] exploited college girls aarin orgasm.

Raphaelmup (not verified)

japanese milf getting hardcore fucked [url=https://milfwebcams00.club]nude milf ass up [/url] huge tits milf young man.

Raphaelmup (not verified)

japanese milf getting hardcore fucked [url=https://milfwebcams00.club]nude milf ass up [/url] huge tits milf young man.

Raphaelmup (not verified)

old young first lesbian porn [url=https://lesbianwebcams00.fun]lauren phillips lesbian porn [/url] lesbian schoolgirl scat porn.

Raphaelmup (not verified)

gay black anal freaks [url=https://blackwebcams00.pw]white girl sucks black cock [/url] hefty thick black bbw ass tumblr.

Raphaelmup (not verified)

gay black anal freaks [url=https://blackwebcams00.pw]white girl sucks black cock [/url] hefty thick black bbw ass tumblr.

Raphaelmup (not verified)

gay black anal freaks [url=https://blackwebcams00.pw]white girl sucks black cock [/url] hefty thick black bbw ass tumblr.

Raphaelmup (not verified)

conpilation mature big tits [url=https://maturewebcams00.club]maturewebcams00.club[/url] curvy mature latex lesbo.

Anonymous (not verified)

Recommend Article Article Comments Print ArticleShare this article on FacebookShare this article on TwitterShare this article on LinkedinShare this article on DeliciousShare this article on DiggShare this article on RedditShare this article on PinterestExpert Author Hooman Morvarid water damage repair near me

BrandonPed (not verified)

tiny mature ebony painful anal [url=https://maturewebcams00.club]ebony amature mature nude [/url] amateur mature asian women.

BrandonPed (not verified)

round naked black ass [url=https://blackwebcams00.pw]ass fucking black lesbians [/url] amateur bald ebony thot rides.

BrandonPed (not verified)

round naked black ass [url=https://blackwebcams00.pw]ass fucking black lesbians [/url] amateur bald ebony thot rides.

Buy viagra prof... (not verified)

Kxhiez bcaawk cialis 5 mg pharmacy online

Harveysoync (not verified)

hot milf disciplines naughty teen cheerleader [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] tied and fucked milf.
black teen pussy trimmed [url=https://blackwebcams00.icu]blackwebcams00[/url] black lesbians slave tubes.
milf pussy hd porn video [url=https://milfwebcams00.club]milfwebcams00.club[/url] tumblr slim matures milfs.
black mother white son porn videos [url=https://blackwebcams00.icu]blackwebcams00[/url] black on asia porn.

Harveysoync (not verified)

hot milf disciplines naughty teen cheerleader [url=https://milfwebcams00.casa]milfwebcams00.casa[/url] tied and fucked milf.
black teen pussy trimmed [url=https://blackwebcams00.icu]blackwebcams00[/url] black lesbians slave tubes.
milf pussy hd porn video [url=https://milfwebcams00.club]milfwebcams00.club[/url] tumblr slim matures milfs.
black mother white son porn videos [url=https://blackwebcams00.icu]blackwebcams00[/url] black on asia porn.

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.

BrandonPed (not verified)

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

BrandonPed (not verified)

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

Buy viagra in canada (not verified)

Kamksx ittxip viagra online pharmacy ed pills otc

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.

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)

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.

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.

BrandonPed (not verified)

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

BrandonPed (not verified)

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

BrandonPed (not verified)

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

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.

BrandonPed (not verified)

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

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