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

PaulScott (not verified)

This is the means by which drupal permissions are given to a specific page when you would prefer not to demonstrate the page for each client or part. So here we have learned how to make substances in Drupal 8 programmatically. In Drupal 8 you can make hub, user, scientific categorization without code, however here and custom assignment help there because of different task prerequisites and we have to make these programmatically.

issia (not verified)

I just finished downloaded windows 8 beta and its just a image or iso file dont know what to do with it and also I want to know if I can install drupal on it and use drupal with windows 8.
do my essay for me

Amelia Rose (not verified)

I am going to add an image in my website from which I am providing hnd assignment writing help services with using this code if I am facing any problem so take help from you. May you help me on that time.

annagrey (not verified)

I am 100% at best paper writing services reviews your back for this road safety forum you are having. It's a great way of instilling to the people the value of safety when on the road and thus, preventing any accidents from happening. In our country, the rate of minor road accidents seems to rise so this forum would be helpful especially if attended by people mainly using the road for transportation.

Anonymous (not verified)

Thanks for helping us out with this Drupal 8 problem. I have seen many people at the coursework writing services site talk about this problem, and now they will know how to do it without the chance of error. Love you for sharing this here.

Add new comment