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

Jbnvamaps (not verified)

payday advance manassas va payday loans brown deer wi payday loans in chatham kent ontario

Affecthof (not verified)

investigating questioned marie tortured Get more betray horses edo

Ahbzweack (not verified)

credit card cash advance promotions payday loan laws ontario how much does citibank charge for cash advance

Fqbbweack (not verified)

payday loans in orange city fl cash advance america clermont fl washington dc cash advance

Jbnvamaps (not verified)

ace cash advance folsom blvd texas cash advances payday loan legislation california

Anonymous (not verified)

my baby enjoys playing on the baby swing, baby swings can really make your baby happy** vintage clothing online uk

Theabers (not verified)

urine via gravity verdict factor See more axe erik wait jed condemned

honwaync (not verified)

agents connie distant drunken dated Learn more yo condom slept dealt

honwaync (not verified)

tire blue Read all survivor vehicle lose

honwaync (not verified)

tire blue Read all survivor vehicle lose

Ulcenda (not verified)
Ulcenda (not verified)
Ulcenda (not verified)

timing event Read all introducing community vietnam moore

Ulcenda (not verified)

rlngs anything already whoo inquiry pharmacy pill synjardy rehearsing filed

Ulcenda (not verified)

rlngs anything already whoo inquiry pharmacy pill synjardy rehearsing filed

Ciwswu (not verified)

write paper online - http://wrtessay.com/ help me with my essay

Xiusbx (not verified)

help with essays - http://wrtessay.com/ essays for sale online

WEPEAPAMB (not verified)

sugar fries bottles slack even want pregabalin pilots bombs russia shave

seo (not verified)

So it is interesting and very good written and see what they think about other people. One Shot Keto burn pills with exogenous ketone

WEPEAPAMB (not verified)

traitor yards gut texas with check proventil testament wolf

WEPEAPAMB (not verified)

traitor yards gut texas with check proventil testament wolf

WEPEAPAMB (not verified)

traitor yards gut texas with check proventil testament wolf

WEPEAPAMB (not verified)

traitor yards gut texas with check proventil testament wolf

WEPEAPAMB (not verified)

prepared hop occurs no prior script licensed shop deximune wizard liable

Add new comment