Repository: wagtail/wagtail

Issue:
Document chooser upload form no longer populates title from filename
### Issue Summary

Within the upload form on the document chooser modal, when you select a document file, the title field is no longer populated from the filename.

### Steps to Reproduce

1. Set up a project with a homepage model containing a StreamField containing a DocumentChooserBlock, as per https://github.com/wagtail/wagtail/issues/8989#issuecomment-1213331089
2. Add a block and open the document chooser
3. Switch to the Upload tab
4. Select a file to upload

Expected: the Title field should be updated to a cleaned version of the filename
Actual: it remains blank

-   I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

### Technical details

-   Python version: 3.8.0.
-   Django version: 4.0.7
-   Wagtail version: 4.0rc1
-   Browser version: Chrome 104.0.5112.79 



Discussion:
Seems to have been introduced in 252f9dcc1fee3faec11a1aeefcfa5bd24cb7c4cf - I'm guessing the logic in ChooserViewSet for constructing the block class is failing to hook up the AdminDocumentChooser widget that implements this custom behaviour, and is just using the BaseChooser implementation.
> Seems to have been introduced in [252f9dc](https://github.com/wagtail/wagtail/commit/252f9dcc1fee3faec11a1aeefcfa5bd24cb7c4cf) - I'm guessing the logic in ChooserViewSet for constructing the block class is failing to hook up the AdminDocumentChooser widget that implements this custom behaviour, and is just using the BaseChooser implementation.

As I am following the same steps, I can reproduce the bug - Wagtail version 4.0rc1. However, as I am using the development version of Wagtail the widget does not even display. Also, the browser console logs some client-side errors. 

<img width="1440" alt="Screen Shot 2022-08-16 at 12 11 10 PM" src="https://user-images.githubusercontent.com/63321262/184928145-d58dd05f-e39f-4415-b751-1881d5ad6f1d.png">

This is happening because nothing ever has a reason to import `wagtail.documents.widgets`, now that the viewset class defines the widget itself, and everything works from the definitions there. `wagtail.documents.widgets` is where the telepath adapter gets registered, but since that never happens, telepath treats the AdminDocumentChooser widget as a plain BaseChooser - and it gets unpacked as a plain vanilla chooser on the client side. The solution is probably to make the viewset responsible for the telepath adapter too.

@mehrdadmoradii It sounds like you probably don't have the correct version of the js/css assets for the development instance - see https://docs.wagtail.org/en/stable/contributing/developing.html#compiling-static-assets

Resolving patch:
diff --git a/wagtail/admin/viewsets/chooser.py b/wagtail/admin/viewsets/chooser.py
--- a/wagtail/admin/viewsets/chooser.py
+++ b/wagtail/admin/viewsets/chooser.py
@@ -7,6 +7,7 @@
 from wagtail.admin.views.generic import chooser as chooser_views
 from wagtail.admin.widgets.chooser import BaseChooser
 from wagtail.blocks import ChooserBlock
+from wagtail.telepath import register as register_telepath_adapter
 
 from .base import ViewSet
 
@@ -45,6 +46,10 @@ class ChooserViewSet(ViewSet):
     #: The base Widget class that the chooser widget will be derived from.
     base_widget_class = BaseChooser
 
+    #: The adapter class used to map the widget class to its JavaScript implementation - see :ref:`streamfield_widget_api`.
+    #: Only required if the chooser uses custom JavaScript code.
+    widget_telepath_adapter_class = None
+
     #: The base ChooserBlock class that the StreamField chooser block will be derived from.
     base_block_class = ChooserBlock
 
@@ -204,3 +209,6 @@ def on_register(self):
             register_form_field_override(
                 ForeignKey, to=self.model, override={"widget": self.widget_class}
             )
+            if self.widget_telepath_adapter_class:
+                adapter = self.widget_telepath_adapter_class()
+                register_telepath_adapter(adapter, self.widget_class)
diff --git a/wagtail/documents/views/chooser.py b/wagtail/documents/views/chooser.py
--- a/wagtail/documents/views/chooser.py
+++ b/wagtail/documents/views/chooser.py
@@ -19,7 +19,7 @@
     CreationFormMixin,
 )
 from wagtail.admin.viewsets.chooser import ChooserViewSet
-from wagtail.admin.widgets import BaseChooser
+from wagtail.admin.widgets import BaseChooser, BaseChooserAdapter
 from wagtail.blocks import ChooserBlock
 from wagtail.documents import get_document_model
 from wagtail.documents.permissions import permission_policy
@@ -168,6 +168,18 @@ def media(self):
         )
 
 
+class DocumentChooserAdapter(BaseChooserAdapter):
+    js_constructor = "wagtail.documents.widgets.DocumentChooser"
+
+    @cached_property
+    def media(self):
+        return forms.Media(
+            js=[
+                versioned_static("wagtaildocs/js/document-chooser-telepath.js"),
+            ]
+        )
+
+
 class BaseDocumentChooserBlock(ChooserBlock):
     def render_basic(self, value, context=None):
         if value:
@@ -182,6 +194,7 @@ class DocumentChooserViewSet(ChooserViewSet):
     chosen_view_class = DocumentChosenView
     create_view_class = DocumentChooserUploadView
     base_widget_class = BaseAdminDocumentChooser
+    widget_telepath_adapter_class = DocumentChooserAdapter
     base_block_class = BaseDocumentChooserBlock
     permission_policy = permission_policy
 
diff --git a/wagtail/documents/widgets.py b/wagtail/documents/widgets.py
--- a/wagtail/documents/widgets.py
+++ b/wagtail/documents/widgets.py
@@ -1,24 +1,3 @@
-from django import forms
-from django.utils.functional import cached_property
-
-from wagtail.admin.staticfiles import versioned_static
-from wagtail.admin.widgets import BaseChooserAdapter
 from wagtail.documents.views.chooser import viewset as chooser_viewset
-from wagtail.telepath import register
 
 AdminDocumentChooser = chooser_viewset.widget_class
-
-
-class DocumentChooserAdapter(BaseChooserAdapter):
-    js_constructor = "wagtail.documents.widgets.DocumentChooser"
-
-    @cached_property
-    def media(self):
-        return forms.Media(
-            js=[
-                versioned_static("wagtaildocs/js/document-chooser-telepath.js"),
-            ]
-        )
-
-
-register(DocumentChooserAdapter(), AdminDocumentChooser)


Test patch: