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

Cialis (not verified)

Propecia Risques

Cialis (not verified)

Soft Tabs Viagra

cialis 20 mg (not verified)

Prix Public Viagra

priligy 60 mg price (not verified)

brand name cialis

ivermectin amazon (not verified)

A Quand Le Generique Di Cialis

Priligy (not verified)

Cialis Hochste Konzentration

Lasix (not verified)

Valtrex Online Purchase

where to buy pr... (not verified)

Viagra Moins Cher France

furosemide 40 m... (not verified)

Cialis Diabete

Priligy (not verified)

Mens Health Viragra

what is priligy... (not verified)

Costo Viagra Pfizer

Lasix (not verified)

Cialis Precio Oficial

Prednisone (not verified)

Pharmacie On Line

Lasix (not verified)

Stendra (Avanafil) Tablets

Priligy (not verified)

Can Amoxicillin Make You Lightheaded

Prednisone (not verified)

Pcn Without Apresrciption

Priligy (not verified)

Finasteride 20mg For Sale

is prednisone a... (not verified)

Propecia Hirsutismo

Stromectol (not verified)

dosis maxima de kamagra

Prednisone (not verified)

Amoxicillin Dogs

Prednisone (not verified)

Viagra Preiswert Kaufen

Prednisone (not verified)

Como Puedo Comprar Viagra Sin Receta

prednisone for ... (not verified)

Buy Cialis Brand Online

priligy equivalent (not verified)

Prednisolone

Prednisone (not verified)

Cialis 20mg Emra Med

azithromycin ov... (not verified)

cialis sources

cheapest cialis... (not verified)

Best Price For Cialis Tablets 5 Mg

buy hydroxychlo... (not verified)

que es viagra soft

plaquenil for s... (not verified)

With Out Prsciption Allopurinol 300 Mg

can plaquenil c... (not verified)

Fluoxetine Can I Purchase

zithromax azith... (not verified)

Canada Rx Customer Support

what the diffen... (not verified)

Atenolol And Amoxicillin

does hydroxychl... (not verified)

Fish Flex Cephalexin Ok For Dogs

is azithromycin... (not verified)

Keflex Treatment Bladder Infection

is azithromycin... (not verified)

Keflex Treatment Bladder Infection

gabapentin 600 (not verified)

Clomid Mois Par Mois

discount cialis (not verified)

Propecia Latente

overnight ciali... (not verified)

maximum dosage of cialis

cialis online n... (not verified)

viagra uso recreativo

generic for pla... (not verified)

Hydrochlorothiazide Hctz Without Dr Approval Lichfield

generic for pla... (not verified)

Hydrochlorothiazide Hctz Without Dr Approval Lichfield

coupons for cia... (not verified)

viagra posologie

gabapentin cost (not verified)

quiero comprar viagra farmacia

<a href="https:... (not verified)

The best online gambling sites With every bet To meet all needs of the gambler with a complete range

เกม123 (not verified)

The best online gambling sites With every bet To meet all needs of the gambler with a complete range

>เกม123 </a> (not verified)

The best online gambling sites With every bet To meet all needs of the gambler with a complete range

neurontin 300 mg (not verified)

Cialis Online Fast Delivery

viagra vs cialis (not verified)

Allergies Amoxicillin

cialis coupon (not verified)

Sildenafil Vente 120 Mg

gabapentin 300 ... (not verified)

Canada Pharmacy Mail Order