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

Davidler (not verified)

no sign up sex cams [url=https://sexcams00.live]sexcams00.live[/url] sex video cams couples.
free local sex cams [url=https://sexcams00.website]arizona sex cams [/url] free sex cams for couples live.
free russian sex cams [url=https://sexcams00.work]free live amateur sex cams [/url] free sex web cams.
free amateur sex cams [url=https://sexcams00.site]streamate sex cams [/url] first date sex cams.
top free sex cams [url=https://sexcams00.space]sexcams00.space[/url] amateur honeymoon cum sex cams.
live sex cams asian [url=https://sexcams00.club]sexcams00.club[/url] sex free cams.
free live mobile sex cams [url=https://sexcams00.online]no sign up sex cams [/url] underage sex cams.
all free sex cams [url=https://sexcams00.xyz]sexcams00.xyz[/url] young having sex at home on cams.

Davidler (not verified)

no sign up sex cams [url=https://sexcams00.live]sexcams00.live[/url] sex video cams couples.
free local sex cams [url=https://sexcams00.website]arizona sex cams [/url] free sex cams for couples live.
free russian sex cams [url=https://sexcams00.work]free live amateur sex cams [/url] free sex web cams.
free amateur sex cams [url=https://sexcams00.site]streamate sex cams [/url] first date sex cams.
top free sex cams [url=https://sexcams00.space]sexcams00.space[/url] amateur honeymoon cum sex cams.
live sex cams asian [url=https://sexcams00.club]sexcams00.club[/url] sex free cams.
free live mobile sex cams [url=https://sexcams00.online]no sign up sex cams [/url] underage sex cams.
all free sex cams [url=https://sexcams00.xyz]sexcams00.xyz[/url] young having sex at home on cams.

DavidKnilt (not verified)

free live naked sex cams [url=https://sexcams00.work]japan sex cams [/url] amature daddy daughter sex cams.

smithseo (not verified)

Loving the information on this site, you have done outstanding job on the posts . best CBD gummies

smithseo (not verified)

it is of course wise to always use recycled products because you can always help the environment,. best CBD capsules

zeeshanarain (not verified)

Thanks a bunch for sharing this with all people you actually understand what you’re speaking approximately! Bookmarked. Please additionally consult with my site =). We will have a link exchange arrangement between us! amber park price

zeeshanarain (not verified)

Good stuff here, but i noticed that several of your links are broken. thanks for the content anyway! treasure at tampines

DarrinLouby (not verified)

www.free sex cams [url=https://sexcams00.space]sexcams00[/url] nanny cams vidios sex.

Jamesinvok (not verified)

watch sex cams [url=https://sexcams00.live]sexcams00[/url] real sex cams in the united states.
home sex cams [url=https://sexcams00.website]colombian sex cams [/url] young brother sister sex cams.
latin sex cams [url=https://sexcams00.work]sexcams00[/url] search for private sex web cams.
free sex cams no credit card needed [url=https://sexcams00.site]sexcams00[/url] is sex cams in poland.
free live web sex cams [url=https://sexcams00.space]sexcams00[/url] sex cams with random people.
bedroom sex cams [url=https://sexcams00.club]sexcams00.club[/url] free realtime spy appartmet h1 sex cams.
completely free sex cams [url=https://sexcams00.online]sexcams00[/url] cam4 gay sex cams.

DarrinLouby (not verified)

erotic sex cams [url=https://sexcams00.space]sexcams00.space[/url] threesome sex cams.

DarrinLouby (not verified)

erotic sex cams [url=https://sexcams00.space]sexcams00.space[/url] threesome sex cams.

Jamesinvok (not verified)

video live sex cams [url=https://sexcams00.live]sexcams00.live[/url] coeur sex cams.
goth sex cams [url=https://sexcams00.website]sexcams00[/url] my free cams .com college sex advive.
interactive sex cams [url=https://sexcams00.work]lice sex cams [/url] video game girl characters sex mobile browser game cams.
real time sex cams [url=https://sexcams00.site]sexcams00[/url] sex cams online 1 on 1 no sign up.
skinny sex cams [url=https://sexcams00.space]naked sex cams [/url] best cams for hidden sex.
naked sex cams [url=https://sexcams00.club]free live sex cams porn [/url] sex arabic hidden cams veleb vids.
sex cams porn [url=https://sexcams00.online]gay sex cams [/url] sex face to face cams.

DarrinLouby (not verified)

sex cams online free [url=https://sexcams00.online]sexcams00.online[/url] live stream sex cams.

DarrinLouby (not verified)

sex cams online free [url=https://sexcams00.online]sexcams00.online[/url] live stream sex cams.

DarrinLouby (not verified)

sex cams online free [url=https://sexcams00.online]sexcams00.online[/url] live stream sex cams.

Jamesinvok (not verified)

kinky sex cams [url=https://sexcams00.live]sexcams00.live[/url] czech sex cams girls.
live sex cams no private [url=https://sexcams00.website]free online live sex cams [/url] sex lesbian chat rooms wihtout cams.
sex cams for free [url=https://sexcams00.work]sexcams00.work[/url] teen cams caught haveing sex.
female live sex cams [url=https://sexcams00.site]hd sex cams [/url] romania sex cams boom -porn.
free interactive sex cams [url=https://sexcams00.space]live sex cams naked [/url] shemale sex cams.
ive sex cams [url=https://sexcams00.club]sexcams00[/url] sex live cams.
jasmine live sex cams [url=https://sexcams00.online]sexcams00.online[/url] newbie sex cams.

Jamesinvok (not verified)

kinky sex cams [url=https://sexcams00.live]sexcams00.live[/url] czech sex cams girls.
live sex cams no private [url=https://sexcams00.website]free online live sex cams [/url] sex lesbian chat rooms wihtout cams.
sex cams for free [url=https://sexcams00.work]sexcams00.work[/url] teen cams caught haveing sex.
female live sex cams [url=https://sexcams00.site]hd sex cams [/url] romania sex cams boom -porn.
free interactive sex cams [url=https://sexcams00.space]live sex cams naked [/url] shemale sex cams.
ive sex cams [url=https://sexcams00.club]sexcams00[/url] sex live cams.
jasmine live sex cams [url=https://sexcams00.online]sexcams00.online[/url] newbie sex cams.

Jamesinvok (not verified)

free full sex cams [url=https://sexcams00.live]sexcams00[/url] ebony adult uncensored xxx live two way sex chat cams.
american sex cams [url=https://sexcams00.website]sexcams00.website[/url] free cams...women /dogs sex.
college live sex cams [url=https://sexcams00.work]sexcams00[/url] my free cams .com college sex advive.
live young sex cams [url=https://sexcams00.site]sexcams00[/url] live sex cams in ukraine.
live mobile sex cams [url=https://sexcams00.space]free sex cams [/url] cyber sex cams.
recorded sex cams [url=https://sexcams00.club]sexcams00.club[/url] free sex cams xxx.
free online live sex cams [url=https://sexcams00.online]live milf sex cams [/url] just sex chat, no cams.

Jamesinvok (not verified)

broadcast yourself sex cams [url=https://sexcams00.live]sexcams00.live[/url] ebony adult two way sex cams.
amsterdam sex cams [url=https://sexcams00.website]weird sex cams [/url] xxxnataliamackenziexxxus sex cams.
sex cams for teens [url=https://sexcams00.work]sexcams00.work[/url] vouyer sex cams.
online sex cams free [url=https://sexcams00.site]young girl live sex cams [/url] live sex cams nude girls.
live hardcore sex cams [url=https://sexcams00.space]sexcams00.space[/url] steph and real life cams maya sex.
men sex cams [url=https://sexcams00.club]sexcams00[/url] sex caught on cams.
sex cams centre [url=https://sexcams00.online]sexcams00.online[/url] sexy gay sex live cams.

Jamesinvok (not verified)

broadcast yourself sex cams [url=https://sexcams00.live]sexcams00.live[/url] ebony adult two way sex cams.
amsterdam sex cams [url=https://sexcams00.website]weird sex cams [/url] xxxnataliamackenziexxxus sex cams.
sex cams for teens [url=https://sexcams00.work]sexcams00.work[/url] vouyer sex cams.
online sex cams free [url=https://sexcams00.site]young girl live sex cams [/url] live sex cams nude girls.
live hardcore sex cams [url=https://sexcams00.space]sexcams00.space[/url] steph and real life cams maya sex.
men sex cams [url=https://sexcams00.club]sexcams00[/url] sex caught on cams.
sex cams centre [url=https://sexcams00.online]sexcams00.online[/url] sexy gay sex live cams.

DarrinLouby (not verified)

no membership sex cams [url=https://sexcams00.online]live sex cams free [/url] raellife spy appartments sex cams.

DarrinLouby (not verified)

no membership sex cams [url=https://sexcams00.online]live sex cams free [/url] raellife spy appartments sex cams.

DarrinLouby (not verified)

no membership sex cams [url=https://sexcams00.online]live sex cams free [/url] raellife spy appartments sex cams.

DarrinLouby (not verified)

no membership sex cams [url=https://sexcams00.online]live sex cams free [/url] raellife spy appartments sex cams.

Jarednug (not verified)

american sex cams [url=https://sexcams00.live]live sex cams no credit card [/url] foreign grannies sex cams.
hidden teen sex cams [url=https://sexcams00.website]sexcams00.website[/url] live gay sex web cams.
live sex cams porn [url=https://sexcams00.work]female live sex cams [/url] sex cams for tokens.
sex cams chat [url=https://sexcams00.site]xhamster sex cams [/url] real live streaming sex cams.

DarrinLouby (not verified)

hardcore live sex cams [url=https://sexcams00.live]sexcams00.live[/url] free hd voyeur sex cams.

Jarednug (not verified)

american sex cams [url=https://sexcams00.live]live sex cams no credit card [/url] foreign grannies sex cams.
hidden teen sex cams [url=https://sexcams00.website]sexcams00.website[/url] live gay sex web cams.
live sex cams porn [url=https://sexcams00.work]female live sex cams [/url] sex cams for tokens.
sex cams chat [url=https://sexcams00.site]xhamster sex cams [/url] real live streaming sex cams.

Jarednug (not verified)

american sex cams [url=https://sexcams00.live]live sex cams no credit card [/url] foreign grannies sex cams.
hidden teen sex cams [url=https://sexcams00.website]sexcams00.website[/url] live gay sex web cams.
live sex cams porn [url=https://sexcams00.work]female live sex cams [/url] sex cams for tokens.
sex cams chat [url=https://sexcams00.site]xhamster sex cams [/url] real live streaming sex cams.

DarrinLouby (not verified)

free sex cams no credit card needed [url=https://sexcams00.website]sexcams00[/url] live sex xname sex cams.

DarrinLouby (not verified)

free sex cams no credit card needed [url=https://sexcams00.website]sexcams00[/url] live sex xname sex cams.

Jarednug (not verified)

free live amateur sex cams [url=https://sexcams00.live]sexcams00.live[/url] teen cams caught having sex.
live sex cams porn [url=https://sexcams00.website]sexcams00.website[/url] interia.pl sex cams.
uk live sex cams [url=https://sexcams00.work]live male sex cams [/url] reallife spy sex cams.
watch free live sex cams [url=https://sexcams00.site]real life cams sex [/url] young sex web cams.

Jarednug (not verified)

free live amateur sex cams [url=https://sexcams00.live]sexcams00.live[/url] teen cams caught having sex.
live sex cams porn [url=https://sexcams00.website]sexcams00.website[/url] interia.pl sex cams.
uk live sex cams [url=https://sexcams00.work]live male sex cams [/url] reallife spy sex cams.
watch free live sex cams [url=https://sexcams00.site]real life cams sex [/url] young sex web cams.

Jarednug (not verified)

free live amateur sex cams [url=https://sexcams00.live]sexcams00.live[/url] teen cams caught having sex.
live sex cams porn [url=https://sexcams00.website]sexcams00.website[/url] interia.pl sex cams.
uk live sex cams [url=https://sexcams00.work]live male sex cams [/url] reallife spy sex cams.
watch free live sex cams [url=https://sexcams00.site]real life cams sex [/url] young sex web cams.

DarrinLouby (not verified)

online live sex cams [url=https://sexcams00.online]sexcams00[/url] private sex cams.

DarrinLouby (not verified)

top live sex cams [url=https://sexcams00.website]sexcams00[/url] teen girls tip sex cams.

DarrinLouby (not verified)

top live sex cams [url=https://sexcams00.website]sexcams00[/url] teen girls tip sex cams.

DarrinLouby (not verified)

top live sex cams [url=https://sexcams00.website]sexcams00[/url] teen girls tip sex cams.

DarrinLouby (not verified)

top live sex cams [url=https://sexcams00.website]sexcams00[/url] teen girls tip sex cams.

Jarednug (not verified)

interactive sex cams [url=https://sexcams00.live]watch 100% free sex cams [/url] hidden sex spy cams.
young teen sex cams [url=https://sexcams00.website]cheap sex cams [/url] free sex face to face cams.
live college sex cams [url=https://sexcams00.work]sexcams00[/url] hidden cams used to view males having sex in department stores.
porn sex cams [url=https://sexcams00.site]live ebony sex cams [/url] couples sex cГ ms.

DarrinLouby (not verified)

teen girl sex cams [url=https://sexcams00.xyz]sex cams free [/url] trailer park go on live web cams to have and show having sex party.

DarrinLouby (not verified)

jasmine live sex cams [url=https://sexcams00.online]sexcams00[/url] real maids having sex on hidden cams videos amatuer.

DarrinLouby (not verified)

live teen sex cams [url=https://sexcams00.online]sexcams00[/url] chaturbate - free adult webcams, live sex, free sex chat, exhibitionist & pornstar free cams.

DarrinLouby (not verified)

amature hidden sex cams [url=https://sexcams00.space]sex cams amateur [/url] flodia sex cams.

Jarednug (not verified)

hawaiian sex cams [url=https://sexcams00.live]free ebony sex cams [/url] reallife time appartments sex h1 cams.
pornhub live sex cams [url=https://sexcams00.website]sexcams00[/url] gonewild sex cams.
college live sex cams [url=https://sexcams00.work]sexcams00.work[/url] men and women sex cams.
pregnant sex cams [url=https://sexcams00.site]free lesbian sex cams [/url] young teen sex cams live.

Jarednug (not verified)

hawaiian sex cams [url=https://sexcams00.live]free ebony sex cams [/url] reallife time appartments sex h1 cams.
pornhub live sex cams [url=https://sexcams00.website]sexcams00[/url] gonewild sex cams.
college live sex cams [url=https://sexcams00.work]sexcams00.work[/url] men and women sex cams.
pregnant sex cams [url=https://sexcams00.site]free lesbian sex cams [/url] young teen sex cams live.

DarrinLouby (not verified)

fav sex cams [url=https://sexcams00.club]sexcams00.club[/url] sex cams iraq girls i buy.

DavidIncum (not verified)

real life sex cams [url=https://sexcams00.site]sex cams tumblr [/url] private sex cams.

Andrewsed (not verified)

goth sex cams [url=https://sexcams00.live]sexcams00.live[/url] live sex on web cams.
sex cams and chat [url=https://sexcams00.website]sexcams00.website[/url] secret massage sex cum cams.
totaly free sex cams [url=https://sexcams00.work]free girl sex cams [/url] forced sex video cams free.
office sex cams [url=https://sexcams00.site]midget sex cams [/url] transgender sex cams.

Andrewsed (not verified)

goth sex cams [url=https://sexcams00.live]sexcams00.live[/url] live sex on web cams.
sex cams and chat [url=https://sexcams00.website]sexcams00.website[/url] secret massage sex cum cams.
totaly free sex cams [url=https://sexcams00.work]free girl sex cams [/url] forced sex video cams free.
office sex cams [url=https://sexcams00.site]midget sex cams [/url] transgender sex cams.

DavidIncum (not verified)

personal sex cams [url=https://sexcams00.space]sexcams00[/url] jerky sex cams jenny.

Add new comment