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

CurtisStity (not verified)

cam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] free webcam girl.

Stevenmyday (not verified)

sexy cam girl [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] my sex cam.
live cam sex [url=https://bestonlinesexwebcams.com]sex cam free [/url] granny webcam sex.
live sex cam [url=https://bestonlinesexcams1.com]amature webcam sex [/url] webcam sex app.

Stevenmyday (not verified)

sexy cam girl [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] my sex cam.
live cam sex [url=https://bestonlinesexwebcams.com]sex cam free [/url] granny webcam sex.
live sex cam [url=https://bestonlinesexcams1.com]amature webcam sex [/url] webcam sex app.

CurtisStity (not verified)

best cam to cam sites [url=https://adultcamsites1.com]adult cam to cam sites [/url] free gay cam site.

Stevenmyday (not verified)

best cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] exhibitionist cam sites.
live cam site [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] best cam to cam site.
webcam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] teen girls nude web cam.

Stevenmyday (not verified)

live lesbian sex cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] hidden webcam sex.
top webcam sites [url=https://adultfreewebcamsites.com]free cam to cam sites [/url] free cam 2 cam sites.
free sex cam site [url=https://adultcamsites1.com]best live cam site [/url] best cam sites for couples.

CurtisStity (not verified)

adult cam site [url=https://adultcamsites1.com]adultcamsites1[/url] best cam sites reddit.

CurtisStity (not verified)

adult cam site [url=https://adultcamsites1.com]adultcamsites1[/url] best cam sites reddit.

MarinaAlten (not verified)

http://myseries.namesofhonor.org/__media__/js/netsoltrademark.php?d=vrpornlinks.net/sexe/cam4-hommes/
http://h2know.net/__media__/js/netsoltrademark.php?d=vrpornlinks.net/sexe/vidz7-french/
http://akbankvesanat.org/__media__/js/netsoltrademark.php?d=vrpornlinks.net/sexe/se-doigter/
http://chomperz.com/__media__/js/netsoltrademark.php?d=vrpornlinks.net/sexe/gros-tГ©ton/
http://young-teen-pics.com/crtr/cgi/out.cgi?id=25&l=top_top&u=https://www.vrpornlinks.net/sexe/porno-amГ©ricain/
http://www.iaqpro.com/__media__/js/netsoltrademark.php?d=vrpornlinks.net/sexe/cindy-lopes-porn/

f568v

smithseo (not verified)

Et bien oui et aussi pas vraiment. Certes parce que il est probable qu’on acquiert certaines sources qui certainement accusent les memes validités. Non puisque il ne suffit pas de reproduire ce que l’on a la possibilité de retouver sur des articles autres et le transposer tellement clairement; best CBD oil

Sale viagra (not verified)

Apkqcc fwgsdm sildenafil tablets Buy pfizer viagra online

seo (not verified)

Much thanks for composing such a fascinating article on this point. This has truly made me think and I want to peruse more. UFABET

Free trial of viagra (not verified)

Vikwsv uomcvq online buy viagra Canadian viagra 50mg

Free trial of viagra (not verified)

Vikwsv uomcvq online buy viagra Canadian viagra 50mg

RobertCor (not verified)

young girls on cam [url=https://camgirls1.com]busty cam girls [/url] girls get naked on cam.
best cam sites [url=https://adultcamsites1.com]most popular cam site [/url] most popular webcam sites.
teen webcam sex [url=https://bestonlinesexwebcams.com]adult live sex cam [/url] best sex webcam.
live webcam sites [url=https://adultcamsites1.com]webcam sites like omegle [/url] sexy webcam sites.
live lesbian sex cam [url=https://bestonlinesexcams1.com]phone cam sex [/url] webcam couple sex.
live sex cam [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] video cam sex.

CraigFet (not verified)

porn cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] cam sites like omegle.

CraigFet (not verified)

porn cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] cam sites like omegle.

QuentinPex (not verified)

best adult cam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] real life cam official site.
webcam model [url=https://hotcamgirls1.com]webcam girl friend [/url] girls masturbating webcams.
webcam model [url=https://hotcamgirls1.com]hotcamgirls1[/url] cam girls live.

CraigFet (not verified)

webcam model [url=https://camgirls1.com]cam girl ass [/url] naked girls cam.

RobertCor (not verified)

xxx cam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] live cam sex.
live cam sex [url=https://bestonlinesexwebcams.com]hidden cam sex videos tumblr [/url] live sexcams.
free adult cam sites [url=https://adultfreewebcamsites.com]asian webcam sites [/url] most popular cam site.
free sex cam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] best porn webcam sites.
teen sex cam [url=https://bestonlinesexwebcams.com]live sexcams [/url] hd live sex cam.
sex cam sites [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] asian live sex cam.

QuentinPex (not verified)

best sex cam site [url=https://adultcamsites1.com]free cam site [/url] webcam chat sites quickcam chat.
sex cam sites [url=https://bestonlinesexcams1.com]sex cam sites [/url] young sex cam.
real life cam sex [url=https://bestonlinesexcams1.com]real spy cam sex [/url] real cam sex.

QuentinPex (not verified)

best sex cam site [url=https://adultcamsites1.com]free cam site [/url] webcam chat sites quickcam chat.
sex cam sites [url=https://bestonlinesexcams1.com]sex cam sites [/url] young sex cam.
real life cam sex [url=https://bestonlinesexcams1.com]real spy cam sex [/url] real cam sex.

RobertCor (not verified)

indian live sex cam [url=https://bestonlinesexcams1.com]ebony live sex cam [/url] pinay sex cam.
couple cam site [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] best live sex cam sites.
cam model [url=https://hotcamgirls1.com]cam girl show [/url] live web cam girls.
random sex cam [url=https://bestonlinesexcams1.com]live sex webcam [/url] private sex cam.
young girls on cam [url=https://camgirls1.com]cam girl orgasm [/url] amateur cam girls.
best cam sites [url=https://adultfreewebcamsites.com]best porn cam sites [/url] best sex webcam sites.

CraigFet (not verified)

best live cam sites [url=https://adultcamsites1.com]best free cam site [/url] top sex cam sites.

RobertCor (not verified)

indian live sex cam [url=https://bestonlinesexcams1.com]ebony live sex cam [/url] pinay sex cam.
couple cam site [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] best live sex cam sites.
cam model [url=https://hotcamgirls1.com]cam girl show [/url] live web cam girls.
random sex cam [url=https://bestonlinesexcams1.com]live sex webcam [/url] private sex cam.
young girls on cam [url=https://camgirls1.com]cam girl orgasm [/url] amateur cam girls.
best cam sites [url=https://adultfreewebcamsites.com]best porn cam sites [/url] best sex webcam sites.

CraigFet (not verified)

best live cam sites [url=https://adultcamsites1.com]best free cam site [/url] top sex cam sites.

CraigFet (not verified)

best live cam sites [url=https://adultcamsites1.com]best free cam site [/url] top sex cam sites.

CraigFet (not verified)

best live cam sites [url=https://adultcamsites1.com]best free cam site [/url] top sex cam sites.

CraigFet (not verified)

free cam sites [url=https://adultfreewebcamsites.com]xxx cam sites [/url] free nude webcam sites.

zeeshanarain (not verified)

my dad is a massage therapist and he can really relieve minor pains and injuries’ The Landmark Showflat

RobertCor (not verified)

teen cam girls [url=https://camgirls1.com]hot teen cam girls [/url] naked girls cam.
best free webcam sites [url=https://adultcamsites1.com]best paying webcam sites [/url] best random cam sites.
couple cam site [url=https://adultcamsites1.com]adultcamsites1[/url] sexy webcam sites.
cammodels [url=https://camgirls1.com]cammodel [/url] live web cam girls.
web cam girls [url=https://hotcamgirls1.com]hotcamgirls1[/url] naked cam girls.
couple cam sites [url=https://adultfreewebcamsites.com]top adult cam sites [/url] best chat cam sites.

QuentinPex (not verified)

filipina sex cam [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] sex cam with strangers.
webcam girls [url=https://camgirls1.com]camgirls1.com[/url] view cam of nude girls.
ebony live sex cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex cam women.

Anonymous (not verified)

of course we need to know our family history so that we can share it to our kids.. Kent Ridge Hill Residences Floor Plan

Howardchoob (not verified)

live cam girls [url=https://camgirls1.com]camgirls1[/url] hd cam girls.
sex webcam sites [url=https://adultcamsites1.com]sexy webcam sites [/url] best adult cam sites.
camgirls [url=https://camgirls1.com]girls masturbating webcams [/url] girl cam chat.

RichardReemi (not verified)

best cam girl sites [url=https://camgirls1.com]camgirls1.com[/url] black cam girls.
nude cam girls [url=https://camgirls1.com]camgirls1[/url] big boob cam girls.
cam sites free [url=https://adultfreewebcamsites.com]live webcam sites [/url] top web cam site.
amateur webcam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] chaturbate sex cam.
webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] hidden spy cam sex.
chat cam girl [url=https://camgirls1.com]girls cams [/url] cam girl websites.

RichardReemi (not verified)

best cam girl sites [url=https://camgirls1.com]camgirls1.com[/url] black cam girls.
nude cam girls [url=https://camgirls1.com]camgirls1[/url] big boob cam girls.
cam sites free [url=https://adultfreewebcamsites.com]live webcam sites [/url] top web cam site.
amateur webcam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] chaturbate sex cam.
webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] hidden spy cam sex.
chat cam girl [url=https://camgirls1.com]girls cams [/url] cam girl websites.

Generic viagra us (not verified)

Fubquh hmoxvq generic viagra for sale online 50mg viagra

Howardchoob (not verified)

webcam chat sites without registering [url=https://adultfreewebcamsites.com]mobile cam sites [/url] free gay cam site.
ebony live sex cam [url=https://bestonlinesexwebcams.com]my sex cam [/url] real spy cam sex.
gay webcam sites [url=https://adultfreewebcamsites.com]webcam sites adult [/url] couples webcam sites.

RichardReemi (not verified)

cam to cam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] sex web cam sites.
live free sex cam [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] family sex webcam.
free sex cam sites [url=https://adultcamsites1.com]adultcamsites1.com[/url] best cam to cam site.
cam free sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex webcam.
adult cam site [url=https://adultcamsites1.com]adultcamsites1.com[/url] best porn webcam sites.
sexy cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] sex cam gay.

RichardReemi (not verified)

live cam sex [url=https://bestonlinesexcams1.com]free webcam sex [/url] homemade webcam sex.
free live sex cam [url=https://bestonlinesexwebcams.com]hidden cam dorm sex [/url] best sex cam.
asian sex cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] hidden cam sex clips.
adult web cam sites [url=https://adultcamsites1.com]porn cam sites [/url] best nude cam sites.
cam sex live [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] lesbian webcam sex.
gay sex cam [url=https://bestonlinesexcams1.com]sex cam free [/url] gay sex cam chat.

Howardchoob (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] my sex cam.
web cam sites [url=https://adultcamsites1.com]couples cam site [/url] top web cam sites.
asian cam site [url=https://adultfreewebcamsites.com]live cam site [/url] adult webcam sites.

Howardchoob (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] my sex cam.
web cam sites [url=https://adultcamsites1.com]couples cam site [/url] top web cam sites.
asian cam site [url=https://adultfreewebcamsites.com]live cam site [/url] adult webcam sites.

Howardchoob (not verified)

webcam sex video [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams[/url] my sex cam.
web cam sites [url=https://adultcamsites1.com]couples cam site [/url] top web cam sites.
asian cam site [url=https://adultfreewebcamsites.com]live cam site [/url] adult webcam sites.

Howardchoob (not verified)

free live cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] live cam sex.
live webcam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] home sex cam.
cheapest cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] asian webcam sites.

Howardchoob (not verified)

free live cam sex [url=https://bestonlinesexwebcams.com]bestonlinesexwebcams.com[/url] live cam sex.
live webcam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] home sex cam.
cheapest cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] asian webcam sites.

RichardReemi (not verified)

couple cam site [url=https://adultcamsites1.com]adultcamsites1[/url] webcam sites for couples.
hot sex cam [url=https://bestonlinesexcams1.com]dad daughter webcam sex [/url] desi hidden cam sex.
free webcam sites [url=https://adultcamsites1.com]online webcam sites [/url] webcam sites reviews.
top adult cam sites [url=https://adultfreewebcamsites.com]sex cam site [/url] free adult web cam sites.
free live cam sites [url=https://adultfreewebcamsites.com]best cam sites for couples [/url] free adult web cam sites.
sex cam site [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] cam sites like chaturbate.

zeeshanarain (not verified)

Harry Potter is the best, i really like Daniel Radcliffe because he acts very well,, The Linq Showflat

zeeshanarain (not verified)

Audio started playing as soon as I opened up this website, so frustrating! midtown modern gucco

Stevenken (not verified)

teen webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] chat cam sex.

Stevenken (not verified)

teen webcam sex videos [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] chat cam sex.

Add new comment