Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Allow thumbnails to be chosen from Wagtail image chooser #64

Open
glitchwolfe opened this issue Nov 5, 2019 · 3 comments
Milestone

Comments

@glitchwolfe
Copy link

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 wagtail/wagtail#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(),
        }
@zerolab
Copy link
Member

zerolab commented Nov 6, 2020

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

@zerolab
Copy link
Member

zerolab commented Feb 4, 2022

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")

@jsma
Copy link
Contributor

jsma commented Feb 15, 2022

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!

@zerolab zerolab added this to the 0.12 milestone Oct 15, 2022
@zerolab zerolab modified the milestones: 0.12, The big tidy up Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants