We already have a way to designate custom file types via hook_file_type_info, but no way to let the assign an entity to a new bundle via this, as $file->type is automatically parsed via the mime type. It would be nice to expose a hook for this in file_entity_file_presave:

function file_entity_file_presave($file) {
  // Always ensure the filemime property is current.
  if (!empty($file->original) || empty($file->filemime)) {
    $file->filemime = file_get_mimetype($file->uri);
  }

  // Always update file type based on filemime.
  $file->type = file_get_type($file);
  
  //Allow other modules to alter the file object before presaving it. 
  drupal_alter('file_entity_presave', $file);

  field_attach_presave('file', $file);
}

That way you can now set the custom bundle and allow field_attach_presave to attach the fields from the proper bundle.

If there's interest, I can quickly submit a patch!

Comments

dave reid’s picture

Status: Active » Postponed (maintainer needs more info)

I'm not sure why you would want to change the file type? Also, you can just use module weights (or module_implements_alter()) to ensure your module's hook_file_presave() runs after file_entity_file_presave().

Jamie Holly’s picture

OHHHH module_implements_alter. That's a new hook I missed. I'll use that then. Thanks Dave!

Jamie Holly’s picture

Oh and this is a client request. They want only two files - image and generic files. The complaint was if you change the fields or display on say text, then you also got to change then on application (say for a PDF file). I can kind of see what they mean by the extra work. Of course I have no idea why people would constantly change around fields or displays, but you never know!

dave reid’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.