' . $this->t('About') . ''; $output .= '

' . $this->t('The File module allows you to create fields that contain files. See the Field module help and the Field UI help pages for general information on fields and how to create and manage them. For more information, see the online documentation for the File module.', [ ':field' => Url::fromRoute('help.page', [ 'name' => 'field', ])->toString(), ':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [ 'name' => 'field_ui', ])->toString() : '#', ':file_documentation' => 'https://www.drupal.org/documentation/modules/file', ]) . '

'; $output .= '

' . $this->t('Uses') . '

'; $output .= '
'; $output .= '
' . $this->t('Managing and displaying file fields') . '
'; $output .= '
' . $this->t('The settings and the display of the file field can be configured separately. See the Field UI help for more information on how to manage fields and their display.', [ ':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [ 'name' => 'field_ui', ])->toString() : '#', ]) . '
'; $output .= '
' . $this->t('Allowing file extensions') . '
'; $output .= '
' . $this->t('In the field settings, you can define the allowed file extensions (for example pdf docx psd) for the files that will be uploaded with the file field.') . '
'; $output .= '
' . $this->t('Storing files') . '
'; $output .= '
' . $this->t('Uploaded files can either be stored as public or private, depending on the File system settings. For more information, see the System module help page.', [ ':file-system' => Url::fromRoute('system.file_system_settings')->toString(), ':system-help' => Url::fromRoute('help.page', [ 'name' => 'system', ])->toString(), ]) . '
'; $output .= '
' . $this->t('Restricting the maximum file size') . '
'; $output .= '
' . $this->t('The maximum file size that users can upload is limited by PHP settings of the server, but you can restrict by entering the desired value as the Maximum upload size setting. The maximum file size is automatically displayed to users in the help text of the file field.') . '
'; $output .= '
' . $this->t('Displaying files and descriptions') . '
'; $output .= '
' . $this->t('In the field settings, you can allow users to toggle whether individual files are displayed. In the display settings, you can then choose one of the following formats: A file can still be linked to directly by its URI even if it is not displayed.') . '
'; $output .= '
'; return $output; } return NULL; } /** * Implements hook_field_widget_info_alter(). */ #[Hook('field_widget_info_alter')] public function fieldWidgetInfoAlter(array &$info): void { // Allows using the 'uri' widget for the 'file_uri' field type, which uses // it as the default widget. // @see \Drupal\file\Plugin\Field\FieldType\FileUriItem $info['uri']['field_types'][] = 'file_uri'; } /** * Implements hook_theme(). */ #[Hook('theme')] public function theme(): array { return [ // From file.module. 'file_link' => [ 'variables' => [ 'file' => NULL, 'description' => NULL, 'attributes' => [], ], ], 'file_managed_file' => [ 'render element' => 'element', ], 'file_audio' => [ 'variables' => [ 'files' => [], 'attributes' => NULL, ], ], 'file_video' => [ 'variables' => [ 'files' => [], 'attributes' => NULL, ], ], 'file_widget_multiple' => [ 'render element' => 'element', ], 'file_upload_help' => [ 'variables' => [ 'description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL, ], ], ]; } /** * Implements hook_ENTITY_TYPE_predelete() for file entities. */ #[Hook('file_predelete')] public function filePredelete(File $file): void { // @todo Remove references to a file that is in-use. See https://www.drupal.org/project/drupal/issues/1506314 } /** * Implements hook_form_FORM_ID_alter(). * * Injects the file sanitization options into /admin/config/media/file-system. * * These settings are enforced during upload by the FileEventSubscriber that * listens to the FileUploadSanitizeNameEvent event. * * @see \Drupal\system\Form\FileSystemForm * @see \Drupal\Core\File\Event\FileUploadSanitizeNameEvent * @see \Drupal\file\EventSubscriber\FileEventSubscriber */ #[Hook('form_system_file_system_settings_alter')] public function formSystemFileSystemSettingsAlter(array &$form, FormStateInterface $form_state): void { $config = \Drupal::config('file.settings'); $form['filename_sanitization'] = [ '#type' => 'details', '#title' => $this->t('Sanitize filenames'), '#description' => $this->t('These settings only apply to new files as they are uploaded. Changes here do not affect existing file names.'), '#open' => TRUE, '#tree' => TRUE, ]; $form['filename_sanitization']['replacement_character'] = [ '#type' => 'select', '#title' => $this->t('Replacement character'), '#default_value' => $config->get('filename_sanitization.replacement_character'), '#options' => [ '-' => $this->t('Dash (-)'), '_' => $this->t('Underscore (_)'), ], '#description' => $this->t('Used when replacing whitespace, replacing non-alphanumeric characters or transliterating unknown characters.'), ]; $form['filename_sanitization']['transliterate'] = [ '#type' => 'checkbox', '#title' => $this->t('Transliterate'), '#default_value' => $config->get('filename_sanitization.transliterate'), '#description' => $this->t('Transliteration replaces any characters that are not alphanumeric, underscores, periods or hyphens with the replacement character. It ensures filenames only contain ASCII characters. It is recommended to keep transliteration enabled.'), ]; $form['filename_sanitization']['replace_whitespace'] = [ '#type' => 'checkbox', '#title' => $this->t('Replace whitespace with the replacement character'), '#default_value' => $config->get('filename_sanitization.replace_whitespace'), ]; $form['filename_sanitization']['replace_non_alphanumeric'] = [ '#type' => 'checkbox', '#title' => $this->t('Replace non-alphanumeric characters with the replacement character'), '#default_value' => $config->get('filename_sanitization.replace_non_alphanumeric'), '#description' => $this->t('Alphanumeric characters, dots , underscores and dashes are preserved.'), ]; $form['filename_sanitization']['deduplicate_separators'] = [ '#type' => 'checkbox', '#title' => $this->t('Replace sequences of dots, underscores and/or dashes with the replacement character'), '#default_value' => $config->get('filename_sanitization.deduplicate_separators'), ]; $form['filename_sanitization']['lowercase'] = [ '#type' => 'checkbox', '#title' => $this->t('Convert to lowercase'), '#default_value' => $config->get('filename_sanitization.lowercase'), ]; $form['#submit'][] = 'file_system_settings_submit'; } }