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

Raymondwrels (not verified)

big butt matures bending over [url=https://maturewebcams00.space]saggy tit mature pics [/url] mom stockings garter nude mature.

Raymondwrels (not verified)

lesbians licks ass and fart [url=https://lesbianwebcams01.fun]lesbianwebcams01.fun[/url] old lesbians and old couples.

Raymondwrels (not verified)

ebony teens fucks white dick [url=https://teenwebcams00.uno]skinny black teen anal [/url] amatuer teen blowjob gif.

Raymondwrels (not verified)

mature hairy asian women milf [url=https://milfwebcams00.casa]hairy milf anal xhamster [/url] granny milfs porn videos.

Raymondwrels (not verified)

2 amateur wives sucking one dick [url=https://amateurwebcams00.monster]amateurwebcams00[/url] amateur british housewife creampie tube.

Raymondwrels (not verified)

2 amateur wives sucking one dick [url=https://amateurwebcams00.monster]amateurwebcams00[/url] amateur british housewife creampie tube.

Raymondwrels (not verified)

teen fuck ffm college [url=https://collegewebcams00.store]collegewebcams00.store[/url] college teen pussy rip.

Raymondwrels (not verified)

teen fuck ffm college [url=https://collegewebcams00.store]collegewebcams00.store[/url] college teen pussy rip.

Raymondwrels (not verified)

thai hooker anal gets roughed up [url=https://asianwebcams00.website]nude pictures of indian women [/url] tall woman short asian woman porn.

Raymondwrels (not verified)

thai hooker anal gets roughed up [url=https://asianwebcams00.website]nude pictures of indian women [/url] tall woman short asian woman porn.

Raymondwrels (not verified)

two dicks in one ass anal asian [url=https://asianwebcams00.host]asianwebcams00[/url] japanese sissy rough anal cosplay.

Raymondwrels (not verified)

two dicks in one ass anal asian [url=https://asianwebcams00.host]asianwebcams00[/url] japanese sissy rough anal cosplay.

Raymondwrels (not verified)

amateur bff creampie eating [url=https://amateurwebcams00.monster]free amateur pov vids [/url] amateur wife cum gif porn.

Raymondwrels (not verified)

rachel williams black corset nude [url=https://blackwebcams00.icu]huge tits ebony with big ass [/url] best ebony girls and white guy porn.

bisding (not verified)

Lioresal Commander 25mg https://buycialisuss.com/ - buy generic cialis online Overnight Lavitra buy cialis online reddit Cephalexin For Fish Same As Humans

Raymondwrels (not verified)

rachel williams black corset nude [url=https://blackwebcams00.icu]huge tits ebony with big ass [/url] best ebony girls and white guy porn.

Raymondwrels (not verified)

rachel williams black corset nude [url=https://blackwebcams00.icu]huge tits ebony with big ass [/url] best ebony girls and white guy porn.

Raymondwrels (not verified)

real african anal orgy [url=https://blackwebcams00.icu]black girls with big asses [/url] skinny ebony teen slut porn videos.

Raymondwrels (not verified)

amateur naked girls tinder alabama [url=https://amateurwebcams00.xyz]amateurwebcams00[/url] amateur milf nude spreadeagle.

Raymondwrels (not verified)

searching for lesbian massage lexington ky [url=https://lesbianwebcams01.fun]javfor.me - milf japanese lesbians kissing [/url] long tongue lesbian eats pussy.

Raymondwrels (not verified)

searching for lesbian massage lexington ky [url=https://lesbianwebcams01.fun]javfor.me - milf japanese lesbians kissing [/url] long tongue lesbian eats pussy.

Raymondwrels (not verified)

blonde milf young porn [url=https://milfwebcams00.casa]milf begs for creampie [/url] middle aged milf naked.

Raymondwrels (not verified)

uncensored milf japanese tube [url=https://milfwebcams00.casa]milf begs for anal [/url] hairy women and black men milf.

Raymondwrels (not verified)

uncensored milf japanese tube [url=https://milfwebcams00.casa]milf begs for anal [/url] hairy women and black men milf.

Raymondwrels (not verified)

real amateur granny orgasms at casting [url=https://amateurwebcams00.monster]nude amateur mature pussy [/url] real gay amateur redneck videos tumblr.

Raymondwrels (not verified)

amateur girl muscle nude [url=https://amateurwebcams00.xyz]amateurwebcams00.xyz[/url] hidren cam traveker teen amateur pirn.

Raymondwrels (not verified)

hot mature cum facial porn [url=https://maturewebcams00.space]curvy mature blonde fucks tube [/url] mature short hair blonde big naturals hairy creampie.

Raymondwrels (not verified)

hot mature cum facial porn [url=https://maturewebcams00.space]curvy mature blonde fucks tube [/url] mature short hair blonde big naturals hairy creampie.

Raymondwrels (not verified)

hot mature cum facial porn [url=https://maturewebcams00.space]curvy mature blonde fucks tube [/url] mature short hair blonde big naturals hairy creampie.

Raymondwrels (not verified)

hot mature cum facial porn [url=https://maturewebcams00.space]curvy mature blonde fucks tube [/url] mature short hair blonde big naturals hairy creampie.

Raymondwrels (not verified)

lesbian porn sites teens [url=https://teenwebcams00.uno]2 teens nude stickam [/url] nerdy teen girl porn pictures.

Raymondwrels (not verified)

lesbian porn sites teens [url=https://teenwebcams00.uno]2 teens nude stickam [/url] nerdy teen girl porn pictures.

JeromeExcex (not verified)

that teen's got a bushy pussy jennifer [url=https://teenwebcams00.press]teenwebcams00.press[/url] moviesweet young teens fuck.

JeromeExcex (not verified)

ebony wants you to masturbate for her porn [url=https://blackwebcams00.pw]blackwebcams00[/url] nude girl with short black hair.

JeromeExcex (not verified)

petite asian milfs fucking [url=https://asianwebcams00.website]japanese big tits teacher [/url] xxx pussy torture asian.

JeromeExcex (not verified)

asian porn titty lick [url=https://asianwebcams00.host]uncensored japanese forced anal humiliation punishment [/url] asian girl fucks hard and screams.

JeromeExcex (not verified)

hot amateur girlfriend pov [url=https://amateurwebcams00.monster]amateurwebcams00[/url] skinny ebony amateur solo.

JeromeExcex (not verified)

mature beach amateur ass [url=https://amateurwebcams00.monster]barely legal amateur teen [/url] amateur blonde mature pov blowjob.

JeromeExcex (not verified)

mature beach amateur ass [url=https://amateurwebcams00.monster]barely legal amateur teen [/url] amateur blonde mature pov blowjob.

JeromeExcex (not verified)

mature beach amateur ass [url=https://amateurwebcams00.monster]barely legal amateur teen [/url] amateur blonde mature pov blowjob.

JeromeExcex (not verified)

mature granny pov porn [url=https://maturewebcams00.club]amateur mature screaming tube [/url] free hd mature german lesbians.

JeromeExcex (not verified)

mature granny pov porn [url=https://maturewebcams00.club]amateur mature screaming tube [/url] free hd mature german lesbians.

JeromeExcex (not verified)

mature teach a boy porn [url=https://maturewebcams00.club]maturewebcams00[/url] rough mean butt fuck old mature grandpa.

JeromeExcex (not verified)

amateur black mom son cumshots tubes [url=https://amateurwebcams00.xyz]amateurwebcams00[/url] 20 year old tall volleyball girl creampie amateur.

JeromeExcex (not verified)

bbw amateurs grannies longest cock [url=https://amateurwebcams00.xyz]lesbian scat hollywood amateurs [/url] amateur teen open button shirt flashing.

JeromeExcex (not verified)

amateur milf at gloryhole [url=https://amateurwebcams00.monster]amateur homemade teens fucking in public gif [/url] big amateur boobs facebook.

JeromeExcex (not verified)

amateur milf at gloryhole [url=https://amateurwebcams00.monster]amateur homemade teens fucking in public gif [/url] big amateur boobs facebook.

BrianHic (not verified)

where to buy herbal viagra where can i buy liquid viagra buy online com phentermine viagra

BrianHic (not verified)

where to buy viagra generic best place buy generic viagra online where do i buy viagra online

Anonymous (not verified)

But wanna admit that this is very helpful , Thanks for taking your time to write this. buy fentanyl sublingual tablets