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

Thomasgef (not verified)

lesbian teacher seduces pussy eating [url=https://lesbianwebcams01.fun]lesbian ass slurp nude [/url] naked lesbians with there stepmother.

Thomasgef (not verified)

lesbian teacher seduces pussy eating [url=https://lesbianwebcams01.fun]lesbian ass slurp nude [/url] naked lesbians with there stepmother.

Thomasgef (not verified)

amateur homemade handjob cumshot compilation [url=https://amateurwebcams00.xyz]amateurwebcams00[/url] amateur caught dildoing porn.

Thomasgef (not verified)

amateur homemade handjob cumshot compilation [url=https://amateurwebcams00.xyz]amateurwebcams00[/url] amateur caught dildoing porn.

Thomasgef (not verified)

jillian janson dp blacked porn [url=https://blackwebcams00.pw]blackwebcams00[/url] skinny phat ass ebony.

Kennethviort (not verified)

black milf white dick [url=https://milfwebcams00.club]milfwebcams00[/url] small breasted milf pussy.
porn porn black bbw [url=https://blackwebcams00.icu]blackwebcams00[/url] ny wife sucks black gock.
amateur heavy cum creampie [url=https://amateurwebcams00.monster]real amateur young russian [/url] amateur naked women in mountains.
ebony milf with big tits [url=https://milfwebcams00.casa]porn films milf fucking a college guy [/url] milf fucked at work.
hot blonde milf anal [url=https://milfwebcams00.casa]milfwebcams00[/url] 50 year old milf tumblr.

Thomasgef (not verified)

amateur cum shot fascials [url=https://amateurwebcams00.monster]amateurwebcams00[/url] mature amateur creamy orgasm.

Thomasgef (not verified)

amateur cum shot fascials [url=https://amateurwebcams00.monster]amateurwebcams00[/url] mature amateur creamy orgasm.

Thomasgef (not verified)

allblackbbw mature milf fuck [url=https://milfwebcams00.club]teen milf lesbian incest porn [/url] porn films milf fucking a college guy.

Thomasgef (not verified)

teen lesbian taboo sisters [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] ebony woman white girl lesbian vids.

Buy generic via... (not verified)

Rhfahh rzyxmr vardenafil 20 mg

Thomasgef (not verified)

japanese mom and son fuck bb [url=https://asianwebcams00.website]big dick interracial japanese anal [/url] tattooed asian man porn.

Thomasgef (not verified)

japanese mom and son fuck bb [url=https://asianwebcams00.website]big dick interracial japanese anal [/url] tattooed asian man porn.

Thomasgef (not verified)

big milf tits lactate [url=https://milfwebcams00.club]milf likes young cock [/url] over 30 milf tumblr.

Thomasgef (not verified)

big milf tits lactate [url=https://milfwebcams00.club]milf likes young cock [/url] over 30 milf tumblr.

Thomasgef (not verified)

animated gif porn solo milf [url=https://milfwebcams00.casa]xnxx 2 hot milfs [/url] spread bbw milf pussy tits.

Kennethviort (not verified)

half black girls rubbing their pussy [url=https://blackwebcams00.icu]blackwebcams00[/url] ebony sucking white blowjob.
im in lesbians with you porn [url=https://lesbianwebcams00.fun]amatuer lesbian ass licking [/url] teen raped by plant porn lesbian.
black porn star persia [url=https://blackwebcams00.pw]skinny ebony teen pussy [/url] latinas acompanante en miami porn and black dick.
amateur naked voluptuos in water [url=https://amateurwebcams00.monster]stepmom fucking stepson amateur [/url] amateur fit shemale anal creampie.
busty college girls forced [url=https://collegewebcams00.site]college coed nude images [/url] college movie with massage handjob.

Thomasgef (not verified)

retro nude amateur rita lesbian [url=https://amateurwebcams00.monster]amateurwebcams00[/url] amateur frat guy masturbating while girls watch.

Thomasgef (not verified)

retro nude amateur rita lesbian [url=https://amateurwebcams00.monster]amateurwebcams00[/url] amateur frat guy masturbating while girls watch.

Kennethviort (not verified)

half black girls rubbing their pussy [url=https://blackwebcams00.icu]blackwebcams00[/url] ebony sucking white blowjob.
im in lesbians with you porn [url=https://lesbianwebcams00.fun]amatuer lesbian ass licking [/url] teen raped by plant porn lesbian.
black porn star persia [url=https://blackwebcams00.pw]skinny ebony teen pussy [/url] latinas acompanante en miami porn and black dick.
amateur naked voluptuos in water [url=https://amateurwebcams00.monster]stepmom fucking stepson amateur [/url] amateur fit shemale anal creampie.
busty college girls forced [url=https://collegewebcams00.site]college coed nude images [/url] college movie with massage handjob.

Thomasgef (not verified)

ebony mature fucked and killed by white [url=https://maturewebcams00.club]maturewebcams00[/url] real amateur mature naturists couples.

Thomasgef (not verified)

ebony mature fucked and killed by white [url=https://maturewebcams00.club]maturewebcams00[/url] real amateur mature naturists couples.

Kennethviort (not verified)

free milf petite pov [url=https://milfwebcams00.club]milf ready to fuck [/url] milf fuck handjob milking.
amateur pool bikini boobs [url=https://amateurwebcams00.monster]amateurwebcams00.monster[/url] amateur mature wife jerking husband tube.
blonde milf porn pic [url=https://milfwebcams00.club]milf granny eating pussy [/url] taboo milf kristi mom.
amateur mature public bentover fuck [url=https://maturewebcams00.club]maturewebcams00.club[/url] mature woman role reversal porn.
huge tits amateur cam [url=https://amateurwebcams00.monster]amateurwebcams00[/url] anal amateur tiny petite.

Thomasgef (not verified)

mature mom geta fucked by son [url=https://maturewebcams00.club]maturewebcams00[/url] hd euro mature anal.

Anonymous (not verified)

First, find an OB/GYN that you are comfortable with. He/she will tell you where you are in the "weight category". They will develop a nutritional plan for you and what/how often you need to eat. This will ensure the best possible start in life for your baby before and after he/she is born. It will also help you get in the habit of eating healthy and regularly throughout your pregnancy and beyond. keto bulk diet

Thomasgef (not verified)

college girl virgin teen porn [url=https://collegewebcams00.site]college girls naked tumblr [/url] violent college dick suck.

Kennethviort (not verified)

milf teacher porn game [url=https://milfwebcams00.club]milfwebcams00[/url] hot nude brunette milf.
milf seekers red hot firecracker [url=https://milfwebcams00.casa]milfs beautiful legs over 40 [/url] pornxxx milf first anal.
asian small tit pics [url=https://asianwebcams00.website]vintage chinese mothers fucking [/url] little asians porn pictures.
shemale ass raped by black guy videos [url=https://blackwebcams00.pw]beautiful black women nudes [/url] ebony mom teen orgasm anal.
college professor mardi gras nude [url=https://collegewebcams00.store]tube8 college creampie amateur [/url] college class no panties.

Price viagra (not verified)

Sepiio xvedgp order vardenafil medicine erectile dysfunction

Price viagra (not verified)

Sepiio xvedgp order vardenafil medicine erectile dysfunction

Thomasgef (not verified)

college girls sucking pussy [url=https://collegewebcams00.store]collegewebcams00[/url] blonde nude college girls at the beach.

Kennethviort (not verified)

teen and dad fucking videos [url=https://teenwebcams00.uno]teenwebcams00.uno[/url] super young teens getting fucked.
blonde college sister porn [url=https://collegewebcams00.site]collegewebcams00.site[/url] naked college girls anal in college dorms.
nude college petite ass [url=https://collegewebcams00.site]gif fucking the college girl [/url] redtube surprise threesome has college.
blonde carmen college porn [url=https://collegewebcams00.store]collegewebcams00[/url] college girls spring break lesbians.
asian pussy tease bush [url=https://asianwebcams00.website]hairy asian pussy photos [/url] petite spaoy asian massage porn.

Kennethviort (not verified)

teen and dad fucking videos [url=https://teenwebcams00.uno]teenwebcams00.uno[/url] super young teens getting fucked.
blonde college sister porn [url=https://collegewebcams00.site]collegewebcams00.site[/url] naked college girls anal in college dorms.
nude college petite ass [url=https://collegewebcams00.site]gif fucking the college girl [/url] redtube surprise threesome has college.
blonde carmen college porn [url=https://collegewebcams00.store]collegewebcams00[/url] college girls spring break lesbians.
asian pussy tease bush [url=https://asianwebcams00.website]hairy asian pussy photos [/url] petite spaoy asian massage porn.

Thomasgef (not verified)

college girls sucking pussy [url=https://collegewebcams00.store]collegewebcams00[/url] blonde nude college girls at the beach.

Thomasgef (not verified)

young latina porn amateur [url=https://amateurwebcams00.xyz]amateur teen naked selfies [/url] black tren thot amateurs sucking bbc.

Thomasgef (not verified)

young latina porn amateur [url=https://amateurwebcams00.xyz]amateur teen naked selfies [/url] black tren thot amateurs sucking bbc.

Thomasgef (not verified)

young latina porn amateur [url=https://amateurwebcams00.xyz]amateur teen naked selfies [/url] black tren thot amateurs sucking bbc.

Thomasgef (not verified)

ebony milf mature costume [url=https://milfwebcams00.club]milfwebcams00.club[/url] milf mom next door.

Thomasgef (not verified)

ebony milf mature costume [url=https://milfwebcams00.club]milfwebcams00.club[/url] milf mom next door.

Thomasgef (not verified)

reddit porn blowjob amateur [url=https://amateurwebcams00.monster]amateurwebcams00[/url] desparate amateur wives porn casting.

Thomasgef (not verified)

reddit porn blowjob amateur [url=https://amateurwebcams00.monster]amateurwebcams00[/url] desparate amateur wives porn casting.

Buy cheap viagra now (not verified)

Ybwgos wtfmmp kamagra 100mg medicine for erectile

Anonymous (not verified)

This is my own experience. I ran internet business for two years and yet I produced very poor results. I lost more than $20,000 but I couldn't figure out what was the problem. marketing strategies for small business

Raphaelmup (not verified)

amature college porn tryouts [url=https://collegewebcams00.site]interview with naked female college students porno solo [/url] why do college tours suck.

Raphaelmup (not verified)

got to fuck milf mom [url=https://milfwebcams00.club]milfwebcams00[/url] hot milf porn gif.

Raphaelmup (not verified)

got to fuck milf mom [url=https://milfwebcams00.club]milfwebcams00[/url] hot milf porn gif.

Raphaelmup (not verified)

muslim teen girl nude [url=https://teenwebcams00.uno]ugly naked teen pics [/url] cute chubby teen taking nude selfies.

Raphaelmup (not verified)

muslim teen girl nude [url=https://teenwebcams00.uno]ugly naked teen pics [/url] cute chubby teen taking nude selfies.

Raphaelmup (not verified)

muslim teen girl nude [url=https://teenwebcams00.uno]ugly naked teen pics [/url] cute chubby teen taking nude selfies.

Buy viagra cheap (not verified)

Ucgouy npqfso best slots to play online free slots online

Buy viagra cheap (not verified)

Ucgouy npqfso best slots to play online free slots online