wagtailmedia
wagtailmedia copied to clipboard
Feature Request: Allow thumbnails to be chosen from Wagtail image chooser
In my custom media model, I would like to allow my users to select their thumbnails from their existing image library. While it's possible to override the "thumbnail" field with the wagtailimages model, it does not look like it is possible to enable the ImageChooserPanel widget on the edit form (therefore making it impossible to choose an image/actually use the image model).
I understand this may be more of a limitation of Wagtail than of this package, since Wagtail does not allow you to specify a widget under admin_form_fields
. (See https://github.com/wagtail/wagtail/issues/2610)
Would it be possible to provide an override the "widgets" object under get_media_form
in forms.py
?
For example:
# wagtailmedia/forms.py
# ...
def get_media_form(model):
# ...
if (not model.custom_widgets):
widgets = {
'tags': widgets.AdminTagWidget,
'file': forms.FileInput(),
'thumbnail': forms.ClearableFileInput(),
}
else:
widgets = custom_widgets
return modelform_factory(
model,
form=BaseMediaForm,
fields=fields,
widgets=)
Then in my custom model:
from wagtail.images.fields import WagtailImageField
# other imports
class CustomMedia(AbstractMedia):
# Override the "thumbnail" field to use my custom Wagtail Images model
thumbnail = models.ForeignKey(
'images.CustomImage',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
admin_form_fields = (
'title',
'file',
'thumbnail',
'collection',
'tags',
)
custom_widgets = {
'tags': widgets.AdminTagWidget,
'file': forms.FileInput(),
'thumbnail': WagtailImageField(),
}
Hey @juan0tron,
This is rather late, but you can create your own media form and set in settings via WAGTAILMEDIA_MEDIA_FORM_BASE
https://github.com/torchbox/wagtailmedia/blob/master/wagtailmedia/forms.py#L47 as of #83
With that you could specify your own widget
Given #83 (and a fix in #147) allow you to use your own media form, let's treat this as a documentation piece (i.e. "here's how to use Wagtail image for the thumbnail")
I intended to submit a PR that documents what we had to do to get this to work, but then I saw the "Upcoming breaking changes affecting Wagtail community projects", which warns against directly linking to Wagtail JS/CSS.
We had to create a custom image chooser which links to a few Wagtail JS files to get this working, so we might need to hold off on documenting this until the front-end changes land upstream. Here is an example of what we did:
class CustomImageChooser(AdminImageChooser):
@property
def media(self):
return forms.Media(js=[
versioned_static('wagtailadmin/js/modal-workflow.js'),
versioned_static('wagtailimages/js/image-chooser-modal.js'),
versioned_static('wagtailimages/js/image-chooser.js'),
])
I'd have to dig through my notes but my recollection is that AdminImageChooser implicitly relies on JS files already loaded in the Wagtail admin templates, so we had to explicitly load these files to get it to work. Obviously, if we did this the hard way, happy to learn of a simpler approach! And equally happy to work on a PR to document this the "right" way once that has been defined upstream. Thanks!