I'm struggling to add file to node in code. I know about File::create()
but I want to add file to node based on field configuration of the file field on that particular node's content type, and file's data comes as Base64, not URL or form post.
How do you do it? I'm asking as I want to add file via custom REST call as Base64.
I am using the following code.
$node = Node::load($data['case_id']);
$files = $node->field_file; // unlimited files
/* @var $df \Drupal\field\Entity\FieldConfig */
$df = $node->getFieldDefinition('field_file'); // ??? can i get this config for saving new file?
$file = file_save_data(base64_decode($data['data']), ??path??);
$files->appendItem($file)->validate();
$node->save();
Am I on the valid course? I have no clue how to set config from $df
and before save. How do I use the configuration of that file field during save?
Yurii