public static ManagedFile::valueCallback(&$element, $input, FormStateInterface $form_state)
Determines how user input is mapped to an element's #value property.
Parameters
array $element: An associative array containing the properties of the element.
mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
mixed The value to assign to the element.
Overrides FormElement::valueCallback
File
- core/modules/file/src/Element/ManagedFile.php, line 58
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | public static function valueCallback(& $element , $input , FormStateInterface $form_state ) { // Find the current value of this field. $fids = ! empty ( $input [ 'fids' ]) ? explode ( ' ' , $input [ 'fids' ]) : []; foreach ( $fids as $key => $fid ) { $fids [ $key ] = (int) $fid ; } $force_default = FALSE; // Process any input and save new uploads. if ( $input !== FALSE) { $input [ 'fids' ] = $fids ; $return = $input ; // Uploads take priority over all other values. if ( $files = file_managed_file_save_upload( $element , $form_state )) { if ( $element [ '#multiple' ]) { $fids = array_merge ( $fids , array_keys ( $files )); } else { $fids = array_keys ( $files ); } } else { // Check for #filefield_value_callback values. // Because FAPI does not allow multiple #value_callback values like it // does for #element_validate and #process, this fills the missing // functionality to allow File fields to be extended through FAPI. if (isset( $element [ '#file_value_callbacks' ])) { foreach ( $element [ '#file_value_callbacks' ] as $callback ) { $callback ( $element , $input , $form_state ); } } // Load files if the FIDs have changed to confirm they exist. if (! empty ( $input [ 'fids' ])) { $fids = []; foreach ( $input [ 'fids' ] as $fid ) { if ( $file = File::load( $fid )) { $fids [] = $file ->id(); // Temporary files that belong to other users should never be // allowed. if ( $file ->isTemporary()) { if ( $file ->getOwnerId() != \Drupal::currentUser()->id()) { $force_default = TRUE; break ; } // Since file ownership can't be determined for anonymous users, // they are not allowed to reuse temporary files at all. But // they do need to be able to reuse their own files from earlier // submissions of the same form, so to allow that, check for the // token added by $this->processManagedFile(). elseif (\Drupal::currentUser()->isAnonymous()) { $token = NestedArray::getValue( $form_state ->getUserInput(), array_merge ( $element [ '#parents' ], array ( 'file_' . $file ->id(), 'fid_token' ))); if ( $token !== Crypt::hmacBase64( 'file-' . $file ->id(), \Drupal::service( 'private_key' )->get() . Settings::getHashSalt())) { $force_default = TRUE; break ; } } } } } if ( $force_default ) { $fids = []; } } } } // If there is no input or if the default value was requested above, use the // default value. if ( $input === FALSE || $force_default ) { if ( $element [ '#extended' ]) { $default_fids = isset( $element [ '#default_value' ][ 'fids' ]) ? $element [ '#default_value' ][ 'fids' ] : []; $return = isset( $element [ '#default_value' ]) ? $element [ '#default_value' ] : [ 'fids' => []]; } else { $default_fids = isset( $element [ '#default_value' ]) ? $element [ '#default_value' ] : []; $return = [ 'fids' => []]; } // Confirm that the file exists when used as a default value. if (! empty ( $default_fids )) { $fids = []; foreach ( $default_fids as $fid ) { if ( $file = File::load( $fid )) { $fids [] = $file ->id(); } } } } $return [ 'fids' ] = $fids ; return $return ; } |
Please login to continue.