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

Edwardhaipt (not verified)

top adult webcam sites top adult cam sites free nude webcam sites.

Edwardhaipt (not verified)

gay sex cam filipina sex cam secret cam sex.

Edwardhaipt (not verified)

gay sex cam filipina sex cam secret cam sex.

Edwardhaipt (not verified)

free webcam girls hotcamgirls1 camgirl creampie.

Edwardhaipt (not verified)

camgirl porn hotcamgirls1 camgirl site.

Edwardhaipt (not verified)

camgirl porn hotcamgirls1 camgirl site.

Edwardhaipt (not verified)

free sex web cam bestonlinesexwebcams best webcam sex sites.

smithseo (not verified)

Articles with informative content like yours are a breath of fresh air. I thoroughly enjoyed every thought you made in your material. I am with you on your original views and unique content. Thank you. best CBD oil for pain

Stevearofs (not verified)

[url=https://teenwebcams.xyz]teenwebcams.xyz[/url]
[url=https://sappmysqtadufet.ga]sappmysqtadufet.ga[/url]
[url=https://oninpresento.ml]oninpresento.ml[/url]

Stevearofs (not verified)

[url=https://tancitapo.tk]tancitapo.tk[/url]
[url=https://freeporncams.monster]freeporncams.monster[/url]
[url=https://freesexcams.icu]freesexcams.icu[/url]
[url=https://willlandprednili.gq]willlandprednili.gq[/url]
[url=https://lanthpopcotari.ml]lanthpopcotari.ml[/url]
[url=https://collegesexwebcams.xyz]collegesexwebcams.xyz[/url]
[url=https://oninpresento.ml]oninpresento.ml[/url]
[url=https://teensexwebcams.xyz]teensexwebcams.xyz[/url]
[url=https://imirblixin.tk]imirblixin.tk[/url]
[url=https://benraipensa.tk]benraipensa.tk[/url]
[url=https://dioupurtichi.tk]dioupurtichi.tk[/url]
[url=https://benraipensa.tk]benraipensa.tk[/url]
[url=https://maturesexwebcams.icu]maturesexwebcams.icu[/url]
[url=https://freeadultwebcams.club]freeadultwebcams.club[/url]

Stevearofs (not verified)

[url=https://lesbiansexwebcams.monster]lesbiansexwebcams.monster[/url]
[url=https://leyspenemacle.ml]leyspenemacle.ml[/url]
[url=https://amateurwebcams.monster]amateurwebcams.monster[/url]
[url=https://upturrippse.tk]upturrippse.tk[/url]
[url=https://puvilnebetdeck.cf]puvilnebetdeck.cf[/url]
[url=https://blacksexwebcams.club]blacksexwebcams.club[/url]
[url=https://alinatinen.cf]alinatinen.cf[/url]
[url=https://exviltairietar.ml]exviltairietar.ml[/url]
[url=https://jodhsuphogeli.tk]jodhsuphogeli.tk[/url]
[url=https://niahardbreakizam.gq]niahardbreakizam.gq[/url]
[url=https://blackebonywebcams.club]blackebonywebcams.club[/url]
[url=https://toocarfipeccu.ml]toocarfipeccu.ml[/url]
[url=https://oninpresento.ml]oninpresento.ml[/url]
[url=https://teencams.casa]teencams.casa[/url]
[url=https://willlandprednili.gq]willlandprednili.gq[/url]

Floydwek (not verified)

best cam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] bbw cam site.

smithseo (not verified)

Unaltered individuals civilizations will no more time are current inside of industrialized nations all over the planet which include the united states of the us and also Europe. best CBD gummies

Brgxtn y (not verified)
Williamgon (not verified)

young cam girls [url=https://camgirls1.com]big tit cam girls fucking [/url] video cam girls.

Williamgon (not verified)

porn webcam sites [url=https://adultfreewebcamsites.com]adultfreewebcamsites.com[/url] dirty cam sites.

Williamgon (not verified)

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

Williamgon (not verified)

other ebony cam sites [url=https://adultfreewebcamsites.com]ebony cam sites [/url] best cam site for couples.

Williamgon (not verified)

other ebony cam sites [url=https://adultfreewebcamsites.com]ebony cam sites [/url] best cam site for couples.

Williamgon (not verified)

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

Williamgon (not verified)

my free cam site [url=https://adultfreewebcamsites.com]adultfreewebcamsites[/url] highest paying cam sites.

Williamgon (not verified)

camgirls [url=https://hotcamgirls1.com]lesbian cam girls [/url] sex cam girls.

Williamgon (not verified)

camgirls [url=https://hotcamgirls1.com]lesbian cam girls [/url] sex cam girls.

Williamgon (not verified)

camgirls [url=https://hotcamgirls1.com]lesbian cam girls [/url] sex cam girls.

Williamgon (not verified)

webcam group sex [url=https://bestonlinesexcams1.com]hd live sex cam [/url] hidden sex cam.

zeeshanarain (not verified)

I love to visit your web-blog, the themes are nice.~~-`; One Pearl Bank

Williamgon (not verified)

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

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.

Add new comment