public static ManagedFile::validateManagedFile(&$element, FormStateInterface $form_state, &$complete_form)
Render API callback: Validates the managed_file element.
File
- core/modules/file/src/Element/ManagedFile.php, line 396
Class
- ManagedFile
- Provides an AJAX/progress aware widget for uploading and saving a file.
Namespace
Drupal\file\Element
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public static function validateManagedFile(& $element , FormStateInterface $form_state , & $complete_form ) { // If referencing an existing file, only allow if there are existing // references. This prevents unmanaged files from being deleted if this // item were to be deleted. $clicked_button = end ( $form_state ->getTriggeringElement()[ '#parents' ]); if ( $clicked_button != 'remove_button' && ! empty ( $element [ 'fids' ][ '#value' ])) { $fids = $element [ 'fids' ][ '#value' ]; foreach ( $fids as $fid ) { if ( $file = File::load( $fid )) { if ( $file ->isPermanent()) { $references = static ::fileUsage()->listUsage( $file ); if ( empty ( $references )) { // We expect the field name placeholder value to be wrapped in t() // here, so it won't be escaped again as it's already marked safe. $form_state ->setError( $element , t( 'The file used in the @name field may not be referenced.' , [ '@name' => $element [ '#title' ]])); } } } else { // We expect the field name placeholder value to be wrapped in t() // here, so it won't be escaped again as it's already marked safe. $form_state ->setError( $element , t( 'The file referenced by the @name field does not exist.' , [ '@name' => $element [ '#title' ]])); } } } // Check required property based on the FID. if ( $element [ '#required' ] && empty ( $element [ 'fids' ][ '#value' ]) && !in_array( $clicked_button , [ 'upload_button' , 'remove_button' ])) { // We expect the field name placeholder value to be wrapped in t() // here, so it won't be escaped again as it's already marked safe. $form_state ->setError( $element , t( '@name field is required.' , [ '@name' => $element [ '#title' ]])); } // Consolidate the array value of this field to array of FIDs. if (! $element [ '#extended' ]) { $form_state ->setValueForElement( $element , $element [ 'fids' ][ '#value' ]); } } |
Please login to continue.