When saving a new node, the $node object in the hook_node_insert() misses complete information about uploaded file fields. For instance, there`s no 'uri' and 'filename' items.
As a result, it`s impossible to operate uploaded files in the hook_node_insert(). For example, one can`t call the field_view_field() on the file fields.
Maybe, there`s some workaround?

Comments

inductor’s picture

Title: The $node in hook_node_insert() isn`t populated with file fields data. » The $node in hook_node_insert() isn`t populated with the file fields` data.
mile23’s picture

hook_node_insert() exists to give your module a chance to save/update information to DB when the node is being saved. Calling field_view_field() doesn't make much sense at that time.

I suggest looking at the Node and Field examples in Examples for Developers: http://drupal.org/project/examples

inductor’s picture

Thanks for the answer, Mile23. But imagine you want to send an email to site users when the node is created. And you want to include the file download links in the letter. Or, as in my case, you want to create а heartbeat entry with file information. Now you just can`t do it. I think it`s a bug.

fjen’s picture

+ subscribe

Isostar’s picture

I am also looking for a way to email the link of a file on the moment the node with the file is created.

Antonin Brunon’s picture

Hello,

I do experience this kind of problem.

I have a content type containing a file field named 'field_pdf', this field is of course compulsory.
When the user creates or updates a content of that type, I catch the $node in hook_node_insert & hook_node_update this way :

function my_module_node_insert($node)
{
if($node->type=="magazine")
{
my_module_create($node);
}
}

function my_module_node_update($node)
{
if($node->type=="magazine")
{
my_module_create($node);
}
}

Then I want to use the $node in 'my_module_create()' to get either the filename of the recently uploaded pdf file or its uri :

function my_module_create($node)
{
$sourceFilename = '/home/mydrupal/sites/default/files/public/pdf/' . $node->field_pdf['und'][0]['filename'];
$sourceUri = $node->field_pdf['und'][0]['uri'];
}

But looks like both $node->field_pdf['und'][0]['uri'] and $node->field_pdf['und'][0]['filename'] are undefined.
To be more precise, only 'filename' and 'uri' are not defined.

So, how am I supposed to get these ?

Thanks :)

Antonin Brunon’s picture

Hey,

I have somewhat solved my problem !
Hope this will save time to some of you :

So basically, both filename and uri aren't accessible at this step.
I figured out that the fid is available !

#get the file id aka 'fid'
$fid = $node->field_pdf['und'][0]['fid'];

#create clean and complete URL
$source = file_create_url(file_load($fid)->uri);

HF YA HOORS c:

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.