There appears to be some sort of problem with file (and image) widgets getting broken by passing through the theme system too many times.
I had the following setup:
- a View's page display
- in my theme's hook_preprocess_views_view(), outputting a rendered block config entity
- the block's plugin was custom code, outputting an entity form
This all worked fine, until an file field was added to the entity type. At that point, attempting to add a file broke the form: validation failed on it, with the error message that the file field was required, even if a file had been set into the widget.
My hook_preprocess_views_view() was along the lines of:
$block_view_builder = \Drupal::entityManager()->getViewBuilder('block');
$block = \Drupal\block\Entity\Block::load('mycustomblock');
$build = $block_view_builder->view($block);
$variables['mycustomblock'] = $build;
// A custom twig.html took care of outputting {{mycustomblock}}.
and the custom block plugin's build() was:
$entity = $this->entityManager->getStorage('node')->create($values);
return $this->entityFormBuilder->getForm($entity, 'default');
The following workaround in hook_preprocess_views_view() fixes the problem:
$config = [];
$plugin_block = \Drupal::service('plugin.manager.block')
->createInstance('mycustomblock_plugin', $config);
$build = $plugin_block->build();
$variables['mycustomblock'] = $build;
This outputs the same thing, but doesn't get run through the Block entity's theming.
Thus it seems that the trip through #theme => block is what causes the problem for the file widget.