I have a strange issue. My webform provides a file field. I have created a custom file entity named News (nebst the built-in document, audio, video, image), which has pdf as only file type allowed (like document). What I want is that files uploaded via my webform field are of type document.

When a user uploads a pdf file in this field, this file is listed under /admin/content/file, but as News, and not as Document, as intended.

I found absolutely not config setting to determine the file type which is used by webform.
First I thought that webform generally does not use entity fields - but this seems not to be the case with the file field?
There is the internal Drupal7 core file module, and the file_entity module, which extends the core file module, as far as I understand.
Which role does the "webform file type" play here? And why does it use a random file type?
I for myself can't find anything helpful in the code that would explain that.

So I must assume this is a bug - on the webform side?
Please feel free to mark this as invalid if I am wrong.

To reproduce:

* Install file_entity
* create a file type, with application/pdf as allowed upload type
* create a webform with a file field, allow pdf there.
* go to that webform and upload a pdf file
* go to /admin/content/file, and see the file

Probably, if you test this, it is a document, maybe the newly created one. I can't say which one is weighted higher. So YMMV.

Comments

nerdoc created an issue. See original summary.

nerdoc’s picture

Issue summary: View changes
nerdoc’s picture

I tracked this down a bit, see here too.

To copy the relevant parts here: Webform asks file_entity, exactly using file_get_type() which type the uploaded file is. This function invokes all hook_file_type, and if it is a pdf (in my case) gets an array of 2 types back: array("news", "document").

And then it calls:

return empty($types) ? NULL : reset($types);

Which then picks the (randomly) first element of the array and returns it as single item.

I don't know, but shouldn't it let the user choose here which type he wants? the Media module does this quite nice. Webform just does not play nice here with the file entity - when more than one file types have the same mimetype.

I for myself would NOT let the user, but let the admin choose which file entity type is allowed. Since Webform does not use it's own file "entity" thing, but the file_entity implementation, it should go for it and also take the full capability of the module into account...
But I don't know how to implement this.

nerdoc’s picture

I workarounded this with a custom hook, checking the source html tags where the uploaded files are put into the webform:

function MYMODULE_file_type_alter(&$types, &$file) {
  if($file->filemime == 'application/pdf' && ($file->source == 'submitted_WEBFORM_FILE_FIELD_ID')) {
      // in this case, always use type 'document'!
    $types = array('document');
  }
}

So, this is managable within file, and seems to be no webform bug (maybe a file entity bug).

nerdoc’s picture

I will add this to the bug at file entity - which seem so be responsible here.

nerdoc’s picture

Status: Active » Closed (works as designed)