diff --git a/core/modules/file/file.module b/core/modules/file/file.module index de76bb4..efd4afb 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -1116,9 +1116,10 @@ function file_managed_file_submit($form, FormStateInterface $form_state) { $element = NestedArray::getValue($form, $parents); // No action is needed here for the upload button, because all file uploads on - // the form are processed by file_managed_file_value() regardless of which - // button was clicked. Action is needed here for the remove button, because we - // only remove a file in response to its remove button being clicked. + // the form are processed by \Drupal\file\Element\ManagedFile::valueCallback() + // regardless of which button was clicked. Action is needed here for the + // remove button, because we only remove a file in response to its remove + // button being clicked. if ($button_key == 'remove_button') { $fids = array_keys($element['#files']); // Get files that will be removed. @@ -1152,8 +1153,9 @@ function file_managed_file_submit($form, FormStateInterface $form_state) { // $form_state->getValues() must be updated in case additional submit // handlers run, and for form building functions that run during the // rebuild, such as when the managed_file element is part of a field widget. - // FormState::$input must be updated so that file_managed_file_value() - // has correct information during the rebuild. + // FormState::$input must be updated so that + // \Drupal\file\Element\ManagedFile::valueCallback() has correct information + // during the rebuild. form_set_value($element['fids'], implode(' ', $fids), $form_state); NestedArray::setValue($form_state->getUserInput(), $element['fids']['#parents'], implode(' ', $fids)); } diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php index 14d1dd8..f5a442c 100644 --- a/core/modules/file/src/Element/ManagedFile.php +++ b/core/modules/file/src/Element/ManagedFile.php @@ -203,8 +203,8 @@ public static function processManagedFile(&$element, FormStateInterface $form_st '#type' => 'hidden', '#value' => $upload_progress_key, '#attributes' => array('class' => array('file-progress')), - // Uploadprogress extension requires this field to be at the top of the - // form. + // Uploadprogress extension requires this field to be at the top of + // the form. '#weight' => -20, ); } @@ -213,8 +213,8 @@ public static function processManagedFile(&$element, FormStateInterface $form_st '#type' => 'hidden', '#value' => $upload_progress_key, '#attributes' => array('class' => array('file-progress')), - // Uploadprogress extension requires this field to be at the top of the - // form. + // Uploadprogress extension requires this field to be at the top of + // the form. '#weight' => -20, ); } @@ -276,16 +276,16 @@ public static function processManagedFile(&$element, FormStateInterface $form_st * * Upload controls are hidden when a file is already uploaded. Remove controls * are hidden when there is no file attached. Controls are hidden here instead - * of in file_managed_file_process(), because #access for these buttons depends - * on the managed_file element's #value. See the documentation of form_builder() - * for more detailed information about the relationship between #process, - * #value, and #access. + * of in file_managed_file_process(), because #access for these buttons + * depends on the managed_file element's #value. See the documentation of + * form_builder() for more detailed information about the relationship between + * #process, #value, and #access. * * Because #access is set here, it affects display only and does not prevent - * JavaScript or other untrusted code from submitting the form as though access - * were enabled. The form processing functions for these elements should not - * assume that the buttons can't be "clicked" just because they are not - * displayed. + * JavaScript or other untrusted code from submitting the form as though + * access were enabled. The form processing functions for these elements + * should not assume that the buttons can't be "clicked" just because they are + * not displayed. */ public static function preRenderManagedFile($element) { // If we already have a file, we don't want to show the upload controls. @@ -315,7 +315,7 @@ public static function validateManagedFile(&$element, FormStateInterface $form_s foreach ($fids as $fid) { if ($file = File::load($fid)) { if ($file->isPermanent()) { - $references = \Drupal::service('file.usage')->listUsage($file); + $references = static::fileUsage()->listUsage($file); if (empty($references)) { $form_state->setError($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title']))); } @@ -338,4 +338,13 @@ public static function validateManagedFile(&$element, FormStateInterface $form_s } } + /** + * Wraps the file usage service. + * + * @return \Drupal\file\FileUsage\FileUsageInterface + */ + protected static function fileUsage() { + return \Drupal::service('file.usage'); + } + } diff --git a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php index f9becf4..ab0c6a8 100644 --- a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php +++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php @@ -14,6 +14,7 @@ use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; +use Drupal\file\Element\ManagedFile; /** * Plugin implementation of the 'file_generic' widget. @@ -289,7 +290,7 @@ public static function value($element, $input = FALSE, FormStateInterface $form_ } // We depend on the managed file element to handle uploads. - $return = file_managed_file_value($element, $input, $form_state); + $return = ManagedFile::valueCallback($element, $input, $form_state); // Ensure that all the required properties are returned even if empty. $return += array(