I've tried to use the #states attribute to manage visibility (or enabled/disabled state) on a managed_file and it had no effect at all.

$form['source']=array(
'#title'=>t('Ad picture source'),
'#type'=>'radios',
'#required'=>TRUE,
'#options' => array('upload'=>t('File Upload'),'url'=> t('URL')),
'#default_value'=>'',
'#weight'=>-2.2,
);
$form['ad_file']=array(
'#title'=>t('Ad picture file'),
'#type'=>'managed_file',
'#required'=>FALSE,
'#default_value'=>'',
'#weight'=>-2.1,
'#states'=>array(
'visible'=>array(
':input[name="source"]'=>array('value'=>'upload')
),
),
);

Changing 'managed_file' by 'file' (or any other type) in this code works fine.
Files: 
CommentFileSizeAuthor
#14 1118016-states-managed_file-date-14.patch1.63 KBpwolanin
PASSED: [[SimpleTest]]: [MySQL] 32,910 pass(es).
[ View ]
#11 1118016-states-managed_file-date.patch1.65 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 32,900 pass(es).
[ View ]
#8 1118016-states-managed_file.patch898 bytesdawehner
PASSED: [[SimpleTest]]: [MySQL] 32,908 pass(es).
[ View ]

Comments

Version:7.0» 7.2
Component:file system» javascript

yap, bug confirmed.
it happens to me exactly as described above.

Confirmed same problem, even tried using invisible and visible together and still managed file stays visible in the form. Will be leaving the code in because no error is thrown (dblog, javascript alert, javascript console).

Priority:Normal» Major

Just found that the same problem exists for date form type. Elevating priority because of this.

Version:7.2» 8.x-dev
Priority:Major» Normal
Issue tags:+needs backport to D7

A work around for this is add items into a field set and then control visiablity based on the field, not ideal I admit but at the same time enough to put this back down to normal I believe. Also as it is a bug needs to go to d8 and be backported.

Priority:Normal» Major

Just ran in to this too - very annoying. I would consider this major. #states is broken.

subscribe

subscribe

Status:Active» Needs review
StatusFileSize
new898 bytes
PASSED: [[SimpleTest]]: [MySQL] 32,908 pass(es).
[ View ]

Justed runned into this as well, and i would say file_managed_file_process is broken...

Some reasons for this:

  • It adds a $id-ajax-wrapper around the widget, which seems to be not normal on drupal
  • It adds never the $id of the file_managed element.

Another thing i realized is that theme_file_managed_file doesn't add the id. Here is a patch which does this
and wooot this works.

Personally i fear that there are other form element which doesn't support this as well. For example theme_date looks somehow bad,
as well as tableselect, and perhaps many more.

Component:javascript» file.module

Update the component.

Yes, there is a note above about it also being broken for date fields.

StatusFileSize
new1.65 KB
PASSED: [[SimpleTest]]: [MySQL] 32,900 pass(es).
[ View ]

This patch fixes the date theme element as well.

I'm wondering whether all places should be fixed, or whether there should be created new issues.

A tip.. Until this patch is ported to v7 you can use a theme override to fix this in your theme or module..

function THEME_file_managed_file($variables) {
  $element = $variables['element'];
  $attributes = array();
  if (isset($element['#id'])) {
    $attributes['id'] = $element['#id'];
  }
  $attributes['class'] = 'form-managed-file';
  if (!empty($element['#attributes']['class'])) {
    $attributes['class'] .= ' ' . implode(' ', $element['#attributes']['class']);
  }
  // This wrapper is required to apply JS behaviors and CSS styling.
  $output = '';
  $output .= '<div' . drupal_attributes($attributes) . '>';
  $output .= drupal_render_children($element);
  $output .= '</div>';
  return $output;
}

for modules you should use the hook_theme_registry_alter to override..

I think you want to keep the classes as an array to pass to drupal_attributes

StatusFileSize
new1.63 KB
PASSED: [[SimpleTest]]: [MySQL] 32,910 pass(es).
[ View ]

Status:Needs review» Reviewed & tested by the community

This patch adds standard drupal_attributes pass-through that should have existed already to theme_date and theme_file_managed_file. It's ready to be committed.

Status:Reviewed & tested by the community» Fixed

Awesome, thanks folks. My kingdom for a JS test suite. :)

Committed and pushed to 8.x and 7.x.

Status:Fixed» Closed (fixed)
Issue tags:-needs backport to D7

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

Issue summary:View changes

Fixed typo