During altering a form I realized that form '#states' are currently not working with managed file fields.
To reproduce:

1. Create a new custom form
2. Add two fields
2.1. Image field
2.2. Boolean field
3. Configure the form state for the image field that it should just be shown when the boolean field value is TRUE.

It works for every other field type except this one.

Membership dollars fund testing for the Drupal project. Drupal Association Learn more

Comments

sg88 created an issue. See original summary.

jrockowitz’s picture

I can confirm that this is an issue.

It is due to the #attributes being reset via template_preprocess_file_managed_file().

As soon as $variables['attributes'] = array(); is removed template_preprocess_file_managed_file() the #states API starts working as expected.


/**
 * Prepares variables for file form widget templates.
 *
 * Default template: file-managed-file.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: A render element representing the file.
 */
function template_preprocess_file_managed_file(&$variables) {
  $element = $variables['element'];

  # The below line of code needs to be removed for #states API to work properly.
  $variables['attributes'] = array();
  if (isset($element['#id'])) {
    $variables['attributes']['id'] = $element['#id'];
  }
  if (!empty($element['#attributes']['class'])) {
    $variables['attributes']['class'] = (array) $element['#attributes']['class'];
  }
}

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

deltahex’s picture

The same problem exists for the "datetime" form field as well. The template_preprocess function is identical but the above solution is not applicable in this case. I will spend some time on it and will provide an update here.

Also there is a duplicated "id" attribute for the element wrapper div and the file input element itself.

wheelercreek’s picture

I don't know if this is related, but when using filefields on a wizard page (multi-page) YAML form, I'm suddenly getting these warnings about array expected..

Warning: end() expects parameter 1 to be array, null given in Drupal\file\Element\ManagedFile::validateManagedFile() (line 400 of core/modules/file/src/Element/ManagedFile.php).
Drupal\file\Element\ManagedFile::validateManagedFile(Array, Object, Array) (Line: 505)
Drupal\yamlform\Plugin\YamlFormElement\YamlFormManagedFileBase::validate(Array, Object, Array)
call_user_func_array(Array, Array) (Line: 1024)
Drupal\yamlform\YamlFormSubmissionForm::hiddenElementValidate(Array, Object, Array)
call_user_func_array(Array, Array) (Line: 277)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 235)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 235)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 235)

SchnWalter’s picture

Status: Active » Needs review
FileSize
603 bytes

Indeed, the drupal_process_states() uses the data-drupal-states attribute to pass JSON encoded data to the Drupal.behaviors.states.

SchnWalter’s picture

Title: Form states managed file fields » Allow states on managed file form widgets

Status: Needs review » Needs work

The last submitted patch, 6: CORE-managed_files_field_states-2705471-6.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

idebr’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2847425: #states not affecting visibility/requirement of managed_file

Hello everyone,

This issue was also reported at #2847425: #states not affecting visibility/requirement of managed_file. The related issue has a patch and contains an automated test to prevent this bug from occurring in the future.

I'll close this issue as a duplicate, so we can focus our efforts in the related issue.