I am using the file entity patch in #1260050: Provide administrative UI for adding/removing file types so I have multiple image file types.

When I upload an image into a field I upload, press next, then have to select a file type from a list of my available image file types.

In a lot of cases this will be an unnecessary step as the field will be specific to the file type.

So it would be good if the field settings had a list of checkboxes for allowed file types and if only one file type is selected then that second step is skipped, otherwise the second step is choosing from the list of allowed types for that field.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Title: Add "allowed file types" option to field settings » Resolve the 'allowed file types' field widget setting with the file_entity forms

The allowed file types option already exists, it just isn't resolved with the new file_entity multi-step form workflow.

jstoller’s picture

Category: Feature request » Bug report
Issue summary: View changes

I'm fighting my way through this same problem right now. I have two file types that can be used with PDFs: document and board_agenda. I have one media upload widget that is supposed to support board_agenda exclusively, but when I upload a PDF it still asks me if the file should be classified as a board_agenda or a document. There is no media field on my site where this choice should be made by a user.

I'm changing this issue to a Bug Report, since it is clearly deviating from expected behavior.

I've been exploring the idea of using hook_file_type_alter() as a workaround, but unfortunately that function doesn't seem to have any knowledge of what field it was called from. $file->source is just listed as "upload." Other workaround suggestions would be appreciated while waiting for a fix.

jstoller’s picture

Here's the workaround I came up with. It seems to work, but any suggestions for improvement would be appreciated.

<?php
/**
 * Implements hook_file_type_alter().
 */
function MY_MODULE_file_type_alter(&$types, $file) {
  // If this is called from a media field using the media browser widget then
  // any allowed file types will be set in the $_GET array.
  if ($allowed_types = $_GET['types']) {
    // Intersect the allowed file types with those recommended for the current
    // file, based on its mimetype, to create a new $types array.
    $types = array_values(array_intersect($allowed_types, $types));
  }
}
?>
stevieegee’s picture

Not good. Below is better

stevieegee’s picture

FileSize
2.19 KB

This is a better patch.

joewhitsitt’s picture

#5 works for me. i have a unique file type that shares some mimetypes with document. even though i selected my unique file type and not document, the media workflow was having me select the file type after upload. this patch seems to workaround that and places it in the correct file type.

Jeroen_005’s picture

Project: D7 Media » File Entity (fieldable files)
Status: Active » Needs review

#5 works for me also. Maybe we can add this to the file_entity module?

Status: Needs review » Needs work

The last submitted patch, 5: file_entity.pages_.inc_.patch, failed testing.

Jeroen_005’s picture

Status: Needs work » Needs review
FileSize
2.03 KB

Retest :-)

Status: Needs review » Needs work

The last submitted patch, 9: 1901566-patch_file_types.patch, failed testing.

Leksat’s picture

Status: Needs work » Needs review
FileSize
192 bytes

I'm working on this, but some tests fail on my local on clean 7.x-2.x branch. Need to check if this happens with drupal.org testbot too.

Leksat’s picture

Leksat’s picture

Testbot does not work? Or I did something wrong?

jstoller’s picture

Looks like a typo in your file name. It should end in ".patch", but yours ends in ".-patch".

Leksat’s picture

Status: Needs review » Needs work

@jstoller, I did not get it, but anyway, I had no time to work on this. To quickly solve the issue, I applied #5 patch, and it works good.

mkhamash’s picture

Status: Needs work » Needs review
FileSize
2.4 KB

The logic in patch #5 is correct, I have only handled not selecting any file types and added comments to code.

Status: Needs review » Needs work

The last submitted patch, 16: resolve_the_allowed-1901566-16.patch, failed testing.

mkhamash’s picture

Status: Needs work » Needs review
FileSize
3.13 KB
1.49 KB

The field instance settings (Allowed types, Enabled plugins, etc ...) are not always available, since file_entity_add_upload() form is not used for fields only for example (file/add/upload). so I have took the liberty to initialize the $form['#options'] array to stop causing notices if not settings are available, this will solve the Test notices for this current patch but it should solve other problems down the road and limit the numbers of isset & empty we use in the future.

museumboy’s picture

If I add a file field to my form and choose Media as the widget I see options for selecting the appropriate file-type to accept. However when uploading I'm still asked to choose the file-type as if the prior settings had no relevance.

I really do not want to give anonymous users access to the media module. I think the multiupload or standard file widget is best here. Using those widgets I see no option for selecting file-type. How can I force this field to conform to a specific filetype?

Leksat’s picture

Status: Needs review » Reviewed & tested by the community

Manually tested #18 and reviewed its code. Works good, looks good!

gausarts’s picture

+1 RTBC. Thank you.

Rajab Natshah’s picture

+1

Rajab Natshah’s picture

Tested - still works with 2.0-beta3

potassiumchloride’s picture

Tested and confirmed to work on 2.0-beta3.

joseph.olstad’s picture

Possibly needs a reroll , or a re-upload of the patch in order to wake up the bot and run the simpletests.

Please reroll this patch if necessary, and re-upload it and set to "needs review" so as to trigger the simpletests.

jstoller’s picture

#18 didn't apply, so I rerolled the patch, but no changes to the code.

jstoller’s picture

Status: Needs review » Reviewed & tested by the community

Setting back to RTBC.

joseph.olstad’s picture

setting author

  • stevieegee authored fe30d62 on 7.x-2.x
    Issue #1901566 by mkhamash, stevieegee, jstoller, Leksat, Jeroen_005:...
joseph.olstad’s picture

Status: Reviewed & tested by the community » Fixed

fixed in 7.x-2.x dev

Status: Fixed » Closed (fixed)

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

cpena’s picture

I've found that the portion of this patch that checks the filetype causes the media browser window to disappear before I get the chance to set field values for a given media file upload. So I've removed this portion from file_entity_get_filetype_candidates().

joseph.olstad’s picture

cpena, perhaps you've not configured the plupload library correctly.

https://www.drupal.org/node/2843391

if you want bulk uploading to work you'll need to use the plupload library

to see a working environment you can have a look at the media_dev distribution as a base line and see how it's configured and what libraries it's using.

Or you can refer to the quick install guide https://www.drupal.org/node/2843391

this is a drush make file but gives you an idea of which modules, libraries to use. there's also some ACL settings if you want to enable that for ckeditor.

cpena’s picture

Hey jospeh.olstad, thank you for the recommendation. I tried out the make file settings and still experienced the issue media browser disappearing before I could get the chance to select file settings. The bulk upload functionality was working fine prior to applying the module update and the code snippet I indicated was the cause of the issue I experienced.

joseph.olstad’s picture

@cpena, your text format (html or rich text or other) is not configured correctly or your allowed file types are not configured for the correct types (file entity types , file entity file field for image/video/audio/document type and or media configuration and text format configuration check those). That is why this patch works for you because you're short circuiting the allowed file types.

I don't recommend patching like this, best to add the missing allowed file types specific to your desired configuration.

cpena’s picture

@josepg.olstad I'll keep the current version of the file entity module as is and modify my allowed file types configuration to ensure the media browser works. Thank you.