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

BobbyPaw (not verified)

free shemale sex cams [url=https://sexcams00.space]sexcams00[/url] live sex cams in ukraine.

BobbyPaw (not verified)

sex cams for men [url=https://sexcams00.site]sex cams free trial [/url] st free live sex cams.

BobbyPaw (not verified)

sex cams for men [url=https://sexcams00.site]sex cams free trial [/url] st free live sex cams.

BobbyPaw (not verified)

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

BobbyPaw (not verified)

free girl sex cams [url=https://sexcams00.work]sexcams00[/url] live sex asia cheap live sex cams.

BobbyPaw (not verified)

free girl sex cams [url=https://sexcams00.work]sexcams00[/url] live sex asia cheap live sex cams.

zeeshanarain (not verified)

Youre so cool! I dont suppose Ive read anything like this prior to. So nice to locate somebody with original thoughts on this subject. realy we appreciate you beginning this up. this amazing site is a thing that is required on the web, a person with a little originality. beneficial purpose of bringing interesting things to your internet! Biloxi handyman

zeeshanarain (not verified)

Trusting to make the right decisions can be tough. Many of us develop this ability over the course of our life. It doesn’t really just happen if you know what I mean. tree companies near me

pills erectile ... (not verified)

Rviiio gbkjnk cialis online pills erectile dysfunction

BobbyPaw (not verified)

live sex cams no credit card [url=https://sexcams00.xyz]sexcams00[/url] cams sex xxx.

Thomasnaw (not verified)

bbw live sex cams [url=https://sexcams00.work]skype sex cams [/url] real amatuer home sex cams.

Thomasnaw (not verified)

reddit sex cams [url=https://sexcams00.xyz]live free sex cams [/url] the reallife appartments spy sex cams.

Thomasnaw (not verified)

fantasy sex cams [url=https://sexcams00.site]sexcams00[/url] chatrerbait sex cams.

Thomasnaw (not verified)

hidden gay sex cams [url=https://sexcams00.website]adult sex cams [/url] best live streaming amateur sex cams.

Thomasnaw (not verified)

hidden gay sex cams [url=https://sexcams00.website]adult sex cams [/url] best live streaming amateur sex cams.

Thomasnaw (not verified)

www live sex cams [url=https://sexcams00.website]sexcams00[/url] bbw sex cams.

Thomasnaw (not verified)

cam4 live sex cams [url=https://sexcams00.work]free male sex cams [/url] 65 yo granny sex cams.

Thomasnaw (not verified)

live jasmine sex cams [url=https://sexcams00.online]sexcams00.online[/url] jerky sex cams mature.

Thomasnaw (not verified)

live jasmine sex cams [url=https://sexcams00.online]sexcams00.online[/url] jerky sex cams mature.

Thomasnaw (not verified)

live gay male sex cams [url=https://sexcams00.work]sexcams00.work[/url] indian sex cams.

Thomasnaw (not verified)

free male sex cams [url=https://sexcams00.live]sexcams00[/url] real homemade amateur forced anal sex cams.

Thomasnaw (not verified)

free male sex cams [url=https://sexcams00.live]sexcams00[/url] real homemade amateur forced anal sex cams.

Thomasnaw (not verified)

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

Thomasnaw (not verified)

hot live sex cams [url=https://sexcams00.club]sexcams00[/url] hidden cams pinay teen sex.

Thomasnaw (not verified)

live home sex cams [url=https://sexcams00.xyz]free streaming sex cams [/url] sex cams justi.

Thomasnaw (not verified)

black live sex cams [url=https://sexcams00.club]woman live sex cams [/url] bongacams live sex cams.

JamesTOURI (not verified)

free home sex cams [url=https://sexcams00.work]sexcams00[/url] forcing unwilling milf into sex acts cams.

JamesTOURI (not verified)

free home sex cams [url=https://sexcams00.work]sexcams00[/url] forcing unwilling milf into sex acts cams.

JamesTOURI (not verified)

shared sex cams free [url=https://sexcams00.website]sexcams00[/url] sex cams for tokens.

JamesTOURI (not verified)

shared sex cams free [url=https://sexcams00.website]sexcams00.website[/url] free sex cams in manchester n.h..

JamesTOURI (not verified)

libe sex cams [url=https://sexcams00.space]japan sex cams [/url] craigslist personals no cc or sign up or upgrades public sex cams.

JamesTOURI (not verified)

lesbian sex cams [url=https://sexcams00.work]indian live sex cams [/url] sex cams online.

JamesTOURI (not verified)

free live private sex cams [url=https://sexcams00.website]sexcams00.website[/url] older milf resisting sex cams.

JamesTOURI (not verified)

personal sex cams [url=https://sexcams00.club]sexcams00.club[/url] live sex cams apps.

JamesTOURI (not verified)

personal sex cams [url=https://sexcams00.club]sexcams00.club[/url] live sex cams apps.

JamesTOURI (not verified)

personal sex cams [url=https://sexcams00.club]sexcams00.club[/url] live sex cams apps.

JamesTOURI (not verified)

free asian sex cams [url=https://sexcams00.work]lesbian live sex cams [/url] web cams wont display on asian cam sex.

JamesTOURI (not verified)

live young sex cams [url=https://sexcams00.space]private sex cams [/url] just sex chat, no cams.

JamesTOURI (not verified)

free live sex cams couples [url=https://sexcams00.live]sexcams00[/url] 1 on one sex cams.

JamesTOURI (not verified)

live sex cams naked [url=https://sexcams00.online]sexcams00.online[/url] milf resists sex cams.

JamesTOURI (not verified)

college live sex cams [url=https://sexcams00.live]sexcams00.live[/url] 25 cents sex cams.

JamesTOURI (not verified)

sex cams no registration [url=https://sexcams00.space]sexcams00.space[/url] live sex cams straight men in u.s.a..

JamesTOURI (not verified)

live sex cams guys [url=https://sexcams00.xyz]sexcams00.xyz[/url] peoplecam.weebly.com sex cams.

JamesTOURI (not verified)

live sex cams asian [url=https://sexcams00.xyz]sexcams00[/url] sex cams porn.

JamesTOURI (not verified)

teen live sex cams [url=https://sexcams00.website]chatubate sex cams [/url] reallife spy appartment sex cams.

Estherchera (not verified)

Lqpvfy myxfyh generic viagra online Real viagra without prescription

JamesTOURI (not verified)

erotic sex cams [url=https://sexcams00.site]live sex cams [/url] you tube hidden apt sex cams blogs.

JamesTOURI (not verified)

boy sex cams [url=https://sexcams00.space]how many girls stream sex cams [/url] life cams sex.

JamesTOURI (not verified)

free live web sex cams [url=https://sexcams00.space]indian sex cams [/url] instant sex with strange girls hiden cams.

JamesTOURI (not verified)

home milf amateurs sex cams [url=https://sexcams00.space]sexcams00.space[/url] asian girls sex bf in hidden cams.

Add new comment