The file scheme selection form (public, private, etc) that is present when the system has more than 1 scheme available does not respect the field's settings. My file field only allows private files, yet the form allows users to select public.

Here's a form alter I've been using to eliminate disallowed options. The same logic can be added to this module fix the issues. Let me know if you need/want a patch:

function hook_form_file_entity_add_upload_alter(&$form, &$form_state) {
  // The scheme selection step
  if ($form['#step'] == 3) {
    // See if the available schemes for this field are present
    if (isset($form_state['build_info']['args'][0]['schemes'])) {
      // Iterate the schemes for this field and remove options that are
      // not available.
      foreach ($form_state['build_info']['args'][0]['schemes'] as $scheme => $active) {
        if (!$active) {
          unset($form['scheme']['#options'][$scheme]);
        }
      }
      // If there is only one scheme left, default to it
      if (count($form['scheme']['#options']) == 1) {
        $form['scheme']['#default_value'] = key($form['scheme']['#options']);
      }
    }
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mstef’s picture

Better alteration:

/**
 * Implements hook_form_file_entity_add_upload_alter().
 */
function hook_form_file_entity_add_upload_alter(&$form, &$form_state) {
  // The scheme selection step
  if ($form['#step'] == 3) {
    // See if the available schemes for this field are present
    if (isset($form_state['build_info']['args'][0]['schemes'])) {
      // Iterate the schemes for this field and remove options that are
      // not available. This is a three-fold problem with file entity.
      foreach ($form['scheme']['#options'] as $scheme => $label) {
        if (isset($form_state['build_info']['args'][0]['schemes'][$scheme]) && $form_state['build_info']['args'][0]['schemes'][$scheme]) {
          continue;
        }
        unset($form['scheme']['#options'][$scheme]);
      }
      // If there is only one scheme left, default to it
      if (count($form['scheme']['#options']) == 1) {
        $form['scheme']['#default_value'] = key($form['scheme']['#options']);
      }
    }
  }
}
valderama’s picture

Thanks! This snippet works nicely.

It might be nice to skip this page completely if only one option is available.

jhedstrom’s picture

Status: Active » Needs review
FileSize
881 bytes

I was seeing this issue on a site where I had the scheme selection turned off completely. This worked great for files that were supposed to be stored in the default scheme. However, on a field where the only available scheme was not the default, it still tried to move the file to the default scheme.

The attached patch iterates through the available schemes for a given instance of the media browser, and removes any not available.

jhedstrom’s picture

FileSize
690 bytes
882 bytes

The last patch sometimes didn't work since disallowed schemes might not even be in the build arguments.

jhedstrom’s picture

This is the same as the patch in #04, but against a version of file_entity/media that is more stable (a few commits after last alpha). The one in #04 is the one to test, this is out of necessity since upgrading to later media/file entity is not working smoothly.

Status: Needs review » Needs work

The last submitted patch, 5: file_entity-3661d8b-2217841-05.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Needs review

#05 failed to apply the patch (as it should have). Sorry for the noise.

Yazzbe’s picture

applied #4 with no luck ...

I have a media field that specifies files to be uploaded to a private folder. But when I attach a file to the media field, it gets uploaded to the (unprotected) default location.

However, when clicking the edit button of the file afterwards, I can select 'private folder' and move the file over to the private location.

I'm can't write patches (themer). But I can help review and test solutions.

Drupal 7.31
File entity 7.x-2.0-alpha3+37-dev
Media 7.x-2.0-alpha3+114-dev

Devin Carlson’s picture

A patch to take any restricted schemes from field instance settings into account when adding a file through the file upload wizard.

For the related Media issue, see #1289680: Widget does not respect Allowed URI Schemes.

Dave Reid’s picture

Status: Needs review » Fixed

Tested and committed #9 to 7.x-2.x.

  • Dave Reid committed c96f802 on 7.x-2.x
    Issue #2217841 by jhedstrom, Devin Carlson | mstef: Fixed scheme...

Status: Fixed » Closed (fixed)

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