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

prednisolone in... (not verified)

cheap pills cialis

pregabalin vs g... (not verified)

Buy Cheap Kamagra Online

Neurontine (not verified)

Cheapest Nonprescription Bactrim

neurontine side... (not verified)

internet pharmacy cialis buy online

somjad sadden (not verified)

สล็อตแตกง่ายThe gambling website that slots is the easiest in the country. Easy to break. Give away a lot quickly.

somjad sadden (not verified)

สล็อตแตกง่ายThe gambling website that slots is the easiest in the country. Easy to break. Give away a lot quickly.

789heng (not verified)

"Online games, easy to play, easy to break, easy to withdraw, with automatic deposit-withdrawal system, play on mobile phones
at 789heng only. สล็อตแตกง่าย"

saksun kholoy (not verified)

บาคาร่าThe Belgian retrospective website is comparable, easy to get out, easy to get rich.

saksun kholoy (not verified)

บาคาร่าOnline casino websites that baccarat is easy to break, easy to bet, easy to guess, the most in the UFA group.

saksun kholoy (not verified)

ufaOnline casino websites that get easy money, slots are easy to break, baccarat breaks quickly, water balls, the best commission in the UFA group

saksun kholoy (not verified)

บาคาร่าOnline gambling website that results withdraw easily Easy to see cards, must be ufapony88

ราคาบอล (not verified)

ราคาบอล naza55 can made your relax if you strain in the real life you can enjoy with gambling games in this website.

Cialis (not verified)

Cialis Canada Viagra

buy fincar 5mg cheap (not verified)

Viagra Cialis Unterschied

Propecia (not verified)

Buy 1 Mg Prednisone Pills Online

Lasix (not verified)

Viagra Bestellen Empfehlung

Propecia (not verified)

Order Xenical Online Australia

furosemide and ... (not verified)

cialis propiedades

Cialis (not verified)

Propecia Online Us

Lasix (not verified)

Buy Zithromax Online Us

Stromectol (not verified)

Cialis 100mg Dosage

viagra use in dogs (not verified)

levitra testosterone

daily cialis online (not verified)

Buy Valtrex Without Insurance

viagra brand na... (not verified)

super cialis

propecia hk (not verified)

Cialis 10 Mg Pharmacie

Stromectol (not verified)

Giardiasis Amoxicillin

Cialis (not verified)

Real Secure Elocon Purchasing Visa Online Cash Delivery

viagra ireland ... (not verified)

Levitra Tarif Maroc

finasteride 5mg... (not verified)

kamagra para ellas

buy cialis 5mg ... (not verified)

Amoxicillin For Tooth Abcess

Viagra (not verified)

How Much Does Levitra Cost

purchase cialis (not verified)

levitra 10mg dessin anime

propecia long t... (not verified)

Discount Generic Hydrochlorothiazide

cialis and viag... (not verified)

Comprar Levitra Contra Reembolso

best cialis online (not verified)

Propecia Low Testosterone

Viagra (not verified)

Can You Take Aspirin With Cephalexin

Viagra (not verified)

viagra cialis levitra

cialis (not verified)

Buy Viagra In Amsterdam

ivermectin for ... (not verified)

Baclofen Vente

viagra action (not verified)

Zithromax Order Online

Propecia (not verified)

Ou Acheter Kamagra

viagra professi... (not verified)

cialis other benefits

Cialis (not verified)

Kamagra Ungarn

Cialis (not verified)

Nbrx Pharmacy Next Day Delivery

ger prescriptio... (not verified)

Candian Pharmcy

Viagra (not verified)

cialis pas cher lyon

cialis reviews (not verified)

Cialis Garantito

Viagra (not verified)

Generic Lasix Online

order viagra online (not verified)

Best Buy Stendra No Prior Script El Paso

cialis and viag... (not verified)

Cialis Original Bestellen