diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc index 22e4975..1eb4662 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -18,59 +18,6 @@ function file_field_info_alter(&$info) { } /** - * Retrieves the upload validators for a file field. - * - * @param array $field_settings - * The field settings. - * - * @return - * An array suitable for passing to file_save_upload() or the file field - * element's '#upload_validators' property. - */ -function file_field_widget_upload_validators(array $field_settings) { - // Cap the upload size according to the PHP limit. - $max_filesize = parse_size(file_upload_max_size()); - if (!empty($field_settings['max_filesize']) && parse_size($field_settings['max_filesize']) < $max_filesize) { - $max_filesize = parse_size($field_settings['max_filesize']); - } - - $validators = array(); - - // There is always a file size limit due to the PHP server limit. - $validators['file_validate_size'] = array($max_filesize); - - // Add the extension check if necessary. - if (!empty($field_settings['file_extensions'])) { - $validators['file_validate_extensions'] = array($field_settings['file_extensions']); - } - - return $validators; -} - -/** - * Determines the URI for a file field. - * - * @param array $field_settings - * The field settings. - * @param $data - * An array of token objects to pass to - * \Drupal\Core\Utility\Token::replace(). - * - * @return - * A file directory URI with tokens replaced. - * - * @see \Drupal\Core\Utility\Token::replace() - */ -function file_field_widget_uri(array $field_settings, $data = array()) { - $destination = trim($field_settings['file_directory'], '/'); - - // Replace tokens. - $destination = Drupal::token()->replace($destination, $data); - - return $field_settings['uri_scheme'] . '://' . $destination; -} - -/** * Render API callback: Retrieves the value for the file_generic field element. * * This function is assigned as a #value callback in file_field_widget_form(). diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php index 2b50e40..ef88c93 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php @@ -156,7 +156,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { '#options' => $scheme_options, '#default_value' => $this->getFieldSetting('uri_scheme'), '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'), - '#disabled' => $this->getInstance()->getField()->hasData(), + '#disabled' => $has_data, ); return $element; @@ -310,4 +310,55 @@ public function validateMaxFilesize($element, &$form_state) { } } + /** + * Determines the URI for a file field instance. + * + * @param $data + * An array of token objects to pass to token_replace(). + * + * @return + * A file directory URI with tokens replaced. + * + * @see token_replace() + */ + public function getUri($data = array()) { + $settings = $this->getFieldSettings(); + $destination = trim($settings['file_directory'], '/'); + + // Replace tokens. + if (!empty($data)) { + $destination = \Drupal::token()->replace($destination, $data); + } + + return $settings['uri_scheme'] . '://' . $destination; + } + + /** + * Retrieves the upload validators for a file field. + * + * @return + * An array suitable for passing to file_save_upload() or the file field + * element's '#upload_validators' property. + */ + public function getUploadValidators() { + $validators = array(); + $settings = $this->getFieldSettings(); + + // Cap the upload size according to the PHP limit. + $max_filesize = parse_size(file_upload_max_size()); + if (!empty($settings['max_filesize'])) { + $max_filesize = min($max_filesize, parse_size($settings['max_filesize'])); + } + + // There is always a file size limit due to the PHP server limit. + $validators['file_validate_size'] = array($max_filesize); + + // Add the extension check if necessary. + if (!empty($settings['file_extensions'])) { + $validators['file_validate_extensions'] = array($settings['file_extensions']); + } + + return $validators; + } + } diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php index 245ec34..ae2f6d2 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php @@ -184,8 +184,6 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang // The field settings include defaults for the field type. However, this // widget is a base class for other widgets (e.g., ImageWidget) that may act // on field types without these expected settings. - // @todo Add support for merging settings of base types to implementations - // of FieldDefinitionInterface::getFieldSettings(). $field_settings += array( 'display_default' => NULL, 'display_field' => NULL, @@ -204,8 +202,8 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang $element_info = element_info('managed_file'); $element += array( '#type' => 'managed_file', - '#upload_location' => file_field_widget_uri($field_settings), - '#upload_validators' => file_field_widget_upload_validators($field_settings), + '#upload_location' => $items[$delta]->getUri(), + '#upload_validators' => $items[$delta]->getUploadValidators(), '#value_callback' => 'file_field_widget_value', '#process' => array_merge($element_info['#process'], array('file_field_widget_process')), '#progress_indicator' => $this->getSetting('progress_indicator'),