How to alter the output of a file-field as a custom field-widget I am implementing a custom field-widget for the file-field in Drupal 8.

I extended the FileWidget class as my widget uses most of the file-field-widget functionality.

There is just a small part I want to theme/customize from the file upload form, the part I marked in the following screenshot:



What is the best way to alter the output from a field-widget?

I want to add some surrounding container elements and similar things you do in a theme function.
The file field for uploading is defined in the render callback of ManagedFile:

public static function processManagedFile(&amp;$element, FormStateInterface $form_state, &amp;$complete_form) {
  ...    

  // The file upload field itself.
  $element['upload'] = [
    '#name' =&gt; 'files[' . $parents_prefix . ']',
    '#type' =&gt; 'file',
    '#title' =&gt; t('Choose a file'),
    '#title_display' =&gt; 'invisible',
    '#size' =&gt; $element['#size'],
    '#multiple' =&gt; $element['#multiple'],
    '#theme_wrappers' =&gt; [],
    '#weight' =&gt; -10,
    '#error_no_message' =&gt; TRUE,
  ];


So one way to change the output of the field is to define a custom form element which extends File. The element can be themed as wanted.

And in the process callback of the custom widget the type of the upload element can be overwritten to use the newly defined element:

public static function process($element, FormStateInterface $form_state, $form) {
  $element = parent::process($element, $form_state, $form);
  $element['upload']['#type'] = 'myCustomFileElement';
  return $element;
}