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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

neoglez’s picture

Version: 7.0 » 7.2
Component: file system » javascript

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

coolestdude1’s picture

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).

coolestdude1’s picture

Priority: Normal » Major

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

marcingy’s picture

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.

pwolanin’s picture

Priority: Normal » Major

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

rfay’s picture

subscribe

olafkarsten’s picture

subscribe

dawehner’s picture

Status: Active » Needs review
FileSize
898 bytes

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.

dawehner’s picture

Component: javascript » file.module

Update the component.

pwolanin’s picture

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

dawehner’s picture

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.

prinds’s picture

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..

pwolanin’s picture

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

pwolanin’s picture

jessebeach’s picture

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.

webchick’s picture

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.

Anonymous’s picture

Issue summary: View changes

Fixed typo