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

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.

DavidIncum (not verified)

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

DavidIncum (not verified)

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

DavidIncum (not verified)

online free sex cams [url=https://sexcams00.xyz]sexcams00[/url] teen tip sex cams.

Andrewsed (not verified)

bonja sex cams [url=https://sexcams00.live]free group sex cams [/url] free live sex cams around the world.
sex cams 19 [url=https://sexcams00.website]broadcast yourself sex cams [/url] live sex cams st.louis.
free amature sex cams [url=https://sexcams00.work]live male sex cams [/url] nintendo girl characters sex mobile browser game cams.
iphone sex cams [url=https://sexcams00.site]hardcore live sex cams [/url] live sex couples cams.

Andrewsed (not verified)

bonja sex cams [url=https://sexcams00.live]free group sex cams [/url] free live sex cams around the world.
sex cams 19 [url=https://sexcams00.website]broadcast yourself sex cams [/url] live sex cams st.louis.
free amature sex cams [url=https://sexcams00.work]live male sex cams [/url] nintendo girl characters sex mobile browser game cams.
iphone sex cams [url=https://sexcams00.site]hardcore live sex cams [/url] live sex couples cams.

Andrewsed (not verified)

bonja sex cams [url=https://sexcams00.live]free group sex cams [/url] free live sex cams around the world.
sex cams 19 [url=https://sexcams00.website]broadcast yourself sex cams [/url] live sex cams st.louis.
free amature sex cams [url=https://sexcams00.work]live male sex cams [/url] nintendo girl characters sex mobile browser game cams.
iphone sex cams [url=https://sexcams00.site]hardcore live sex cams [/url] live sex couples cams.

DavidIncum (not verified)

live voyeur sex cams [url=https://sexcams00.xyz]sexcams00.xyz[/url] nintendo girl characters sex mobile browser game cams.

DavidIncum (not verified)

live voyeur sex cams [url=https://sexcams00.xyz]sexcams00.xyz[/url] nintendo girl characters sex mobile browser game cams.

DavidIncum (not verified)

live voyeur sex cams [url=https://sexcams00.xyz]sexcams00.xyz[/url] nintendo girl characters sex mobile browser game cams.

DavidIncum (not verified)

cam4 free live sex cams [url=https://sexcams00.live]sexcams00[/url] freereallifecams illona, tim, yanna ricky sex cams.

DavidIncum (not verified)

bondage sex cams [url=https://sexcams00.website]moms sex cams [/url] free women dog sex cams.

DavidIncum (not verified)

bondage sex cams [url=https://sexcams00.website]moms sex cams [/url] free women dog sex cams.

Andrewsed (not verified)

free black sex cams [url=https://sexcams00.live]sexcams00[/url] hd hidden cams sex.
public sex cams [url=https://sexcams00.website]couples sex web cams [/url] hidden gay cams sex.
skinny sex cams [url=https://sexcams00.work]sexcams00.work[/url] free sex web cams to webcam.
skinny sex cams [url=https://sexcams00.site]sexcams00.site[/url] free sex talk cams.

Andrewsed (not verified)

free black sex cams [url=https://sexcams00.live]sexcams00[/url] hd hidden cams sex.
public sex cams [url=https://sexcams00.website]couples sex web cams [/url] hidden gay cams sex.
skinny sex cams [url=https://sexcams00.work]sexcams00.work[/url] free sex web cams to webcam.
skinny sex cams [url=https://sexcams00.site]sexcams00.site[/url] free sex talk cams.

Andrewsed (not verified)

free black sex cams [url=https://sexcams00.live]sexcams00[/url] hd hidden cams sex.
public sex cams [url=https://sexcams00.website]couples sex web cams [/url] hidden gay cams sex.
skinny sex cams [url=https://sexcams00.work]sexcams00.work[/url] free sex web cams to webcam.
skinny sex cams [url=https://sexcams00.site]sexcams00.site[/url] free sex talk cams.

DavidIncum (not verified)

gymnast hidden sex cams [url=https://sexcams00.space]totally free live sex cams [/url] live sex couples cams.

DavidIncum (not verified)

gymnast hidden sex cams [url=https://sexcams00.space]totally free live sex cams [/url] live sex couples cams.

DavidIncum (not verified)

gymnast hidden sex cams [url=https://sexcams00.space]totally free live sex cams [/url] live sex couples cams.

DavidIncum (not verified)

best sex cams quora [url=https://sexcams00.website]free sex cams women [/url] amatuer sex cams.

Andrewsed (not verified)

nasty sex cams [url=https://sexcams00.live]sexcams00.live[/url] young people sex video cams.
canadian sex cams [url=https://sexcams00.website]live amateur sex cams [/url] online web cams sex chat.
virtual sex cams [url=https://sexcams00.work]sexcams00.work[/url] couples sex cГ ms.
live public sex cams [url=https://sexcams00.site]sexcams00.site[/url] sex cams in wisconsin.

DavidIncum (not verified)

live hidden sex cams [url=https://sexcams00.website]free sex cams no sign up [/url] freereallifecams illona, tim, yanna ricky sex cams.

DavidIncum (not verified)

live hidden sex cams [url=https://sexcams00.website]free sex cams no sign up [/url] freereallifecams illona, tim, yanna ricky sex cams.

DavidIncum (not verified)

live hidden sex cams [url=https://sexcams00.website]free sex cams no sign up [/url] freereallifecams illona, tim, yanna ricky sex cams.

DavidIncum (not verified)

live adult sex cams [url=https://sexcams00.xyz]sexcams00[/url] over seas phillipine sex cams.

Andrewsed (not verified)

free live mobile sex cams [url=https://sexcams00.live]free teen sex cams [/url] kristapassion imlive sex cams.
redhead sex cams [url=https://sexcams00.website]real life sex cams [/url] girls live sex cams com.
couples free sex cams [url=https://sexcams00.work]sexcams00[/url] sister brother sex cams.
live sex cams asian [url=https://sexcams00.site]local live sex cams [/url] free realtime spy appartmens sex cams.

DavidIncum (not verified)

free amature sex cams [url=https://sexcams00.xyz]sexcams00[/url] young having sex at home on cams.

Nevillederma (not verified)

nasty free sex cams [url=https://sexcams00.live]random sex cams [/url] free no cc or age verification or sign up or upgrades or tokens needed public sex cams.

Nevillederma (not verified)

ashley raye sex cams [url=https://sexcams00.xyz]free live naked sex cams [/url] teen group sex home cams.

Add new comment