Score:0

Programmatically attach an image to a field

as flag

I am using Drupal 9.3.13 on localhost. I generate successfully a QR Code based on the .SVG format and store that image on public://. I created various field types, one of which is a field_file_svg field type.

The goal is to attach that existing image uploaded to the public:// directory and attach it to field_file_svg. I am using hook_entity_presave() for this. The QR Code is generated and stored successfully, but the node doesn't show anything.

In /admin/reports/dblog/ it throws the following error.

Warning: Creating default object from empty value in urlist_entity_presave() (line 83 of C:\MAMP\htdocs\drupal9\modules\custom\urlist\urlist.module)

This the code I am using

$qrcode = (new QRCode($options))->render("$output");
$file_data = file_get_contents($qrcode);
$file = file_save_data($file_data, 'public://t10.svg', FileSystemInterface::EXISTS_REPLACE);

if (!empty($file)) { 
  $file_arr = array(
    "target_id" => $file->id(),
    "alt" => "Alt1",
    "title" => "Title1"
  );       
   
  $node->field_file_svg = $file_arr;
}
4uk4 avatar
cn flag
This is not a Drupal issue. See https://stackoverflow.com/questions/8900701/creating-default-object-from-empty-value-in-php
mariosdaskalas avatar
as flag
Thanks for pointing that out. However, I do not get it. I am using PHP version: 7.4.1. The error is at $node->field_file_svg = $file_arr;. I do not understand what I should do.
mariosdaskalas avatar
as flag
@4uk4 Apache Database client version: libmysql - mysqlnd 7.4.1 PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation PHP version: 7.4.1
4uk4 avatar
cn flag
This PHP warning means you are trying to set the field on an empty variable `$node`.
mariosdaskalas avatar
as flag
@4uk4 But how it's empty? I assign it with $node->field_file_svg = $file_arr; I'am confused.
mariosdaskalas avatar
as flag
@4uk4 I've used devel and I saw the structure of the field.
4uk4 avatar
cn flag
In a default \stdClass object?
mariosdaskalas avatar
as flag
@4uk4 I've used $node = new \stdClass(); $node->success = false; $node->field_file_svg = $file_arr; with no errors in dblog. Still the image is generated succesfully, but does not get attached in the field.
4uk4 avatar
cn flag
I take this as a confirmation devel shows a stdClass object. This is not a node. You have to assign a node to $node first in order to set a field value to the node.
mariosdaskalas avatar
as flag
@4uk4 I'm really sorry, but I am new to Drupal. How can I do that? With $node = Node::load(1);
mariosdaskalas avatar
as flag
@4uk4 I've used dpm and print_r and I can see stdClass Object ( [success] => [field_file_svg] => Array ( [target_id] => 21 [alt] => Alt1 [title] => Title1 ) ) field_file_svg
mariosdaskalas avatar
as flag
@4uk4 I can see the field_file_svg in fieldDefinitions just fine, but not in the #values array in devel.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.