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

viagra for plants (not verified)

cialis allergy

Viagra (not verified)

Cialis Versus Propecia

Propecia (not verified)

Propecia Adverse Reactions

best price for ... (not verified)

Cialis Jean Coutu

Viagra (not verified)

Vente De Tadalis Sx En France

bina pink (not verified)

นาซ่า Register for free online baccarat. No need to play Baccarat Poipet, just enter the website and get real money at home. Baccarat online, Baccarat formula is very popular to make money.

Cialis (not verified)

Viagra Canada. Shop

fincar 5mg (not verified)

Viagra Kaufen Augsburg

Cialis (not verified)

buy generic cialis fast shipping

Propecia (not verified)

Buy Amoxicillin Online Uk

MichaelMot (not verified)

gay free dating site in usa
gay desi dating in us
episcopal gay dating

viagra and well... (not verified)

Infected Tooth Still Hurts With Amoxicillin

5678 (not verified)

Iggyrenngoku479@gmail.com

tab ivermectin 3mg (not verified)

Order Prescription Water Pills

Cialis (not verified)

Como Comprar Cialis En Espana

Stromectol (not verified)

Order Viagra Online Fast Shipping

Stromectol (not verified)

Low Cost Pfizer Viagra

Plaquenil (not verified)

Amoxicillin Recommended Dosage

can you buy zit... (not verified)

Where To Purchase Clomphene

Zithromax (not verified)

cialis guidelines

Priligy (not verified)

Rio Rico Pharmacy Canadian Rx Net

Zithromax (not verified)

Buy Zithromax Single Dose Packets

Lasix (not verified)

cialis blindness

plaquenil buy online (not verified)

Zithromax Urine Smell

hydroxychloroqu... (not verified)

Amoxicillin For Sale In Us

adverse effects... (not verified)

Levaquin With Doctor Consult

Lasix (not verified)

Best And Quickest Viagra Online

Priligy (not verified)

Prix Kamagra France

Lasix (not verified)

Levitra Prezzo Farmacia Italia

Zithromax (not verified)

principio activo viagra

hydroxychloroqu... (not verified)

Viagra Zurich

plaquenil and h... (not verified)

Levitra Gunstig Bestellen

Priligy (not verified)

250 Mg Amoxicillin Capsule For Dogs

Zithromax (not verified)

Levitra Online Espana

plaquenil diarrhea (not verified)

What Amoxicillin Works For

azithromycin for std (not verified)

Viagra Effetti Sulla Salute

Priligy (not verified)

Cialis Plus Efficace Que Viagra

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

Online casino sites With every bet A complete answer to the gambler

5678 (not verified)

Iggyrenngoku534@gmail.com

Lasix (not verified)

Cialis Tarif Pharmacie

justin (not verified)

My wife style of bogus body art were being quite unsafe. Mother worked with gun first, after which they your lover snuck free upon an tattoo ink ink. I was sure the fact just about every should not be epidermal, due to the tattoo ink could be attracted from the entire body. make an own temporary tattoo รักษาสิวผด

Priligy (not verified)

Canadian Meds Paypal

prednisolone effect (not verified)

how long does cialis last

prednisone for ... (not verified)

Viagra Soft 20 Mg

gabapentin with... (not verified)

viagra cialis levitra mejor

Neurontine (not verified)

Prezzo Viagra Pfizer

does prednisone... (not verified)

dapoxetine clinical trials

Prednisone (not verified)

Walmart Prescription Drug Plan

gabapentin alcohol (not verified)

Priligy Does It Work

Neurontine (not verified)

Keflex Affects On Birth Control Pills