I've been trying to come up with a solution for hiding the upload widget when other sources are enabled, with little luck. The best I've been able to accomplish was to hide "Upload" from the widget's sources list, but that's it.

I know it can be achieved with CSS, but I need something more radical.

Here's a possible use case:

  • say I have two node types: Gallery (used to upload images, with per-user upload limits) and Page (a regular page, with the possibility to reference existing images via the reference source
  • users should be allowed to upload only on the Gallery node type, so that maximum file upload limits can be honored.

I know Filefield Sources relies on the upload widget, which is why in my attempts I've always preserved all references to the validation processes and such, but the best I end up with is an unusable upload widget (unusable because it lacks an "Upload" button). I'm currently out of ideas on how to remove the file input field and make one of the secondary sources the default source. I've also looked into Filefield's theme_filefield_widget_file(), but with little or no luck—I'm not sure it's the right way to go, either.

Any help will be much appreciated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

antiorario’s picture

Title: Help needed with hiding upload widget for special cases » Disable upload widget for special cases
Version: 6.x-1.x-dev » 6.x-1.2
Assigned: Unassigned » antiorario
Status: Active » Needs review
FileSize
1.56 KB
1.1 KB

I've given up (for the time being) on the idea of completely hiding the upload widget, and I've settled with disabling it. I've created a new source (although it's a source only by name, not by function) that does the following:

  • sets the file input field as disabled
  • removes the upload button
  • adds a warning in the upload widget's description that says uploading is disabled for the content type in use.

Since this is not an actual source, it makes no sense for it to have its own entry in the sources list on the node edit form. Hence, the source has no label. This, however, means that there will be an empty spot in the sources list. To overcome this problem without breaking existing functionality, I propose an appropriate modification to theme_filefield_sources_list(), to show a source in the list only when the source has a label attribute.

In addition to this, I don't think there should be an "Upload" source in the widget's sources list when uploading is disabled, so I propose a further modification to the same function—again, this doesn't break any existing functionality. Here's what it would look like:

<?php
function theme_filefield_sources_list($element, $sources) {
  $links = array();

  // Add the default "Upload" since it's not in our list.
  if ( $element['filefield_upload'] ) {
    $default['upload'] = array(
      'label' => t('Upload'),
      'description' => t('Upload a file from your computer.'),
    );
    $sources = array_merge($default, $sources);
  }

  foreach ($sources as $name => $source) {
    if ( $source['label'] ) {
      $links[] = '<a href="#" onclick="return false;" title="' . $source['description'] . '" id="' . $element['#id'] . '-' . $name . '-source" class="filefield-source filefield-source-' . $name . '">' . $source['label'] . '</a>';
    }
  }
  return '<div class="filefield-sources-list">' . implode(' | ', $links) . '</div>';
}
?>

Attached: the proposed patch to filefield_sources.module (1.2) and the proposed noupload.inc source file.

sirviejo’s picture

And how about disable the upload widget for a specific role?

antiorario’s picture

sirviejo: it's definitely something worth considering, but that would mean giving a specific source (or pseudo-source, as in this case) a role-based setting while leaving the other sources without this option.

quicksketch can correct me if I'm wrong, but I think if we were to give the sources the option of role-based visibility, it should be accomplished via a module-wide hook that any source (including those provided by external modules) should be able to tap into. However, for what I've understood from older issues, I'm not sure that's the direction this module is supposed to be going.

arlinsandbulte’s picture

If #877452: Enable uploading and deletion through IMCE is acheived, I think we could just allow the default upload widget to be disabled and allow IMCE to fill in.
Then, the excellent IMCE profile & permission settings could be used.

antiorario’s picture

This issue is not related to IMCE, and my proposal should work independent of IMCE.

jenlampton’s picture

Status: Needs review » Closed (duplicate)

This looks like a duplicate of #742134: I would like to disable the default "Upload" source in favour of using the upload option within IMCE ?

@arlinsandbulte: Note that using IMCE poses a huge security risk

arlinsandbulte’s picture

@jenlampton: Correct me if I am wrong, but yes, IMCE might pose a security risk if configured improperly or the wrong permissions are given to untrusted users.

antiorario’s picture

Status: Closed (duplicate) » Needs review

@jenlampton I don't think this is necessarily a duplicate of the issue you're referring to. Set aside any security concerns, my initial post had nothing to do with IMCE, while the other issue has everything to do with it. Also, while I do consider both solutions to fall into the category of tweaks, I'm trying to find something that can achieve the goal system-wide, without resorting to hiding content via CSS (which is theme-dependent and doesn't prevent users from using the upload widget if they want to).

Please, let's not close issues only because they sound like something else.

antiorario’s picture

Status: Needs review » Closed (fixed)

I'm closing this, because I've just released a module that hooks into FileField Sources and achieves this exact goal, including the theme function override I suggested at #1.

I'm marking it as fixed because this is, in fact, a solution. FileField Sources Disable Upload: http://drupal.org/project/filefield_sources_noupload (As usual, the dev version should be available later.)

quicksketch’s picture

Component: Code » General
Status: Closed (fixed) » Active

Reopening this as a potential feature for inclusion directly in FileField Sources. I've said in the past that this was not possible, but I've clearly been proven wrong. Let's think about this as a patch to FIleField Sources instead of an additional module.

quicksketch’s picture

Assigned: antiorario » Unassigned
antiorario’s picture

quicksketch, it seems I missed your response on this issue. Of course it would be much better if it were part of FileField Sources—any progress on that?

dazweeja’s picture

@antiorario, I added some JS to your module so that the interface was cleaner for users. Obviously, it could be cleaner still if it was integrated directly into Filefield Sources.

#1177230: Added JS so that first file source is automatically displayed

antiorario’s picture

New dev of FileField Sources Disable Upload will include dazweeja's contribution.

iNade’s picture

Is there any port for D7 module ? Just asked in the filefield_sources_noupload project issue queue.

iNade’s picture

Version: 6.x-1.2 » 7.x-1.4
quicksketch’s picture

Status: Active » Closed (duplicate)