Adding custom actions button to wagtail snippets
I 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.
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.
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
)