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

Williamgon (not verified)

cam sex online [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] hidden cam sex clips.

Williamgon (not verified)

free webcam sites [url=https://adultcamsites1.com]webcam chat sites quickcam chat [/url] webcam sites reviews.

RickyTrods (not verified)

free webcam girls [url=https://camgirls1.com]teen girls nude web cam [/url] cam girl show.

RickyTrods (not verified)

other ebony cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] cheapest cam site.

RickyTrods (not verified)

other ebony cam sites [url=https://adultcamsites1.com]adultcamsites1[/url] cheapest cam site.

StephenToill (not verified)

asian cam sites [url=https://adultcamsites1.com]naked cam site [/url] mobile cam sites.

StephenToill (not verified)

cam girl sites [url=https://adultcamsites1.com]adultcamsites1[/url] girl cam sites.

StephenToill (not verified)

young webcam sex [url=https://bestonlinesexcams1.com]bestonlinesexcams1[/url] free live sex cam chat.

StephenToill (not verified)

hd sex cam [url=https://bestonlinesexwebcams.com]rabbit cam sex [/url] drunk sex caught on cam.

StephenToill (not verified)

sex webcam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] most popular webcam sites.

StephenToill (not verified)

free adult web cam sites [url=https://adultfreewebcamsites.com]video cam sites [/url] xxx cam site.

StephenToill (not verified)

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

StephenToill (not verified)

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

KelvinJax (not verified)

webcam sites [url=https://adultcamsites1.com]free sex cam sites [/url] jasmin cam site.

KelvinJax (not verified)

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

KelvinJax (not verified)

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

KelvinJax (not verified)

free sex web cam [url=https://bestonlinesexcams1.com]bestonlinesexcams1.com[/url] family sex webcam.

KelvinJax (not verified)

web cam sex [url=https://bestonlinesexcams1.com]mobile sex cam [/url] men sex cam.

KelvinJax (not verified)

camgirl [url=https://camgirls1.com]camgirls1.com[/url] girls camera chat.

KelvinJax (not verified)

camgirl [url=https://camgirls1.com]camgirls1.com[/url] girls camera chat.

CurtisStity (not verified)

naked cam girls [url=https://hotcamgirls1.com]hot cam girl [/url] nude webcam girls.

Stevenmyday (not verified)

other ebony cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] asian webcam sites.
young cam girls [url=https://camgirls1.com]top webcam girls [/url] romanian cam girls.
cam to cam sex [url=https://bestonlinesexcams1.com]cam sex [/url] best free sex cam.

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.

Add new comment