I've got two file types - the default Document and a second custom file type, Vacancy Application. They both are tied to PDF and Word documents. I would like to use the Document file type on one particular file field and Vacancy Application on another file field. I can't see any current way of specifying which file type those file fields use. As it stands, having just created the custom Vacancy Application file type, it seems to be overriding the Document file type on all pdf/word document file uploads.

Is there any way I can specify which file field uses which file type?

Cheers

welly

Comments

welly’s picture

I'm just looking at the file_entity_file_presave function and it looks like this is where the file type is specified so perhaps I can create a helper module that will implement the hook_file_presave function and select the file type there. I'll give it a go.

Pierre.G’s picture

I couldn't figure out how to get the field_name in hook_file_presave function. I used hook_field_attach_presave function instead and force file->type for my filefield here.

/**
 * Implements hook_field_attach_presave().
 */
function MY_MODULE_field_attach_presave($entity_type, $entity) {
  if ($entity_type == 'node') {
    if ($entity->type == 'MY_NODE_TYPE') {
      $node_wrapper = entity_metadata_wrapper($entity_type, $entity);
      $file_value = $node_wrapper->MY_FILE_FIELD_NAME->value();
      $file = file_load($file_value['fid']);
      $file->type = 'MY_FILE_TYPE';
      file_save($file);
    }
  }
}

It works but I'm not really sure if it is the best way to do it.
I would prefer doing it when the file is saved.
Any feedbacks ?

cobenash’s picture

I am facing the same issue. Any feedbacks ?

@Pierre Did your code still work perfectly ?