Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding custom actions button to wagtail snippets

Screenshot of default actionsI have been trying to look through the documentation on how to add custom action buttons for wagtail snippets. No luck so far.

My wagtail version is 6.1.3

This is my snippet class.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class CurrentDayForecastViewSet(SnippetViewSet):
    model = CurrentDayForecast
    menu_label = 'Current Day Forecast'
    list_display = ('forecast_title', 'forecast_date', 'town')
    search_fields = ('forecast_title', 'forecast_date', 'town')

    panels = [
        FieldPanel('forecast_title'),
        MultiFieldPanel([
            FieldPanel('forecast_date', classname='col6'),
            FieldPanel('town', classname='col6'),
            FieldPanel('min_temperature', classname='col6'),
            FieldPanel('max_temperature', classname='col6'),
        ], heading="Forecast Details", classname='col12'),

        MultiFieldPanel(
            [
                FieldPanel('weather_condition_5AM', classname='col4'),
                FieldPanel('wind_direction_5AM', classname='col4'),
                FieldPanel('wind_speed_5AM', classname='col4'),
            ],
            heading='5AM',
            classname='collapsible col12'
        ),
        MultiFieldPanel(
            [
                FieldPanel('weather_condition_12PM', classname='col4'),
                FieldPanel('wind_direction_12PM', classname='col4'),
                FieldPanel('wind_speed_12PM', classname='col4'),
            ],
            heading='12PM',
            classname='collapsible col12'
        ),
        MultiFieldPanel(
            [
                FieldPanel('weather_condition_5PM', classname='col4'),
                FieldPanel('wind_direction_5PM', classname='col4'),
                FieldPanel('wind_speed_5PM', classname='col4'),
            ],
            heading='5PM',
            classname='collapsible col12'
        ),
        MultiFieldPanel(
            [
                FieldPanel('weather_condition_9PM', classname='col4'),
                FieldPanel('wind_direction_9PM', classname='col4'),
                FieldPanel('wind_speed_9PM', classname='col4'),
            ],
            heading='9PM',
            classname='collapsible col12'
        ),
    ]

By default the actions dropdown has "edit, copy, delete". I need to add a custom action button just for this snippet to run some custom logic.

Appreciate if anyone can point me to right direction.

>Solution :

Use the register_snippet_listing_buttons hook. From the documentation (added to a wagtail_hooks.py file in any application):

from wagtail import hooks
from wagtail.snippets import widgets as wagtailsnippets_widgets

@hooks.register('register_snippet_listing_buttons')
def snippet_listing_buttons(snippet, user, next_url=None):
    yield wagtailsnippets_widgets.SnippetListingButton(
        'A page listing button',
        '/goes/to/a/url/',
        priority=10
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading