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

Asharseo (not verified)

Aw, i thought this was quite a nice post. In concept I must set up writing like this additionally – spending time and actual effort to make a great article… but exactly what do I say… I procrastinate alot through no means appear to go carried out. business

Williamdub (not verified)

free live naked sex cams [url=https://sexcams00.website]sexcams00.website[/url] hidden cams in school college dorm real sex and orgasm.

Williamdub (not verified)

free live naked sex cams [url=https://sexcams00.website]sexcams00.website[/url] hidden cams in school college dorm real sex and orgasm.

Williamdub (not verified)

free live naked sex cams [url=https://sexcams00.website]sexcams00.website[/url] hidden cams in school college dorm real sex and orgasm.

Williamdub (not verified)

free full sex cams [url=https://sexcams00.site]sexcams00.site[/url] audio sex cams.

Williamdub (not verified)

free full sex cams [url=https://sexcams00.site]sexcams00.site[/url] audio sex cams.

Williamdub (not verified)

black live sex cams [url=https://sexcams00.website]sexcams00.website[/url] hidden cams on nude beach sex.

Williamdub (not verified)

colombian sex cams [url=https://sexcams00.live]free live sex cams free [/url] free the realtime spy appartments sex cams.

Williamdub (not verified)

colombian sex cams [url=https://sexcams00.live]free live sex cams free [/url] free the realtime spy appartments sex cams.

Williamdub (not verified)

colombian sex cams [url=https://sexcams00.live]free live sex cams free [/url] free the realtime spy appartments sex cams.

Williamdub (not verified)

lesbian free sex cams [url=https://sexcams00.club]sexcams00.club[/url] brother sister live sex cams.

Williamdub (not verified)

free real sex cams [url=https://sexcams00.online]sexcams00.online[/url] realtime appartments h1 sex cams.

Williamdub (not verified)

free real sex cams [url=https://sexcams00.online]sexcams00.online[/url] realtime appartments h1 sex cams.

Williamdub (not verified)

sex cams girls [url=https://sexcams00.site]free sex cams no sign up [/url] free bbw sex cams.

Williamdub (not verified)

sex cams girls [url=https://sexcams00.site]free sex cams no sign up [/url] free bbw sex cams.

Williamdub (not verified)

xhamster sex cams [url=https://sexcams00.online]sexcams00.online[/url] hidden cams pinay sex scandals.

Williamdub (not verified)

sex cams no registration [url=https://sexcams00.work]total free sex cams [/url] black girl sex cams.

Williamdub (not verified)

sex cams xxx [url=https://sexcams00.work]couples sex cams [/url] tiny teen sex cams.

Williamdub (not verified)

free amature sex cams [url=https://sexcams00.site]sexcams00[/url] jerky sex cams mature.

Williamdub (not verified)

free amature sex cams [url=https://sexcams00.site]sexcams00[/url] jerky sex cams mature.

Williamdub (not verified)

free amature sex cams [url=https://sexcams00.site]sexcams00[/url] jerky sex cams mature.

Williamdub (not verified)

free male sex cams [url=https://sexcams00.online]sex cams no sign up [/url] hidden camera at sex hidden voyeur cams hidden spy cam sex.

Williamdub (not verified)

live sex cams bbw [url=https://sexcams00.work]live office sex cams [/url] free sex chats cams.

smithseo (not verified)

I’d ought to talk with you here. Which isn’t some thing It’s my job to do! I quite like reading a post that will get people to think. Also, thanks for permitting me to comment! best CBD oil

smithseo (not verified)

An impressive share, I merely given this onto a colleague who had been conducting a small analysis for this. And the man in reality bought me breakfast since I ran across it for him.. smile. So well then, i’ll reword that: Thnx to the treat! But yeah Thnkx for spending enough time go over this, I feel strongly regarding this and adore reading more about this topic. If you can, as you become expertise, do you mind updating your blog site to comprehend details? It really is extremely ideal for me. Big thumb up in this article! best CBD oil

Williamdub (not verified)

sex cams amateur [url=https://sexcams00.live]amature sex cams [/url] real life cams in cancun sex.

Williamdub (not verified)

sex cams amateur [url=https://sexcams00.live]amature sex cams [/url] real life cams in cancun sex.

Williamdub (not verified)

sex cams amateur [url=https://sexcams00.live]amature sex cams [/url] real life cams in cancun sex.

Georgedig (not verified)

fre live sex cams [url=https://sexcams00.site]sexcams00[/url] family sex cams site:xvideos.com.

Georgedig (not verified)

fre live sex cams [url=https://sexcams00.site]sexcams00[/url] family sex cams site:xvideos.com.

Georgedig (not verified)

turkish sex cams [url=https://sexcams00.club]online sex cams [/url] hidden cams used in men's public places for sex.

Georgedig (not verified)

turkish sex cams [url=https://sexcams00.club]online sex cams [/url] hidden cams used in men's public places for sex.

Georgedig (not verified)

turkish sex cams [url=https://sexcams00.club]online sex cams [/url] hidden cams used in men's public places for sex.

Georgedig (not verified)

boy sex cams [url=https://sexcams00.club]sexcams00.club[/url] random sex chat cams.

Georgedig (not verified)

boy sex cams [url=https://sexcams00.club]sexcams00.club[/url] random sex chat cams.

Georgedig (not verified)

boy sex cams [url=https://sexcams00.club]sexcams00.club[/url] random sex chat cams.

Georgedig (not verified)

boy sex cams [url=https://sexcams00.club]sexcams00.club[/url] random sex chat cams.

Georgedig (not verified)

sex cams free live [url=https://sexcams00.website]pantyhose sex cams [/url] camsacademy.com free live sex cams.

Georgedig (not verified)

sex cams free live [url=https://sexcams00.website]pantyhose sex cams [/url] camsacademy.com free live sex cams.

Georgedig (not verified)

sex cams free live [url=https://sexcams00.website]pantyhose sex cams [/url] camsacademy.com free live sex cams.

Georgedig (not verified)

sex cams free live [url=https://sexcams00.website]pantyhose sex cams [/url] camsacademy.com free live sex cams.

Georgedig (not verified)

sex cams free live [url=https://sexcams00.website]pantyhose sex cams [/url] camsacademy.com free live sex cams.

Georgedig (not verified)

webcam sex cams [url=https://sexcams00.club]sexcams00[/url] secret massage sex cum cams.

Georgedig (not verified)

free live ebony sex cams [url=https://sexcams00.work]my sex cams [/url] columbian women live sex cams.

Georgedig (not verified)

free live ebony sex cams [url=https://sexcams00.work]my sex cams [/url] columbian women live sex cams.

Georgedig (not verified)

free live sex cams porn [url=https://sexcams00.xyz]live amateur sex cams [/url] live gay sex web cams.

Georgedig (not verified)

free live sex cams porn [url=https://sexcams00.xyz]live amateur sex cams [/url] live gay sex web cams.

Georgedig (not verified)

luve sex cams [url=https://sexcams00.live]www live sex cams [/url] big thighs forced mature sex cams.

Georgedig (not verified)

hidden gay sex cams [url=https://sexcams00.website]sex cams jasmin [/url] free online sex cams with actual hot girls.

Georgedig (not verified)

hidden gay sex cams [url=https://sexcams00.website]sex cams jasmin [/url] free online sex cams with actual hot girls.

Add new comment