I'm using file_entity 7.x-2.0-alpha3+11-dev and media 7.x-2.0-alpha3+40-dev (the latest dev versions as I write this).

In the File Types there's an 'add' button, one can add multiple file types with different fields.

I created a "Photo" file type that has a few fields that only photos have.

Next, I created a content type for "Photo Gallery" and add an "Unlimited" photo field.

Here's where I'm stuck -- how do I make this content type reference the new file type I specified ?

In the settings the only reference to it is "Allowed remote media types" -- so I check "Photos"

But when I add a "Photo Gallery" and upload photos, when I click "Edit" I still see the fields from the "Image" file type.

It's great that I can create different file types and add different fields to them... but how do I use them?

Comments

tswaters’s picture

Turns out I had a setting under file settings enabled, "Skip filetype selection."

The file type is determined by mime type -- a series of 'candidates' are generated based on the file's mime type and the user is given a choice as to which one to use.

With 'skip filetype selection' enabled and multiple candidates, it sets it to 'undefined' .... I'm not sure how mine ended up getting set to 'Image', probably something I'm missing but this isn't really important.

There is a hook that can be used here, hook_file_type_alter( &$types, $file ) -- but I'm not entirely sure how I can identify the content type I'm using (Photo Gallery) and strip out the candidates to just photos.

I suppose I could just let site users know they need to select the right type -- but ideally I'd like to get this working as seamlessly as possible.... I'll post back here if I find out anything else.

tswaters’s picture

Component: Documentation » User interface
Status: Active » Fixed

This may not be ideal and probably isn't as efficient as it could but, but this seems to work so I'm posting it here in case this helps someone in the future. It turns out there isn't a way to set the type of the file_entity when using multiple uploads -- it just does it's thing and the form closes without showing additional options in the wizard.

To target a specific file entity type for a particular field with cardinality > 0 on a node type form, I implement a 'hook_form_FORM_ID_alter' for the node edit form. I set a '#media_options' value, to pick up on it later :

$element = &$form['field_photos'];
$lang = $element['#language'];
$element[$lang]['#media_options']['global']['source_bundle'] = $form['#bundle'];

I then implement a hook_form_FORM_ID_alter for the file_entity_add_upload_multiple form, check for the presence of this property and add a custom submit handler if it is found :

$build_args = $form_state['build_info']['args'];
if (isset($build_args[0]['source_bundle']) && $build_args[0]['source_bundle'] == 'my_node_form' ) {
  $form['#submit'][] = 'CUSTOM_MODULE_multiple_file_entity_submit';
}

In that submit handler, I iterate through all the files in the form_state and set the type to my custom type and call file_save.

function CUSTOM_MODULE_multiple_file_entity_submit (&$form, &$form_state) {
  foreach( $form_state['files'] as $file ) {
    $file->type = 'photos';
    file_save($file);
  }
}

Like I say, it's not pretty and there's probably better ways to accomplish this -- but it does the trick. It seems I didn't get many comments from this post so I'm going to go ahead and close this ticket. I think there should probably be a way to identify a file entity type to use with a particular field as a default and this doesn't seem to be possible.... but hey, it is what it is.

Status: Fixed » Closed (fixed)

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

kle’s picture

@tswaters
The function file_entity_add_upload_step_filetype simply ignores the field-bundle settings.
filed_entity 7.x-2.0-beta1
media 7.x-2.0-alpha4

I changed the function in this way:

/**
 * Generate form fields for the second step in the add file wizard.
 */
function file_entity_add_upload_step_filetype($form, &$form_state, array $options = array()) {
  $file = file_load($form_state['storage']['upload']);

  $candidates = file_entity_get_filetype_candidates($file);
  if ($options['types']) { // respect fieldbundle-settings
    $candidates = array_intersect_key($candidates, $options['types']);
  }
  $form['type'] = array(
    '#type' => 'radios',
    '#title' => t('File type'),
    '#options' => $candidates,
    '#default_value' => isset($form_state['storage']['type']) ? $form_state['storage']['type'] : NULL,
    '#required' => TRUE,
  );
  if(count($candidates)==1) { // only one to choose - set it as default.
    $form['type']['#default_value'] = key($candidates);
  }

  $form['actions'] = array('#type' => 'actions');

It would be perfect to NOT display this step if there is only one $candidate...

OFF’s picture

seems so hard..

Leagnus’s picture

Where 3-steps form is? I've created multiple upload image field in one content type.
When I upload files while creating a node – there is 1 step only.

It would be perfect to add selection of existing file types on 1-st step of add field for a content type form.

Added lately: module Media creates mediabrowser widget – so 2-nd step in image upload with this widget shows new custom file type.